繁体   English   中英

Java JDBC-准备使用合并排序进行批量插入的语句

[英]Java JDBC - prepared statement for bulk insertion using merge sort

我正在使用JDBC准备的语句进行批量插入。 我正在调用ps.execute()方法。 如果失败,那么我正在调用一种方法,其中我要传递参数列表和准备好的语句。 我正在使用merge sort technique to divide the list ,然后尝试插入记录,但没有成功。

这是我的代码;

从我正在调用的executePrepareStatement方法

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) {
            }
        }
    }

谢谢

我认为您使用合并排序的方式并不正确。 我了解您正在尝试使用分而治之的概念来实现您的解决方案,但是我认为您使问题变得比真正需要的更加困难( 并且更加令人困惑/复杂 )。

如果我理解正确,则您有一个要插入数据库的数据集。 您可能要批量进行。 PreparedStatement让我们通过使用几个简单的方法来做到这一点: addBatch()executeBatch()

这是我将如何尝试实现您的要求的概述:

  • 我要设置批次限制,即当我想执行该批次时,我的批次中的语句数。 除非达到此限制( 可以使用计数器很好地跟踪它 ),否则我将继续添加到批次中

  • 达到限制后,我将执行批处理,重置计数器,清除批处理并重新执行步骤1

  • 这将一直持续到我完成整个数据集为止。 最后,根据我的要求,我要么将数据提交到数据库,要么执行回滚。

请看一下此答案 ,以获取有关如何执行和实施此示例的示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM