0

Hello everyone i am trying to compare a user input id with a db id but when i execute the query i get syntax error what am i doing wrong?

 sql = "SELECT individual.id " 
                + "FROM individual"
                + "WHERE individual.id = ? ";
       PreparedStatement pst = con.prepareStatement(sql);
       pst.setInt(1, input);

    while(rs.next()){
        int Iid = rs.getInt("id");
        System.out.println(Iid);
    }

Error message

Exception in thread "main" 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 '.id = ?' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2738)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1617)
2
  • A bit far fetched, but what do you get if you replace "FROM individual" with "FROM individual "? Commented Dec 30, 2015 at 9:19
  • This code does not throw this exception. You aren't calling executeQuery() anywhere, as shown in the stack trace. Commented Dec 30, 2015 at 10:06

1 Answer 1

4
+ "FROM individual" + "WHERE individ

Will be resolved to

  FROM individualWHERE individ...

Add spaces to your string and you should be fine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.