2017-10-23 1 views
0

그래서 하나의 업데이트에 여러 행을 삽입하려고합니다. 일부 인터넷 검색을 한 후 rewriteBatch는 대부분의 사람들이 권고 한 것입니다.ExecuteBatch()가 업데이트되지 않습니다.

기본적으로 배치를 실행하려고하면 db가 업데이트되지 않습니다. db는 localhost이며 동일한 URL을 사용하여 db에서 읽을 수 있으며 예외 (Exception 또는 SQLException)가 없습니다.

public void insert(Node[] nodes) { 
    try { 
     conn = (Connection) DriverManager.getConnection(url); 
     System.out.println(conn.getMetaData().supportsBatchUpdates()); 
     conn.setAutoCommit(false); 
     for (int i = 0; i < nodes.length; i++) { 
      if(nodes[i].next!=null){ 
     pstmt=conn.prepareStatement(StatementUtils.createInsertStatement(i)); 
       addToBatch(nodes[i].next); 
       int [] res=pstmt.executeBatch(); 
       System.out.print("has "+res.length+" elements. Status:["); 
       for (int j = 0; j < res.length; j++) { 
        System.out.print(res[j]+" "); 
       } 
       System.out.println("]"); 
       conn.commit(); 
       pstmt.close(); 
      } 
     } 
    } 
    catch(Exception e){ 
     try { 
      conn.rollback(); 
     } catch (SQLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 
    finally { 
     if (conn != null) { 
     try { conn.close(); } 
     catch (SQLException e) { 
      printSQLException(e); 
      } 
     } 
    } 
} 

private void addToBatch(Node n) throws SQLException{ 
    if(n!=null){ 
     pstmt.setString(1, n.can.getCar()); 
     pstmt.setInt(2, n.can.getID()); 
     pstmt.setTimestamp(3, n.can.getDate()); 
     pstmt.setInt(4, n.can.getLength()); 
     for (int i = 1; i <= n.can.getLength(); i++) { 
      pstmt.setInt(i+4, n.can.getData(i-1)); 
     } 
     pstmt.addBatch(); 
     addToBatch(n.next); 
    } 
} 

public static String createInsertStatement(int length){ 
     String s="INSERT into CanData (carName,systemID,dataDate,size"; 
     for (int i = 0; i < length; i++) 
       s+=",d"+i; 
     s+= ")values(?,?,?,?"; 
     for (int i = 0; i <length; i++) 
       s+=",?"; 
     s+=")"; 
     return s; 
    } 

삽입 (for)이 있어도 테스트 목적으로 노드 배열에는 1 개의 위치 만 있습니다.

true 
has 18 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ] 
true 
has 14 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 ] 
true 
has 15 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ] 
true 
has 18 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ] 
true 
has 14 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 ] 
true 
has 11 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 ] 

왜 업데이트하지 않습니다/내 DB에 삽입 :

또한이 내 출력?

여기 양해 모두의 xD

+0

이 문제가 확실하지 않지만 커밋하기 전에 진술을 닫고 싶을 수 있습니다. – EvanM

+0

고마워요 내가 시도 할께요 –

+0

Nope =/ –

답변

0

먼저 주셔서 감사 때까지 읽고 Btw는 경우에 저를 돕기 위해 노력하고 검색하고 내 질문에 대답하기 위해 노력하고 시간을 투자 한 모든 사람에게 감사합니다.

문제는 URL 연결입니다. 그것은이었다

url="jdbc:sqlserver://localhost;rewriteBatchUpdates=true;instanceName=MSSQLSERVER;integratedSecurity=true;";

하지만 실종 그쪽으로 데이터베이스 이름 및 포트 번호가 있었다.

url = "jdbc:sqlserver://localhost:1433;database=IFS_DB;rewriteBatchUpdates=true;instanceName=MSSQLSERVER;integratedSecurity=true;"; 

이 또한 내가 URL을 게시하지 대한 죄송 : 당신이 당신과 같이, 데이터베이스 및 포트 번호를 정의해야 한 번에 여러 행을 삽입하려고 할 때 보인다. 내가 어쩌면이 시간 전에 수정되었을 수도 있습니다.

관련 문제