假设我们有这个片段:
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();
它基本上会向PreparedStatement
添加5000个批量插入并执行它们。
现在,我有一个JProgressBar
的GUI,我希望在PreparedStatement
调用后收听executeBatch()
,以便了解已执行的查询数量和更新进度条。我已经有了更新进度条的代码,但我不知道如何听PreparedStatement
,是否可以这样做?
答案 0 :(得分: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();
}