java - MySQLSyntaxErrorException: Error in SQL Syntax -
please have @ following code
public int insert(int index, string hashword) { string str=""; int result=0; int returnresult=0; try { con.setautocommit(false); preparedstatement ps = con.preparestatement("insert key_word (key,hashed_word) values(?,?)"); ps.setint(1, index); ps.setstring(2, hashword); result = ps.executeupdate(); con.commit(); if(result>0) { returnresult = 1; } else { returnresult = -1; system.out.println( index+" failed update"); } } catch(sqlexception s) { returnresult = -1; s.printstacktrace(); try { con.rollback(); system.out.println(index+" exception occured. update failed"); } catch (sqlexception ex) { ex.printstacktrace(); str = index+" exception occured. update failed. rollback failed"; } } return returnresult; }
when run code, gives below error.
com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'key,hashed_word) values(0,'001')' @ line 1 0 exception occured. update failed @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) operation broke completed @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:57) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) @ java.lang.reflect.constructor.newinstance(constructor.java:525) @ com.mysql.jdbc.util.handlenewinstance(util.java:411) @ com.mysql.jdbc.util.getinstance(util.java:386) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1054) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4237) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4169) @ com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:2617) @ com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:2778) @ com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2834) @ com.mysql.jdbc.preparedstatement.executeinternal(preparedstatement.java:2156) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2441) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2366) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2350) @ inglesrapido.databasehandler.insert(databasehandler.java:64) @ inglesrapido.keywordtablecreator.readandupdate(keywordtablecreator.java:53) @ inglesrapido.keywordtablecreator.<init>(keywordtablecreator.java:25) @ inglesrapido.main.main(main.java:26)
below mysql table
what wrong here?
key
reserved word, needs escape used in queries, in:
insert key_word (`key`,hashed_word) values(?,?)
it better choose column name, doesn't clash sql keyword.
Comments
Post a Comment