【问题标题】:Java Sqlite Database returning null pointer exception except returning data [duplicate]Java Sqlite数据库返回空指针异常,除了返回数据[重复]
【发布时间】:2019-03-10 04:37:58
【问题描述】:

我正在用 Java 编写桌面应用程序,并且正在使用 SQLite 数据库。我正在从 SQLite 工作室获取数据,并且它已被获取,但是当我在应用程序中获取它时,我得到一个空指针。

下面是连接数据库和调用db相关函数的数据库管理器类:

数据库类:

public class DBManager {
Connection con=null;//build connection
Statement stmt=null;//execute query
static PreparedStatement pst= null;
public DBManager(){
    try {
        Class.forName("org.sqlite.JDBC");
        String url="jdbc:sqlite:mobileDB.db";
        con=DriverManager.getConnection(url);
        stmt=con.createStatement();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}
public void insertUpdateDelete(String query){
    try{
        stmt.execute(query);
    }catch(Exception ex){
        System.out.print(ex.toString());
    }
}
public ResultSet select(String query){
    try{
        ResultSet rs= stmt.executeQuery(query);
        return rs;
    }catch(Exception ex){
        ex.printStackTrace();
        return null;
    }
}
public ResultSet getLogInRecord(String user,String pass, String role) throws SQLException {
    String query="select * from Users where Username=? and Password=? and Role=?";
    pst.setString(0, user);
    pst.setString(1, pass);
    pst.setString(2, role);
    pst=con.prepareStatement(query);
    ResultSet rs=pst.executeQuery();
    return rs;

}

函数调用:

btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                String username=txtUsername.getText(),password=txtPass.getText(),role=cmbRole.getSelectedItem().toString();
                if(username.equals("")||password.equals("")||role.equals("Select")) {
                    JOptionPane.showMessageDialog(frame, "Please input all the data. No empty data allowed.","Empty Fields",JOptionPane.ERROR_MESSAGE);
                }else {
                    DBManager dbm= new DBManager();
                    //ResultSet rs= dbm.getLogInRecord(username, password, role);
                    ResultSet rs=dbm.select("select * from Users where Username='admin' and Password='admin' and Role='Admin'");
                    if(rs.next()) {
                        JOptionPane.showMessageDialog(frame, "True","Error",JOptionPane.ERROR_MESSAGE);
                    }else {
                        JOptionPane.showMessageDialog(frame, "False","Error",JOptionPane.ERROR_MESSAGE);
                    }
                }
            }catch (Exception e) {
                JOptionPane.showMessageDialog(frame, e,"Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    });

【问题讨论】:

  • 附加堆栈跟踪会有所帮助。
  • 我们需要查看您遇到的错误才能提供任何帮助。
  • 大概这里"pst=con.prepareStatement(query);",你是在初始化之前使用pst,先调用pst=con.prepareStatement(query);之后调用 pst.setString(0, user);

标签: java swing sqlite jdbc


【解决方案1】:

pst.setString(0, user) 是导致空指针的行。

pst=con.prepareStatement(query) 必须在它之前,因为它从未被初始化。

太棒了

public ResultSet getLogInRecord(String user,String pass, String role) throws SQLException {
   String query="select * from Users where Username=? and Password=? and Role=?";
   pst=con.prepareStatement(query);
   pst.setString(0, user);
   pst.setString(1, pass);
   pst.setString(2, role);
   ResultSet rs=pst.executeQuery();
   return rs;
}

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多