使用executeQuery时出现NullPointerException

发布于 2024-10-09 02:10:41 字数 722 浏览 1 评论 0原文

我的代码中有一个空指针异常

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;
}

}

I have a null pointer exception in

ResultSet rs = aStatement.executeQuery(Query); // it can't be executed 

my code is like this :

 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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弱骨蛰伏 2024-10-16 02:10:41

aStatement 很可能为 null。

Most likely aStatement is null.

深海少女心 2024-10-16 02:10:41

听起来您认为 aStatement 不应该为 null,但事实确实如此。

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

  1. 没有清理资源。
  2. 不使用PreparedStatement
  3. 不断地一遍又一遍地创建查询字符串,而不是使用静态变量
  4. 不遵循Java编码标准(“Query”应该是“query”)

这是另一种编写方式。从接口开始:

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;
    }
}

Sounds like you think aStatement should not be null, but it is.

This is bad JDBC code, for many reasons:

  1. No cleanup of resources.
  2. Doesn't use PreparedStatement
  3. Keeps creating the query string over and over again instead of using a static variable
  4. Doesn't follow Java coding standards ("Query" should be "query")

Here's another way to write it. Start with an interface:

package persistence;

import java.sql.SQLException;

public interface CredentialDao
{
    boolean isValidUser(String username, String password) throws SQLException;
}

Write an implementation:

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;
    }
}
瘫痪情歌 2024-10-16 02:10:41

aStatement 变量显然为 null,请验证其设置是否正确。您应该考虑阅读 Java 命名约定 并确保使用较低的变量和 java bean 约定的驼峰式大小写。

对于stackoverflow中的代码片段,如果它们不言自明,则应遵守SSCCE,这将帮助您获得更多更好的答案。您还应该提供发生异常的堆栈跟踪。

The aStatement variable is apparently null, please validate that it is correctly set. You should consider read the Java Naming Conventions and make sure you use the lower camel case for variables and java bean conventions.

For code snippets in stackoverflow if they are not self-explanatory, you should obey the rules of the SSCCE, this will help you to get more and better answers. Also you should provide a stack trace with the occured exception.

随梦而飞# 2024-10-16 02:10:41

使用准备好的语句

    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

Use prepared statements.

    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
寒冷纷飞旳雪 2024-10-16 02:10:41
 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?
}

info 指的是什么以及为什么在赋值之后立即有一个 return

 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?
}

What is info refering to and why is there a return imediatly after the assignments?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文