Java JDBC-使用合并排序为批量插入准备的语句
我使用JDBC准备语句进行批量插入。我正在调用Java JDBC-使用合并排序为批量插入准备的语句,java,jdbc,prepared-statement,mergesort,Java,Jdbc,Prepared Statement,Mergesort,我使用JDBC准备语句进行批量插入。我正在调用ps.execute()方法。如果失败,则调用一个方法,在该方法中传递参数列表和准备好的语句。我正在使用合并排序技术分割列表,然后尝试插入记录,但没有成功 这是我的密码 我正在调用ExecutePreparement方法 this.executeInsertStatement(query, myCollection, 0, myCollection.size()); //executeInsertStatement方法 public void ex
ps.execute()
方法。如果失败,则调用一个方法,在该方法中传递参数列表和准备好的语句。我正在使用合并排序技术分割列表
,然后尝试插入记录,但没有成功
这是我的密码
我正在调用ExecutePreparement方法
this.executeInsertStatement(query, myCollection, 0, myCollection.size());
//executeInsertStatement方法
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) {
}
}
}
public void executeInsertStatement1(字符串查询、列表myCollection、int-sIndx、int-eIndx)抛出DBException、SQLException{
int startIndx=sIndx,endIndx=eIndx,mid=0;
试一试{
试一试{
如果(连接已关闭())
新的数据服务(CoreConstants.TARGET);
}捕获(例外e){}
如果(startIndx>=endIndx){
返回;
}
连接设置自动提交(错误);
if(查询!=null){
mid=(startIndx+endIndx)/2;
ps=conn.prepareStatement(查询);
executeInsertStatement(查询、myCollection、startIndx、mid);
executeInsertStatement(查询、myCollection、mid+1、endIndx);
//int end_低=中;
//int start_high=mid+1;
如果(中间
谢谢我认为您使用合并排序的方法不正确。我知道你试图用分而治之的概念来解决问题,但我认为你让问题变得比实际需要的更难(也更混乱/复杂) 如果我理解正确,您已经有了一个要插入数据库的数据集。你会想批量做的。PreparedStatement让我们使用两种简洁的方法来实现这一点:和 以下是我将如何尝试实现您的要求的概要:
- 我会设置一个批处理限制,即当我想执行批处理时,批处理中语句的数量。除非我达到这个极限(我可以使用计数器很好地跟踪),否则我将继续添加到批中
- 一旦达到限制,我执行批处理、重置计数器、清除批处理并重做步骤1
- 这将一直持续到我完成我的整个数据集。最后,我会根据自己的需求,将数据提交到数据库,甚至执行回滚
请举例说明如何执行此操作。能否发布异常stacktrace?您是否调试了代码?我看到几个catch块没有代码来处理异常。这是一个非常糟糕的做法,你应该不惜一切代价避免。你能详细说明你想做什么吗?你得到了什么样的例外?你从哪里得到的?您是否收到stackoverflow异常?对我来说,您似乎永远调用executeInsertStatement1(…)方法,因为我看不到停止递归的条件。感谢您的回复,当startIndx>=endIndx时,递归应该结束。当preparestatement失败时,我试图实现合并排序技术。mycollectionlist是包含要插入的值的列表集合。我不知道预处理语句执行失败的位置,所以我尝试划分列表并尝试插入记录。假设我有1000条记录,400条执行失败。我传递整个列表,使用合并排序技术将其分割并插入。我会说重新开始。甚至不要先用代码来写,先在一张纸上用伪代码来写算法。然后实施它。你那里的东西简直糟透了(请原谅我的语言)。不要再把自己和合并排序混淆了。这不是关于排序,而是关于将一个范围拆分为子范围。关注您试图解决的问题,而不是与此无关的任意算法。感谢您的回答,sujay,我们正在将数据从sql迁移到Oracle server。我们创建的中间txt文件中的一些数据包含重复,因为有些数据是硬编码的。由于约束,我的批处理执行失败。在这个场景中,我尝试构建分而治之的方法。当前,如果批量插入失败,我将插入每个记录并调用提交(顺序插入)。如果有任何投入,将会有很大帮助。Thanks@user1103504:您可以扩展上述解决方案。其思想是分块执行批处理更新。假设你有100行,你把它分成10块。当您执行
execut时,您会看到