MySQL syntax error when executing SQL query

前端 未结 1 450
不思量自难忘°
不思量自难忘° 2020-12-12 06:32

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 f

相关标签:
1条回答
  • 2020-12-12 06:57

    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.

    0 讨论(0)
提交回复
热议问题