sql - Prepared Statement INSERT JDBC MySQL -
i getting error on doing ' mvn tomcat:run " . error getting is:
exception org.springframework.web.util.nestedservletexception: request processing failed; nested exception org.springframework.jdbc.badsqlgrammarexception: preparedstatementcallback; bad sql grammar [insert ibstechc_dev.device (key, ip_address, type, name) values (?, ?, ?, ?)]; nested exception com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'key, ip_address, type, name) values ('abcd', 'abcd', 1234, 'abcd')' @ line 1 root cause org.springframework.jdbc.badsqlgrammarexception: preparedstatementcallback; bad sql grammar [insert ibstechc_dev.device (key, ip_address, type, name) values (?, ?, ?, ?)]; nested exception com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'key, ip_address, type, name) values ('abcd', 'abcd', 1234, 'abcd')' @ line 1 org.springframework.jdbc.support.sqlerrorcodesqlexceptiontranslator.dotranslate(sqlerrorcodesqlexceptiontranslator.java:237) org.springframework.jdbc.support.abstractfallbacksqlexceptiontranslator.translate(abstractfallbacksqlexceptiontranslator.java:72
my code segment is:
list<device> devices = this.jdbctemplate.query( "select * xyz.device a,xyz.user_device b " + "where b.user_id = ? , a.device_id = b.device_id , " + "a.type = ?", new object[]{userid,type}, new rowmapper<device>() { public device maprow(resultset rs, int rownum) throws sqlexception { device device = new device(); device.setid(long.valueof(rs.getint(1))); device.setkey(rs.getstring(2)); device.setipaddress(rs.getstring(3)); device.settype(rs.getint(4)); device.setname(rs.getstring(5)); return device; } }); system.out.println("found user..." + userid); return devices; } public void create(device device) { this.jdbctemplate.update("insert xyz.device (key, ip_address, type, name) values (?, ?, ?, ?)", new object[]{device.getkey(), device.getipaddress(), device.gettype(), device.getname()}); } public void delete(device device) { this.jdbctemplate.update("delete xyz.device device_id = ?", new object[] {device.getid()}); } public void update(device device) { this.jdbctemplate.update( "update xyz.device set key = ?, ip_address = ?, type = ?, name =? device_id = ?", new object[]{device.getid(),device.getkey(), device.getipaddress(), device.gettype(), device.getname()});
and debug.java code is:
public string getnavbardata(){ device device = new device(); device.setkey("abcd"); device.setipaddress("abcd"); device.settype(1234); device.setname("abcd"); devicedao.create(device); return "";
the mysql table has same columns in code above not null each field. have used same code different functionality , works there. why getting error one? pls. help.
key
reserved word in mysql. therefore either rename column (which better in long run) or use ticks around it.
that being said insert statement should this
insert xyz.device (`key`, ip_address, type, name) values (?, ?, ?, ?) ^ ^
the same goes update statement
update xyz.device set `key` = ?, ip_address = ?, type = ?, name =? device_id = ? ^ ^
Comments
Post a Comment