简体   繁体   中英

Java Jdbc SQL Exception (Syntax Error)

I'm working a login sample for my Enrollment System. While doing some research grab some codes that I see on the internet. Which I will login in the fields of Username & Password. Where I encounter error using SQL. Any help, tips will appreciate!

ERROR

Error message: Syntax error: Encountered "," at line 1, column 42.
Error code: -1
SQL State: 42X01

This is where the compiler pointing the error at line 1, Column 42. I used Windows Listener for the which can I used for system closing.

Window Listener Code

//Window Listener
    addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent e){
           System.exit(0);
       }//window Closing
       });

Code This is where I login. When I finish filling up the username and password textfields and hit the login button Which where I encounter the error.

loginButton.addActionListener(new ActionListener (){
     public void actionPerformed(ActionEvent e){

         String inputUsername = usernameTextField.getText().trim();
         String inputPassword = String.valueOf(passwordPasswordField.getText());
         String inputUserType = listUser.getSelectedItem().toString();

          if(inputUsername.isEmpty() && inputPassword.isEmpty()){
                  JOptionPane.showMessageDialog(null, "Please fill up!");
          }
          else if(inputUsername.isEmpty()){
                 JOptionPane.showMessageDialog(null, "Please enter your username");
          }
          else if(inputPassword.isEmpty()){
                 JOptionPane.showMessageDialog(null, "Please enter your password");
          }
          else{
                String myQuery = "SELECT * FROM USERTYPE WHERE USERNAME = ?, PASSWORD = ?";

          try(Connection myConnection = DBUtil.getConnection(DBType.JDBC);
                       PreparedStatement myPs = myConnection.prepareStatement(myQuery);
                       ResultSet myResultSet = myPs.executeQuery()){ 

                        while(myResultSet.next())

                            myPs.setString(1, inputUsername);
                            myPs.setString(2, inputPassword);

                            myPs.executeUpdate();
                            JOptionPane.showMessageDialog(null, "Succesfuly Inserted!");
                        //end of else
                    }//end of try   

                   catch(SQLException ex) {
                        DBUtil.processException(ex);
                    }//end of catch
                    }//end of else
        }//end of action performed
            });//end of action listener`

DBUtil as my connection and exception This is where I set aside my connection and exception which where throws the error

private static final String dbUrl = "jdbc:derby://localhost:1527/Info";
private static final String username = "john";
private static final String pass = "john";
                                       //Argument for DBType
public static Connection getConnection(DBType dbType) throws SQLException{
    return DriverManager.getConnection(dbUrl,username,pass);
}

public static void processException(SQLException e){
    System.err.println("Error message: "+e.getMessage());
    System.err.println("Error code: "+e.getErrorCode());
    System.err.println("SQL State: "+e.getSQLState());
}

You need to use and instead of comma , in your query like below:

   String myQuery = "SELECT * FROM USERTYPE WHERE USERNAME = ? and PASSWORD = ?"

And you need to set the 2 parameters before myPs.executeQuery() like below:

  PreparedStatement myPs = myConnection.prepareStatement(myQuery);
  myPs.setString(1, some_userName);
  myPs.setString(2, some_password);

First of all there are so many syntax errors in your code, Either you have just copy pasted it from somewhere or you are careless to put your question with errors and hoping to find the right answer.

Second thing,

With the query you are trying the call executeUpdate is not correct. It is a Select query and you have to set the parameters first before calling executeQuery

String myQuery = "SELECT * FROM USERTYPE WHERE `USERNAME` = ? AND `PASSWORD` = ?";

          try(Connection myConnection = DBUtil.getConnection(DBType.JDBC);
                       PreparedStatement myPs = myConnection.prepareStatement(myQuery){


                            myPs.setString(1, inputUsername);
                            myPs.setString(2, inputPassword);
                       ResultSet myResultSet = myPs.executeQuery();


                        //end of else
                    }//end of try   

                   catch(SQLException ex) {
                        DBUtil.processException(ex);
                    }//end of catch
                    }//end of else
        }//end of action performed
            });//end of action listener`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM