title: Should I put connection.close() on all finaly in java
body: <p>Should I put <strong>connection.close()</strong> inside finally on try catch block? I tried to put connection.close() but i got and error "<strong>java.lang.NullPointerException</strong>"</p>
  
  <p>here is the code on logging in is it Good to put <strong>rs.close()</strong> and pst.close() in every statement likeso below i insert it becuz if it is not there something pop up like database is locked..</p>
  
  <p><strong><em>Login</em></strong></p>
  
  <pre><code>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){
  
         }
      }
      }
  </code></pre>
  
  <p>here is the code on logout when I didn't put the <strong>con.close()</strong> 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 <strong>con.close()"</strong> i got some error "<strong>java.lang.NullPointerException</strong>"</p>
  
  <p><strong><em>Logout</em></strong></p>
  
  <pre><code>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{
  
      }
  </code></pre>
  
  <p>i hope someone can help</p>