0

Im having trouble getting my query statement running. Says there is a syntax error at the query statement but I'm pretty sure thats how it goes but clearly Im mistaken. Here is part of the code:

private String url = "jdbc:mysql://localhost:3306/MatrixUsers";
private String dbName = "MatrixUsers";
private String driver = "com.mysql.jdbc.Driver";
private String userNameDB = "root"; 
private String passwordDB = "password1"; 

private Connection connectMe = null;
private PreparedStatement selectUsers = null;
private Statement st;
private ResultSet results;


public void validateUserFromDB() {


    try {
        Class.forName("com.mysql.jdbc.Driver");
        connectMe = DriverManager.getConnection(url, userNameDB, passwordDB);
        String newUser = "Sil";
        String query = "SELECT count(*) FROM MatrixUsers.Users WHERE userNames = ?;";

        selectUsers = connectMe.prepareStatement(query);
        selectUsers.setString(1, newUser);
        results = selectUsers.executeQuery(query);

        //System.out.println(results);
        if(results.next()) {
            String count = null;
                    count= results.getString(1);

            if(count == null)
                System.out.println("NO MATCH");
            else
                System.out.println("MATCH");

        }

        connectMe.close();


    } catch (Exception e) {
        e.printStackTrace();
    }


}

The error is this:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '?' at line 1
4
  • Ddi you try the single-quote userName = '?' ; Commented Feb 12, 2015 at 13:22
  • Yes, if I do that I get this new error: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). Commented Feb 12, 2015 at 13:23
  • you need to remove the ; Commented Feb 12, 2015 at 13:25
  • I did and still same issue. Only way to move to another issue is using the ' ' Commented Feb 12, 2015 at 13:25

2 Answers 2

2

remove semicolon and replace line:

results = selectUsers.executeQuery(query);

by this

results = selectUsers.executeQuery();
Sign up to request clarification or add additional context in comments.

1 Comment

That did the job, thanks! Will select correct answer.
2

remove the ; after the question mark. and remove the query from results = selectUsers.executeQuery(query);

results = selectUsers.executeQuery(query); will execute the query without replacement of the placeholder. That is used for "static" queries.

results = selectUsers.executeQuery(); will execute the statement wich was prepared earlier.

2 Comments

I did and still same error. If I add the ' ', I get this: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
@Silvestrini single qoutes are not reded. remove them.

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.