开发者

NullPointerException when using executeQuery

开发者 https://www.devze.com 2023-01-31 18:53 出处:网络
I have a null pointer exception in ResultSet rs = aStatement.exec开发者_StackOverflow社区uteQuery(Query); // it can\'t be executed

I have a null pointer exception in

ResultSet rs = aStatement.exec开发者_StackOverflow社区uteQuery(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;
}

}


Most likely aStatement is null.


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


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.


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


 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消