与MySQL进行简单的数据比较,nullpointerexception

时间:2013-11-26 20:08:50

标签: java sql jdbc nullpointerexception

我目前正被学校深深吸引,他们希望我使用sql编写一个简单的登录表单。他们给了我们关于他们如何使用JDBC以及它们是什么的简短示例,但是没有真正逐步解释如何自己使用它。因此,我从示例中抢夺了一些代码,但我无法使其正常工作。我一直收到nullpointerexception,我无法弄清楚原因:(

这是连接类:

package Database;

import java.sql.*;

public class MySQLConnection {

    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String DBURL = "jdbc:mysql://localhost/corendon";
    public static final String DBUSER = "root";
    public static final String DBPASS = "simplepass";
    private ResultSet result = null;
    private int affectedRows = -1;
    Connection conn = null;

    public void startConnection() {
        try {

            Class.forName(DRIVER);
            DriverManager.setLoginTimeout(5);
            conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

        } catch (Exception e) {
        }
    }

    public void closeConnection() {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (Exception e) {
        }
        conn = null;
    }

    public ResultSet performSelect(PreparedStatement prdstmt) throws SQLException {
        result = prdstmt.executeQuery();

        return result;
    }

    public int performUpdate(PreparedStatement prdstmt) throws SQLException {

        affectedRows = prdstmt.executeUpdate();

        return affectedRows;
    }

    public Connection getConnection() {
        return conn;
    }
}

这是我在(在另一个类中)得到异常的方法:

MySQLConnection conn = new MySQLConnection();

public void compareData(int id, String pass) throws SQLException{
    ResultSet rs = null;
    PreparedStatement prdstmt = null;

    String query = "SELECT id, password FROM users WHERE id=?, password=?";

    conn.startConnection();

    prdstmt = conn.getConnection().prepareStatement(query);
    prdstmt.setInt(1, id);
    prdstmt.setString(2, pass);

    rs = conn.performSelect(prdstmt);

    while (rs.next()){
        String tempPass = rs.getString("password");
        int tempId = rs.getInt("id");
    }

    if(conn != null){
        conn.closeConnection();
    }
}

我正在线上获得nullpointerexception:

prdstmt = conn.getConnection().prepareStatement(query);

为什么它会在那里抛出一个异常,但是当我开始连接时它也不会解决这个问题?提前谢谢。

1 个答案:

答案 0 :(得分:0)

当你调用startConnection()时,你正在抛出一个Exception

public void startConnection() {
        try {

            Class.forName(DRIVER);
            DriverManager.setLoginTimeout(5);
            conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

        } catch (Exception e) {
            //An exception occurs here, but you don't do anything about it
        }
    }

因此,当您调用getConnection()时,conn变量仍然是null,这会抛出NullPointerException

使startConnection()抛出一个异常,以便你被迫处理它(这通常是大多数JDBC驱动程序的工作方式),或检查conn变量是否为{{1}在你开始使用它之前。

null

或(我认为个人会更好)

public void compareData(int id, String pass) throws SQLException{
    ResultSet rs = null;
    PreparedStatement prdstmt = null;

    String query = "SELECT id, password FROM users WHERE id=?, password=?";

    conn.startConnection();

    if (conn.getConnection() == null) {
        throw new SQLException("Connection is null!");
    }

另外,作为提示,您应该避免将您的类声明为与您正在使用的其他类相同的名称。这一切都发生在您自己的public void startConnection() throws Exception { Class.forName(DRIVER); DriverManager.setLoginTimeout(5); conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); } public void compareData(int id, String pass) throws SQLException{ ResultSet rs = null; PreparedStatement prdstmt = null; String query = "SELECT id, password FROM users WHERE id=?, password=?"; try { conn.startConnection(); } catch (Exception e) { throw new SQLException(e); } 课程中,但这可能会与实际的MySQLConnection课程相混淆。