Basically, i have two tables named Users and Doctors. These tables both have "username" field and Doctors are pre-defined by system.
In GUI, when a user tries to register to system, he has to enter some information like ID, fullname, username, password... etc.
Usernames must be unique so if the username entered by the user is already a doctor's username, it should throw an exception but when i enter the info and press the button, it just stucks and does not respond at all.
register.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "SYSTEM", "1234");
System.out.println("Database Connected");
try {
String sql = "insert into users values(" + idnum.getText() + ", '" + fullname.getText() + "', '" + username1.getText() + "', '" + password1.getText() + "', '" + gender.getText() + "', '" + birthday.getText() + "', " +
ageTF.getText() + ", '" + allergy.getText() + "', '" + chronic.getText() + "')";
System.out.println(sql);
String selSQL = "select * from doctors";
Statement selstm = conn.createStatement();
ResultSet rs = selstm.executeQuery(selSQL);
while(rs.next()) {
if(rs.getString("username") == username1.getText()) {
selstm.close();
throw new SQLIntegrityConstraintViolationException();
}
}
Statement stm = conn.createStatement();
stm.executeUpdate(sql);
stm.close();
}
catch(SQLIntegrityConstraintViolationException SQLICVE) {
JOptionPane.showMessageDialog(null,"ERROR: A user with same ID or Username is already in system!!");
}
catch(Exception exp) {
System.out.println(exp);
}
finally {
conn.close();
}
}
catch(Exception exp) {
JOptionPane.showMessageDialog(null,"ERROR: Can Not Connect to System Right Now!!");
System.exit(0);
}
}
});