Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Batch consisting of multiple PreparedStatements

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way to create a single batch, with each line being generated by a PrepareStatememt, rather than manual string concatenation ?
I don't think it would do to use PreparedStatement.addBatch(), because this would enforce a uniform structure on all lines...

For example, suppose I'd like the following 4 lines to execute as a single batch. Do I have to concatenate them manually, or is there a way to use PreparedStatements (one PreparedStatement for "update", and one for "insert):

--------- start batch --------
- update TAB set salary=1000 where id=1
- update TAB set salary=2000 where id=2
- insert into TAB values(3,3000)
- insert into TAB values(4,4000)
--------- end batch ---------

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sol,
What do you mean by "manual string concatenation" ?
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sol,
Perhaps the following will be of help (assuming you haven't already seen it):

http://java.sun.com/docs/books/tutorial/jdbc/jdbc2dot0/batchupdates.html

Good Luck,
Avi.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.

What I meant was, I'd like to create a batch that consists of PreparedStatemtns.

It's *easy* to create a batch if you represent each line as a *stirng*:
batchStmt.addBatch("update TAB set salary=1000 where id=1");
batchStmt.addBatch("update TAB set salary=2000 where id=2");
batchStmt.addBatch("insert into TAB values(3000)");
batchStmt.addBatch("insert into TAB values(4000)");

But i'd like each line to be a preparedStatemet, which would look something like:


I know it can be done if all 4 lines have the exact same structure (e.g. if all 4 lines where "insert"), but I couldn't find a way to do it when there's mixed structure (e.g. 2 "inserts" and 2 "updates")...

Thanks
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sol,
You said:

I couldn't find a way to do it when there's mixed structure

That's because it doesn't exist! You can only associate precisely one SQL statement with a single instance of "PreparedStatement". Guess you'll have to find a workaround -- like maybe implementing this functionality yourself (probably by creating some "wrapper" class for "PreparedStatement").

Good Luck,
Avi.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic