0

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);
            }
        }
    });
Emre Turan
  • 83
  • 1
  • 8
  • 1
    You are comparing strings using == instead of equals() so your condition is never met, then you probably get an SQLException later that is catched and silently printed to the console, please check if this is the case. – Rocco May 02 '21 at 21:07
  • In addition, even if this works, you are doing blocking work on the Swing event thread. That will cause your GUI to be non-responsive until the query completes. You need to offload blocking work to another thread. On a separate note, you should offload the selection of the user to the database, by adding an appropriate where clause (and using a prepared statement with parameters) to only find the record matching the user name instead of fetching all records. – Mark Rotteveel May 03 '21 at 08:26

0 Answers0