MySQL 在 Java 中使用 LIMIT 命令和 Prepared Statement 时出现语法错误

MySQL syntax error using LIMIT command with Prepared Statement in Java

我正在 Java 中编写代码,每次我 运行 这段代码我都想从 MySQL table.The 第二次 [=] 的下一行开始21=]这个代码就是这个

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接受整数参数,所以你应该使用ints,而不是Strings:

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

我能够在没有准备好的语句的情况下做到这一点

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

得到第一行作为输出。