简体   繁体   English

使用LIMIT命令和Java中的Prepared Statement进行MySQL语法错误

[英]MySQL syntax error using LIMIT command with Prepared Statement in Java

I am writing code in Java and I want to take every time I run this code the next line from a MySQL table.The second time I run this code is this. 我正在用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);

But it shows me this error : 但它告诉我这个错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1','2'' at line 1 检查与MySQL服务器版本对应的手册,以便在第1行的“1”,“2”附近使用正确的语法

limit accepts integer parameters, so you should use int s, not String s: limit接受整数参数,所以你应该使用int s,而不是String s:

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

I was able to do it without prepared statement 我没有准备好的陈述就能做到

int i = 0;
    int j = 1;
    sql = "Select SURNAME ,KATHGORIA, AFM , NAME FROM EMPLOYEE limit "+i+ ","+j;

got first row as output. 获得第一行作为输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM