[英]Listening to batch queries with PreparedStatement in Java
Let's say we have this snippet: 假设我们有以下代码段:
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();
It basically adds 5000 batch inserts to the PreparedStatement
and executes them. 它基本上将5000个批处理插入添加到PreparedStatement
并执行它们。
Now, i have a GUI with a JProgressBar
, and I would like to listen to the PreparedStatement
after the executeBatch()
call, in order to know how many queries have been executed and update the progress bar. 现在,我有一个带有JProgressBar
的GUI,我想在executeBatch()
调用之后侦听PreparedStatement
,以便知道已执行了多少查询并更新了进度条。 I already have the code to update the progressbar but i don't know how to listen to PreparedStatement
, is it possible to do that? 我已经有更新进度条的代码,但是我不知道如何听PreparedStatement
,有可能做到这一点吗?
You can divide whole data into bulks and treat each of them individually and update the progress bar afterwards. 您可以将整个数据划分为多个数据块,然后分别处理每个数据块,然后再更新进度条。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.