我正在使用 JDBC 准备语句进行批量插入。我正在调用ps.execute()
方法。如果失败,那么我将调用一个方法,在该方法中传递参数列表和准备好的语句。我正在使用merge sort technique to divide the list
然后尝试插入记录,但没有成功。
这是我的代码;
从我正在调用的 executePrepareStatement 方法
this.executeInsertStatement(query, myCollection, 0, myCollection.size());
// 执行插入语句方法
public void executeInsertStatement1(String query, List myCollection, int sIndx, int eIndx) throws DBException,SQLException {
int startIndx = sIndx, endIndx = eIndx, mid = 0;
try {
try{
if(conn.isClosed())
new DataService(CoreConstants.TARGET);
} catch (Exception e) { }
if(startIndx >= endIndx) {
return;
}
conn.setAutoCommit(false);
if (query != null) {
mid = (startIndx + endIndx) / 2;
ps = conn.prepareStatement(query);
executeInsertStatement(query, myCollection, startIndx, mid);
executeInsertStatement(query, myCollection, mid+1, endIndx);
//int end_low = mid;
//int start_high = mid + 1;
if(mid < endIndx)
endIndx = mid;
for (int i = 0; i < endIndx; i++) {
List list = (List) myCollection.get(i);
int count = 1;
for (int j = 0; j < list.size(); j++) {
if(list.get(j) instanceof Timestamp) {
ps.setTimestamp(count, (Timestamp) list.get(j));
} else if(list.get(j) instanceof java.lang.Character) {
ps.setString(count, String.valueOf(list.get(j)));
}
else {
ps.setObject(count, list.get(j));
}
count++;
}
try {
ps.execute();
} catch (Exception e) {
rollback();
}
}
}
} catch (Exception e) {
rollback();
} finally{
try {
if (ps != null) {
ps.close();
ps = null;
}
} catch (Exception ex) {
}
}
}
谢谢