Java 使用executeQuery时出现NullPointerException

Java 使用executeQuery时出现NullPointerException,java,jdbc,resultset,Java,Jdbc,Resultset,我在中有一个空指针异常 ResultSet rs = aStatement.executeQuery(Query); // it can't be executed 我的代码如下: public static boolean testLogin(String user, String password) throws SQLException { String Query = "select * from TBL_Users where userName = '" + user +

我在中有一个空指针异常

ResultSet rs = aStatement.executeQuery(Query); // it can't be executed 
我的代码如下:

 public static boolean testLogin(String user, String password) throws SQLException {
    String Query = "select * from TBL_Users where userName = '" + user + "' and  passWord = '" + password + "' ";
    ResultSet rs = aStatement.executeQuery(Query);

    while (rs.next()) {

        info.Id = rs.getInt("ID");
        info.userName = rs.getString("userName");
        info.Name = rs.getString("User_Name");
        info.Password = rs.getString("passWord");
        info.isAdmin = rs.getBoolean("Admin");
        return true;
    }
    return false;
}

}最有可能的
aStatement
为空。

变量
aStatement
显然为
null
,请验证其设置是否正确。您应该考虑阅读并确保使用变量的下CAMEL案例和java bean约定。
对于stackoverflow中的代码段,如果它们不是自解释的,则应遵守的规则,这将帮助您获得更多更好的答案。您还应该提供一个包含发生异常的堆栈跟踪。

听起来像是您认为aStatement不应该为null,但它确实为null

这是糟糕的JDBC代码,原因有很多:

  • 没有清理资源
  • 不使用PreparedStatement
  • 不断重复创建查询字符串,而不是使用静态变量
  • 不遵循Java编码标准(“查询”应为“查询”)
  • 这里有另一种写作方法。从一个界面开始:

    package persistence;
    
    import java.sql.SQLException;
    
    public interface CredentialDao
    {
        boolean isValidUser(String username, String password) throws SQLException;
    }
    
    编写一个实现:

    package persistence;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class CredentialDaoImpl implements CredentialDao
    {
        private static final String CREDENTIAL_QUERY = "SELECT COUNT() FROM USER WHERE USERNAME = ? AND PASSWORD = ?";
    
        private Connection connection;
    
        public CredentialDaoImpl(Connection connection)
        {
            this.connection = connection;
        }
    
        public boolean isValidUser(String username, String password) throws SQLException
        {
            boolean isValidUser = false;
    
            PreparedStatement ps = null;
            ResultSet rs = null;
    
            try
            {
                ps = this.connection.prepareStatement(CREDENTIAL_QUERY);
                ps.setString(1, username);
                ps.setString(2, password);
                rs = ps.executeQuery();
                while (rs.next())
                {
                    int count = rs.getInt(1);
                    isValidUser = (count > 0);
                }
            }
            finally
            {
                DatabaseUtils.close(rs);
                DatabaseUtils.close(ps);
            }
    
            return isValidUser;
        }
    }
    
    使用


    什么是
    info
    指的是什么?为什么在作业结束后立即返回
    return

    什么是
    aStatement
    指的是什么?1。你没有给我们看堆栈跟踪。这可以消除90%的问题。2.您在结果集处理代码时遇到了问题。确切地说,
    aStatement
    ?构造函数中未初始化的私有字段?@Martijn Courtaux:这是一个反问:)请显示“aStatement”的定义。在字段中存储
    连接的DAO?我觉得这不是个好主意…连接是你的问题吗,杰迪?如果连接不是共享的,而是从每个线程的池中签出的,那么这个类将是线程安全的。@ColinD-Spring习惯用法是将连接注入DAO。DAO还可以如何参与与其他DAO的事务?您的备选方案是什么?让DAO创建自己的连接吗?我以为这就是连接池被发明的原因。@duffymo:我从来没有见过注入连接。。。刚刚注入数据源。您通常需要在池连接上调用
    close()
    ,将其返回到池中,以便其他事物可以使用它,并且您通常希望尽快这样做。是的,您是对的,它是唯一使用Spring注入的数据源。我也不要求关闭数据源;如果我在做SpringJDBC,DAO将扩展SimpleJdbcTemplate,它为我处理所有管道。因为这是直接的JDBC,没有Spring,所以我显式地传递了连接以说明一点:DAO被赋予了对数据源的访问权;它没有创造它。
        Connection con = ...; // obtain connection here
        PreparedStatement pstmt = con.prepareStatement("select * from TBL_Users where userName = ?'");
        pstmt.setInt(1, userName);
    
        ResultSet rs = pstmt .executeQuery();
    ...
    // do clean up here
    
     while (rs.next()) {
    
        info.Id = rs.getInt("ID");
        info.userName = rs.getString("userName");
        info.Name = rs.getString("User_Name");
        info.Password = rs.getString("passWord");
        info.isAdmin = rs.getBoolean("Admin");
        return true;       //                                Huh? What?
    }