-3

Please help me. This code no error shows, but when it's run and finished it show error.

This is my code:

private void jBtn_UpdateActionPerformed(java.awt.event.ActionEvent evt {                                            

String user_id = txt_UserID.getText();

String cur_pass = txt_CurrentPassword.getText();

String new_pass = txt_NewPassword.getText();
    try{
        Connection c = DBConnection.dbconmethod();
        Statement s = c.createStatement();
        ResultSet rs = s.executeQuery("SELECT * from admin_data");

    while(rs.next()){
        String userid = rs.getString("user_id");
        String pass = rs.getString("password");
        if(user_id.equals(userid) && cur_pass.equals(pass)){
            s.executeUpdate("UPDATE admin_data SET password='"+new_pass+"'");
            UIManager.put("OptionPane.messageFont", new Font("Monospaced", Font.BOLD, 22));
            JOptionPane.showMessageDialog(new view.AdminPrivacy(), "Password Succesfully Changed!", null, JOptionPane.INFORMATION_MESSAGE);

        txt_UserID.setText(null);
        txt_CurrentPassword.setText(null);
        txt_NewPassword.setText(null);

        }else{
            UIManager.put("OptionPane.messageFont", new Font("Monospaced", Font.BOLD, 22));
            JOptionPane.showMessageDialog(new view.AdminPrivacy(), "Error : Invalid Data.", "Error Message", JOptionPane.ERROR_MESSAGE);
        }

        c.close();
        s.close();

    }   

    } catch (Exception e) {
       e.printStackTrace();       
    }

}       

This is error: https://i.stack.imgur.com/8COxh.png

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Riz buddy
  • 11
  • 1
  • 5
  • 3
    Please put the code and error as *text* in the question... then note that you're closing the connection and statement *in the loop*... – Jon Skeet Jan 20 '17 at 16:45
  • 1
    @JonSkeet, ok.. wait a minute plz... – Riz buddy Jan 20 '17 at 16:47
  • You should consider using [try with resources](http://stackoverflow.com/questions/26516020/try-with-resources-vs-try-catch) instead of a basic try-catch block. That will protect you from making this sort of error. – azurefrog Jan 20 '17 at 16:50

1 Answers1

0

You're closing the connection and statement in the while loop.

while(rs.next()) {
    ...
    c.close();
    s.close();
}

Try to use finally clause (after that catch (Exception) clause) and close such statements there or use ARM block.

ARM Block: This should automatically handle your connection closure as well.

try(Connection c = DBConnection.dbconmethod();
        Statement s = c.createStatement();) {
     ...
}
catch(Exception e) { ... }
Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52