2013-08-23 4 views
1

내가 'MVN 바람둥이 일에 오류가 점점 오전 :. 실행은 "나는 점점 오전 오류 :준비된 문 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 코드는 다음과 같습니다. 나는 샘 사용한 각 필드에 대한 NULL NOT 위에서

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


     deviceDao.create(device); 
     return ""; 

MySQL의 테이블 내 코드에서와 동일한 열이 다른 기능을위한 전자 코드 및 거기 작동한다. 이 오류가 발생하는 이유는 무엇입니까? Pls. 도움.

+0

그냥 제안에 간다, – cameronjonesweb

+0

그것을 시도 물음표 인용 부호를 추가하려고 - 그것은 Eclipse에 의해 표시됩니다. – user2480526

답변

1

KEY은 Mysql에서 reserved word입니다. 따라서 열의 이름을 바꾸거나 (장기적으로 더 좋음) 또는 그 주위에 역 진드기를 사용하십시오.

당신이 문은 다음과 같아야 삽입 말했다되고 그건

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

같은 당신의 update 문

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'가있는 다른 위치가 있다는 것입니다. – peterm

+0

감사합니다. 당신은 생명을 구하십시오 :) – user2480526