简体   繁体   English

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

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

I am using JDBC prepared statement for bulk insertion. 我正在使用JDBC准备的语句进行批量插入。 I am calling ps.execute() method. 我正在调用ps.execute()方法。 If it fails then i am calling a method where i am passing the list of parameters and the prepared statement. 如果失败,那么我正在调用一种方法,其中我要传递参数列表和准备好的语句。 I am using merge sort technique to divide the list and then trying to insert the records, but not successfull. 我正在使用merge sort technique to divide the list ,然后尝试插入记录,但没有成功。

Here is my code; 这是我的代码;

from executePrepareStatement method i am calling 从我正在调用的executePrepareStatement方法

this.executeInsertStatement(query, myCollection, 0, myCollection.size());

// executeInsertStatement method // 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) {
            }
        }
    }

Thanks 谢谢

I don't think you're on the right track by using a merge sort. 我认为您使用合并排序的方式并不正确。 I understand that you're trying to achive your solution using the concept of divide and conquer but I think you're making the problem harder ( and more confusing/complex ) than it really needs to be. 我了解您正在尝试使用分而治之的概念来实现您的解决方案,但是我认为您使问题变得比真正需要的更加困难( 并且更加令人困惑/复杂 )。

If I understand correctly, you have got a data set that you would want to insert into your database. 如果我理解正确,则您有一个要插入数据库的数据集。 And you would want to do it in bulk. 您可能要批量进行。 PreparedStatement let's you do that by using a couple of neat methods: addBatch() and executeBatch() PreparedStatement让我们通过使用几个简单的方法来做到这一点: addBatch()executeBatch()

Here's an outline of how I would have tried to implement your requirement: 这是我将如何尝试实现您的要求的概述:

  • I would set a batch limit ie the number of statements in my batch when I would like to execute the batch. 我要设置批次限制,即当我想执行该批次时,我的批次中的语句数。 Unless I reach this limit ( which I can very well track by using a counter ), I would continue to add to the batch 除非达到此限制( 可以使用计数器很好地跟踪它 ),否则我将继续添加到批次中

  • Once I hit the limit, I execute the batch, reset the counter, clear the batch and redo Step #1 达到限制后,我将执行批处理,重置计数器,清除批处理并重新执行步骤1

  • This would continue till I'm done with my entire data set. 这将一直持续到我完成整个数据集为止。 Finally I would end by either committing the data to the database or even performing a rollback, based on my requirement. 最后,根据我的要求,我要么将数据提交到数据库,要么执行回滚。

Have a look at this answer for an example of how you might go about and implement this. 请看一下此答案 ,以获取有关如何执行和实施此示例的示例。

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

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