Home > Net >  java.sql.SQLSyntaxErrorException for seemingly alright script
java.sql.SQLSyntaxErrorException for seemingly alright script

Time:02-12

I am trying to make a simple login page that uses a MySQL database. I receive the following exception: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and password = md5(?)' at line 1

When trying to execute the following script:

    private static final String FIND_BY_LOGIN_AND_PASSWORD = "select * from user where login = ? and password = md5(?);";

This is where the PreparedStatement is created:

    PreparedStatement preparedStatement = connection.prepareStatement(FIND_BY_LOGIN_AND_PASSWORD);
    for (int i = 1; i <= parameters.length; i  ) {
        preparedStatement.setObject(i, parameters[i - 1]);
    }
    return preparedStatement;

This is where it's used:

   ResultSet resultSet = preparedStatement.executeQuery(query);

Any help would be appreciated

CodePudding user response:

As commented, you should call PreparedStatement#executeQuery without any argument. No need for you to pass a query to the execute command, as the prepared statement already contains your query.

ResultSet resultSet = preparedStatement.executeQuery();  // No argument.
  • Related