2014-02-14 3 views
1

저는 데이터베이스에 연결하는 간단한 연습을했습니다. 하나의 배치 객체를로드하고 다른 하나의 Order 객체를로드하는 두 가지 메소드가 있습니다. 매우 구조적으로 유사한 방법 아직 주문과 하나가 뱉어 :두 가지 유사한 메소드, 다른 동작 setSchema는 두 가지 모두에서 실패하지 않지만 두 번째 경우에서는 MySQL 예외가 발생합니까?

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 'order (order_id,order_no,person_id) VALUES(13,2003,3)' at line 1 

방법 2의 준비된 문 자체가 매우 간단 해집니다 - 나는 무엇이 잘못되었는지를 알아 내기 위해 노력한다. PreparedStatement의 "order"앞에 스키마 이름을 추가하면 문제가 해결됩니다. 그러나 나는 그것에 만족하지 않는다. 어딘가에 스키마 이름이 ps이며 어딘가에 setSchema이 분명히 나쁘다.

질문은 왜 setSchema이 처음에는 그러한 예외가없고 예상대로 실행될 때 두 번째 방법으로 작동하지 않는 이유입니까? 나는 디버깅을했고 연결 인스턴스에 대한 데이터베이스 필드 쇼는 내 스키마의 올바른 이름 인 "ordermanagement"를 보여줍니다.

마지막으로 연결을 다시 사용하지 않도록 별도로 테스트를 실행했습니다. (예외를 throw) (작동, 아무 문제)

방법 1

public int[] personBatchInsert(List<Person> personList) throws SQLException { 
     int[] insertStatuses = null; 

     try { 
      obtainedConnection.setSchema("ordermanagement"); 
      ps = obtainedConnection 
        .prepareStatement("INSERT INTO person (person_id,last_name,first_name, street,city) VALUES(?,?,?,?,?)"); 
      obtainedConnection.setAutoCommit(false); 
      for (Person person : personList) { 

       ps.setLong(1, person.getPersonId()); 
       ps.setString(2, person.getLastName()); 
       ps.setString(3, person.getName()); 
       ps.setString(4, person.getStreet()); 
       ps.setString(5, person.getCity()); 
       ps.addBatch(); 

      } 

      insertStatuses = ps.executeBatch(); 
      obtainedConnection.commit(); 

방법 2

public int[] orderBatchInsert(List<Order> orderList) throws SQLException { 
    int[] insertStatuses = null; 

    try { 
     obtainedConnection.setSchema("ordermanagement"); 
     ps = obtainedConnection 
       .prepareStatement("INSERT INTO order (order_id,order_no,person_id) VALUES(13,2003,3)"); 
     obtainedConnection.setAutoCommit(false); 

     ps.execute(); // here exception is triggered 
     obtainedConnection.commit(); 

enter image description here

내가 정말 어딘가 오타되기를 기대하지만, 그냥 그것을 보지 못한다.

+0

관리자로 연결 하시겠습니까? 적절한 연결 문자열을 사용하여 연결하면 스키마를 설정할 필요가 없습니다. –

+3

'ORDER'는 키워드이며'ORDER BY'의 일부이며 따옴표로 묶어야합니다. –

+0

내가 그것을 시도하자, 오 세상에 나는 이미 그 해결책의 느낌을 가지고있다! – Aubergine

답변

2

ORDERORDER BY의 일부이며 따옴표로 묶어야합니다.

INSERT INTO `order` (order_id,order_no,person_id) VALUES(13,2003,3)