日期:2014-05-20  浏览次数:20748 次

高手来看看这个鬼问题
Java code

public class UserDAOImpl implements UserDAO {
    public User Login(int userid,String password) {

        String sql = "select * from users where userid=? and password=?";

        PreparedStatement ps = null;

        ResultSet rs = null;
        try {
            ConFactory.DateBaseConnection();
            ps = ConFactory.getConnection().prepareStatement(sql);
            ps.setInt(1,userid);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                User LoginUser = new User(//这里明明搞了个实例了,怎么下面RETURN时说找不到LoginUser呢?其他地方都没问题
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getInt(4));                                
            }
            rs.close();
            ps.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("public boolean Login(User user) fail!!!!!!!!");
        } finally {
            ConFactory.closeConn();
        }
        return LoginUser;//报错没有找到LoginUser。unresorve..
    }
}




希望各位帮忙

------解决方案--------------------
User LoginUser =null;
try
{
……
if (rs.next()) {
LoginUser = new User(//这里明明搞了个实例了,怎么下面RETURN时说找不到LoginUser呢?其他地方都没问题
……

------解决方案--------------------
Java code

public class UserDAOImpl implements UserDAO {
    public User Login(int userid,String password) {

        String sql = "select * from users where userid=? and password=?";

        PreparedStatement ps = null;

        ResultSet rs = null;
        User LoginUser=null//先在这里声明,你在try里面声明,然后在try外面用,当然不行 
        try {
            ConFactory.DateBaseConnection();
            ps = ConFactory.getConnection().prepareStatement(sql);
            ps.setInt(1,userid);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                LoginUser = new User(//这里改                        
                            rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getInt(4));                                
            }
            rs.close();
            ps.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("public boolean Login(User user) fail!!!!!!!!");
        } finally {
            ConFactory.closeConn();
        }
        return LoginUser;//这样就不会报错了
    }
}