sql = "select milk_rate from special_milk_rate
where code_producer_id=? and effective_from <= ?
and effective_till >= ?"
在我的 JDBC 代码中,我在这个查询中遇到错误
Exception: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 '? and effective_from <= ? and effective_till >= ?' at line 1
但是当我在 MySQL 命令提示符下使用一些值运行它时,它在那里运行良好。
最佳答案
ResultSet rst= prest.executeQuery(sql);
这是不正确的。它应该是 executeQuery()。
当使用语句时,sql 被传递来执行。当使用绑定(bind)变量时,它会被传递给 PreparedStatement。由于您使用的是 Statement API,因此 JDBC 正在执行它而无需替换您的绑定(bind)变量并想知道如何处理文字问号。
关于java - JDBC 查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4772723/