user3891813
user3891813

Reputation: 1

mysql executeBatch() error..?

In the MySQL environment, I use jdbc addBatch() and executeBatch() When you run a batch update, the entire query does not run, Only the last query will run normally.

I do not know what to check.

My source code

ps = conn.prepareStatement ("update TEMP set LAST_UPDATE_DT = SYSDATE () where id = '1001'");
ps.addBatch ();

ps = conn.prepareStatement ("update TEMP set LAST_UPDATE_DT = SYSDATE () where id = '1002'");
ps.addBatch ();

ps = conn.prepareStatement ("update TEMP set LAST_UPDATE_DT = SYSDATE () where id = '1003'");
ps.addBatch ();

Int result [] = ps.executeBatch ();

Upvotes: 0

Views: 332

Answers (1)

e4c5
e4c5

Reputation: 53734

This is mostly incorrect use of prepared statements. You probably had simple statements in mind when you wrote this. For prepared statement batch, the approach is slightly different

// first create the prepared statement
ps = conn.prepareStatement ("update TEMP set LAST_UPDATE_DT = SYSDATE () where id = ?");
// then bind to it.
ps.setInt(1,1001);
ps.addBatch();

// then bind again
ps.setInt(1,1002);
ps.addBatch();

// and again
ps.setInt(1,1003);
ps.addBatch();

int result [] = ps.executeBatch ();

Upvotes: 1

Related Questions