2013-08-23 108 views
1

我做“MVN的Tomcat收到一个错误:运行”我得到的错误是:预处理语句INSERT JDBC的MySQL

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO ibstechc_dev.device (key, ip_address, type, name) VALUES (?, ?, ?, ?)]; nested exception is 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 'key, ip_address, type, name) VALUES ('abcd', 'abcd', 1234, 'abcd')' at line 1 

root cause 

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO ibstechc_dev.device (key, ip_address, type, name) VALUES (?, ?, ?, ?)]; nested exception is 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 'key, ip_address, type, name) VALUES ('abcd', 'abcd', 1234, 'abcd')' at line 1 
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:237) 
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72 

我的代码段:

List<Device> devices = this.jdbcTemplate.query(
      "select * from xyz.device a,xyz.user_device b " 
        + "where b.user_id = ? and a.device_id = b.device_id and " 
        + "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 for user..." + userId); 
     return devices; 
} 

public void create(Device device) { 
    this.jdbcTemplate.update("INSERT INTO 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 FROM xyz.device WHERE device_id = ?", new Object[] {device.getId()});  
     } 

     public void update(Device device) { 
      this.jdbcTemplate.update(
        "UPDATE xyz.device SET key = ?, ip_address = ?, type = ?, name =? WHERE device_id = ?", new Object[]{device.getId(),device.getKey(), device.getIPAddress(), device.getType(), device.getName()}); 

和我Debug.java代码:

public String getNavBarData(){ 
     Device device = new Device(); 
     device.setKey("abcd"); 
     device.setIPAddress("abcd"); 
     device.setType(1234); 
     device.setName("abcd"); 


     deviceDao.create(device); 
     return ""; 

MySQL表具有相同的列在我上面的代码与NOT NULL的每个字段我已经使用了SAM。 e代码为不同的功能,它在那里工作。为什么我得到这个错误? PLS。帮帮我。

+0

只是一个建议,尝试加入周围的问号 – cameronjonesweb

+0

试了一下引号 - 它由Eclipse标记。 – user2480526

回答

1

KEY是Mysql中的reserved word。因此,您要么重命名该列(长期来看哪个列更好),要么使用后面的勾号。

这就是说,你insert语句应该是这样的

INSERT INTO xyz.device (`key`, ip_address, type, name) VALUES (?, ?, ?, ?) 
         ^^

同样去你的更新语句

UPDATE xyz.device SET `key` = ?, ip_address = ?, type = ?, name =? WHERE device_id = ? 
        ^^
+0

Thx。试过了 - 同样的错误。 – user2480526

+0

@ user2480526它不能**相同的错误**。你确定你使用了反嘀字符而不是单引号吗?这里是[sqlfiddle](http://sqlfiddle.com/#!2/a7071)演示。取消注释最后一个插入并点击'Build schema',你会得到你的错误。或者另一种解释是,你在你的代码中有其他地方,你有'key'而没有back tick。 – peterm

+0

谢谢。你是一个救命的人:) – user2480526