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