使用LIMIT命令和Java中的Prepared Statement的MySQL语法错误
作者:互联网
我正在用Java编写代码,我想每次在MySQL表的下一行运行此代码,第二次运行此代码.
String timh1 = "1";
String timh2 = "2";
PreparedStatement st = null;
String sqlGrammes = "SELECT SURNAME ,KATHGORIA, AFM , NAME FROM EMPLOYEE LIMIT ?,? ";
try {
st = connection.prepareStatement(sqlGrammes);
st.setString(1, timh1);
st.setString(2, timh2);
但这告诉我这个错误:
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 ”1′,’2” at line 1
解决方法:
limit接受整数参数,因此您应该使用整数,而不是字符串:
int timh1 = 1;
int timh2 = 2;
PreparedStatement st = null;
String sqlGrammes = "SELECT SURNAME ,KATHGORIA, AFM , NAME FROM EMPLOYEE LIMIT ?,? ";
try {
st = connection.prepareStatement(sqlGrammes);
st.setInt(1, timh1); // notice the setInt
st.setInt(2, timh2); // here too
标签:sql-limit,jdbc,prepared-statement,java,mysql 来源: https://codeday.me/bug/20191028/1949916.html