MySQL syntax error using LIMIT command with Prepared Statement in Java

I am writing Java code and I want that every time I run this code the next row is from the MySQL table. The second time I run this code.

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 this shows me this error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual corresponding to your MySQL server version for the correct syntax to use next to "1", "2" on line 1

+3


source to share


2 answers


limit

takes integer parameters, so you must use int

s and not 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

      

+3


source


I was able to do this without a prepared statement

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

      



got the first line as output.

0


source







All Articles