그래서 하나의 업데이트에 여러 행을 삽입하려고합니다. 일부 인터넷 검색을 한 후 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
이 문제가 확실하지 않지만 커밋하기 전에 진술을 닫고 싶을 수 있습니다. – EvanM
고마워요 내가 시도 할께요 –
Nope =/ –