0

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());
}
2
  • WHERE USERNAME = ?, PASSWORD = ? - in which manual did you find that syntax? It should be WHERE USERNAME = ? and PASSWORD = ?. Commented Mar 1, 2016 at 8:03
  • Thank you almost of your answer the same. Your'e a God! :) Commented Mar 1, 2016 at 8:35

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Its already get rid of the error. Now I have the error "At least one parameter to the current statement is uninitialized." I already call my setString 1 and 2. Which 1st index is for Username and 2nd for Password? Thanks
your a life saver. Thankyou. Im just starting to learn jdbc thank you for the help.
1

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`

1 Comment

Thank you it already working. Maybe I just a little bit confuse with the syntax. By the way this help so much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.