JDBC MySql bind variable syntax error in where clause

后端 未结 3 807
无人共我
无人共我 2021-01-24 02:22

I am getting this error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server

相关标签:
3条回答
  • 2021-01-24 02:59

    Change

    ResultSet rs = stmt.executeQuery(sql);
    

    to

    ResultSet rs = stmt.executeQuery();
    
    0 讨论(0)
  • 2021-01-24 03:15

    Don't use executeQuery(String) with prepared statements...

    Instead of....

    ResultSet rs = stmt.executeQuery(sql);
    

    use...

    ResultSet rs = stmt.executeQuery();
    

    Take a look at How to use Prepared Statements for more details

    0 讨论(0)
  • 2021-01-24 03:16

    If I understand your question, the problem is you used Statement.executeQuery(String). I'm fairly certain you meant to use PreparedStatement.executeQuery(),

    // Let us select all the records and display them.
    ResultSet rs = stmt.executeQuery(sql);   // <-- adding sql here makes it use the
                                             //     Statement version.
    

    You wanted to use

    // Let us select all the records and display them.
    ResultSet rs = stmt.executeQuery();      // <-- use the version from PreparedStatement
    
    0 讨论(0)
提交回复
热议问题