-5

Should I put connection.close() inside finally on try catch block? I tried to put connection.close() but i got and error "java.lang.NullPointerException"

here is the code on logging in is it Good to put rs.close() and pst.close() in every statement likeso below i insert it becuz if it is not there something pop up like database is locked..

Login

if(evt.getKeyCode()==KeyEvent.VK_ENTER){
        String sql = "select * from User_Table\n" +
                 "inner join Role_Table on User_Table.Role_ID=Role_Table.Role_ID\n" +
                 "where Username = ? and Password = ? and Role_Name = ?";
    try {
        pst = conn.prepareStatement(sql);
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jPasswordField1.getText());
        pst.setString(3, (String) RoleComboBox.getSelectedItem());

        rs=pst.executeQuery();
    if(rs.next()){
        String A = rs.getString("First_Name");
        String B = rs.getString("Role_Name");
    if(RoleComboBox.getSelectedItem().toString().equals("Admin")){
        JOptionPane.showMessageDialog(null, "Hello "+B+" "+A,"Successfully Login",JOptionPane.INFORMATION_MESSAGE);
        rs.close();
        pst.close();
        AdminPortal AP = new AdminPortal();
        AP.setVisible(true);
        this.dispose();
    .... //and so on

    }else if(RoleComboBox.getSelectedItem().toString().equals("Laboratorist,EO")){
        JOptionPane.showMessageDialog(null, "Successfully Login");
        rs.close();
        pst.close();
    }}
    else{
        JOptionPane.showMessageDialog(null, "Incorrect Input","Error",JOptionPane.ERROR_MESSAGE);
    }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }finally {
       try {
           rs.close();
           pst.close();
           conn.close();
       }catch(Exception e){

       }
    }
    }

here is the code on logout when I didn't put the con.close() there my system works fine but when i logout and login again it starts to got some error like database is locked but when I put the con.close()" i got some error "java.lang.NullPointerException"

Logout

int YesOrNo = JOptionPane.showConfirmDialog(null, "Do you want to logout?", "Logout",JOptionPane.YES_NO_OPTION);
    if(YesOrNo == 0){
        try {
            dispose();
            LoginForm LF = new LoginForm ();
            LF.setVisible(true);
            AddPatient.getObj().dispose();
            AddAppointments.getObj().dispose();
        } catch (Exception ex) {

        }finally{
            try{
                rs.close();
                pst.close();
                conn.close();
            }catch(Exception e){

            }
        }
    }else{

    }

i hope someone can help

Sieccc
  • 49
  • 6

2 Answers2

0

If you have no further use of the connection you can close it on finally block.

Abdul Rahman Shamair
  • 580
  • 1
  • 11
  • 22
0

Yes, all types of connections should be closed in finally block.

TheSprinter
  • 1,523
  • 17
  • 30
  • i got an error sayin java.lang.NullPointerException i already put the conn.close() in every finally on try catch block – Sieccc Jan 03 '19 at 13:50
  • 1
    close connection if there is no further use of that connection. i think in your case you closed before its final use. – TheSprinter Jan 03 '19 at 16:59