executeQuery() terminating unexpectedly

Viewed 52

I have been writing login page with netbeans15. When I put the right value for both card number and password field it works intended work.

But when I put the wrong value for card number and password after this statement Rs= St.executeQuery(); the code is terminating, not even execute it's next statement System.out.println("Resultset"+ Rs);.

Rs not even contain null value for wrong card number and password.

My question is how to execute else statement for wrong card number and password? The code not entering into else block for wrong textfield!!

Connection Con = null;
     PreparedStatement pst = null;  
     ResultSet Rs=null;
     Statement St;
    private void LoginTbMouseClicked(java.awt.event.MouseEvent evt) {                                    
        if(UnameTb.getText().isEmpty() || PasswordTb.getText().isEmpty()){
            JOptionPane.showMessageDialog(this, "Enter Card and PIN Number");
        }else{
           String Query1 = "select * from AccountTbl where `Card Number`=? and `PIN`=?";

           try{
         
            Con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CREDITDEBITDb","Amaity","Alok@2022");
          
           PreparedStatement St = Con.prepareStatement(Query1);
           Rs= St.executeQuery();
            System.out.println("Resultset"+ Rs);

            
            if(Rs.next()){
                new MainMenu().setVisible(true);
                this.dispose();
               
            }else{
                JOptionPane.showMessageDialog(this, "Wrong Card Numner or PIN");
            }
        }catch(HeadlessException | SQLException e){
        }
        }      // TODO add your handling code here:
    }  
1 Answers

You cannot see any exceptions because you trap them and do nothing here:

catch(HeadlessException | SQLException e){
   // bare minimum is log message or
   e.printStackTrace();
   // ideally: throw e
   // or deal with issue here
}

You should log and pass on the reported exception.

The error is that you have not assigned values for the bind variables "?" in the statement. Assuming these are strings bind them with:

St.setString(1, cardNum);
St.setString(2, pin);
ResultSet rs = St.executeQuery();

It helps teams if you keep to Java naming conventions (camel case variables).

Refactor your code so that JDBC and GUI code are not mixed. You could have a helper call without the UI code, which would be called just to do the database work:

public boolean checkLogin(String cardNo, String pin) { ... }
Related