Java ResultSet提前关闭

时间:2018-11-25 22:52:51

标签: java swing sqlite

我有一个似乎无法解决的问题。我正在进行注册并使用swing在Java中登录,并使用java.sql。*和jdbc将其连接到sqlite数据库。当我在注册后尝试登录时,显示错误ResultSet关闭。有人能帮我吗。我认为问题在于其中之一 如果没有,这里是项目的链接 https://bitbucket.org/jrdevtech/library-management-system/src 我相信会在AppGui中使用,我只在isFirstUse方法和两个按钮操作中使用了ResultSet


public void actionPerformed(ActionEvent arg0){

            PreparedStatement prep = null;
            try {
                String query = "INSERT INTO newuser"
                        + "(name, dob, phonenumber, username, 
                            password, uuid)"
                        + "VALUES (?,?,?,?,?,?)";
                connection = databaseConnection.dataConnector();
                prep = connection.prepareStatement(query);
                prep.setString(1, txtName.getText());
                prep.setString(2, txtDob.getText());
                prep.setString(3, txtPhone.getText());
                prep.setString(4, txtUsername.getText());
                prep.setString(5, txtPassword.getPassword().toString());
                prep.setString(6, UUID.randomUUID().toString());
                prep.executeUpdate();

            }catch(Exception e) {JOptionPane.showMessageDialog(null,e);}
            finally{
                try {
                    if(prep != null) {
                        prep.close();
                    }
                    if(connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        }
    });

public void actionPerformed(ActionEvent arg0){     PreparedStatement prep = null;

int correct = 0;

try {
    String query = "SELECT * FROM newuser WHERE username=? AND 
    password=?";
    connection = databaseConnection.dataConnector();
    prep = connection.prepareStatement(query);
    prep.setString(1, txtLoginUsername.getText());
    prep.setString(2, txtLoginPassword.getPassword().toString());
    ResultSet rs = prep.executeQuery();
    while(rs.next()){
        JOptionPane.showMessageDialog(null,rs.getString("username"));
        JOptionPane.showMessageDialog(null,rs.getString("password"));
        if(correct == 2) {isLoggedIn = true; 
           JOptionPane.showMessageDialog(null,"You Are Logged In");}
        else if(correct != 2) {JOptionPane.showMessageDialog(null, 
                "Invalid Login Credentials");}
    }
    JOptionPane.showMessageDialog(null, rs.getString("username"));
    }catch (SQLException e) {
                JOptionPane.showMessageDialog(null,e);
            }
            finally {
                try {

                    if(prep != null) {
                        prep.close();
                    }
                    if(connection != null) {
                        connection.close();
                    }
                }catch(Exception e){
                       JOptionPane.showMessageDialog(null,e);}
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

我发现结果集无法在查询中找到任何内容,因此关闭时我只是重新编写了一下查询,并重新命名了我如何保存已保存的密码,因为它与密码字段进行了比较