Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of rows updated with PreparedStatement

How to get how many rows updated with PreparedStatement?

.getUpdateCount() returns 0.

For executeUpdate got error:
error occurred during batching: batch must be either executed or cleared

my code:

updTrans = dataSource.getConnection().prepareStatement("update...");
updTrans.setInt(1, Integer.valueOf(transaksjonstatusid));
...
updTrans.addBatch();
upd = updTrans.executeUpdate();
like image 475
sergionni Avatar asked Nov 30 '22 09:11

sergionni


2 Answers

You should be using PreparedStatement#executeBatch() when using batches.

...
updTrans.addBatch();
upd = updTrans.executeBatch();

It returns an int[] containing update counts of each batch.

like image 87
BalusC Avatar answered Dec 04 '22 10:12

BalusC


Did you try to use:

int n = preparedStatement.executeUpdate();

Here you can find some explanations on how to use a PreparedStatement.

like image 39
Fanny H. Avatar answered Dec 04 '22 11:12

Fanny H.