2015-12-30 179 views
0

大家好,我正在嘗試比較用戶輸入ID和數據庫ID,但是當我執行查詢時,我得到語法錯誤我在做什麼錯誤?Sql查詢中出現語法錯誤

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); 
    } 

錯誤消息

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) 
+0

有點牽強,但什麼如果用'FROM FROM'替換「FROM FROM individual」,你會得到結果嗎?個人「'? – npinti

+0

此代碼不會拋出此異常。您不會在任何地方調用'executeQuery()',如堆棧跟蹤所示。 – EJP

回答

4
+ "FROM individual" + "WHERE individ 

將被解析爲

FROM individualWHERE individ... 

添加空格您的字符串,你應該罰款