0

When I try to run the code below I am getting:

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`

String query="Select * from DB.Admin where username = ?";
PreparedStatement st=connection.prepareStatement(query);
st.setString(1,request.getParameter("loginid"));
ResultSet rst= st.executeQuery(query);
int count=0;
while(rst.next()){
 count++;
}

Please help me in this.

nyname00
2,5862 gold badges25 silver badges25 bronze badges
asked Apr 12, 2016 at 21:02

1 Answer 1

1

You will have to remove the query argument from your executeQuery call. If you provide the parameter, the query will be executed without binding any values (see Statement for details) - this is why the syntax (i.e. the ?) is invalid.

Execute the query like this:

ResultSet rst = st.executeQuery();

As a side note: you should always wrap Connection, PreparedStatement and ResultSet with a try-with-resources block, e.g.

try (ResultSet rst = st.executeQuery()) {
 // read the results
}

This way you can be sure the ResultSet will be closed no matter what happens.

answered Apr 12, 2016 at 21:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.