0

假设我们有这个片段:

dbConnection.setAutoCommit(false);//commit trasaction manually

String insertTableSQL = "INSERT INTO DBUSER"
            + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
            + "(?,?,?,?)";              
PreparedStatement = dbConnection.prepareStatement(insertTableSQL);

for(int i=0;i<5000;i++){
    preparedStatement.setInt(1, 101);
    preparedStatement.setString(2, "im_a_new_user");
    preparedStatement.setString(3, "admin");
    preparedStatement.setTimestamp(4, "00:00:00.000");
    preparedStatement.addBatch();
}

preparedStatement.executeBatch();

dbConnection.commit();

它基本上添加了 5000 个批量插入PreparedStatement并执行它们。

现在,我有一个带有 的 GUI JProgressBar,我想PreparedStatementexecuteBatch()通话后收听,以了解已执行了多少查询并更新进度条。我已经有了更新进度条的代码,但我不知道如何收听PreparedStatement,可以这样做吗?

4

1 回答 1

3

您可以将整个数据分成大块并单独处理每个数据,然后更新进度条。

private int BULKSIZE = 100; // define your own bulk size

for(int j = 0; j < 5000; j += BULKSIZE)
{
    for(int i = 0; i < BULKSIZE; ++i) 
    {
         preparedStatement.setInt(1, 101);
         preparedStatement.setString(2, "im_a_new_user");
         preparedStatement.setString(3, "admin");
         preparedStatement.setTimestamp(4, "00:00:00.000");
         preparedStatement.addBatch();
    }
    preparedStatement.executeBatch();
    updateProgressBar();
}
于 2013-06-21T08:55:24.963 回答