Reputation: 520
String insert1 = "INSERT INTO Table1(Col1, col2, col3)"
+ "VALUES(?,?,?)";
String insert2 = "INSERT INTO Table2(Colx, coly)"
+ "VALUES(?,?)";
Connection conn = aConn;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(insert1);
// ps.addBatch(insert2);
I'm trying to Insert Data into multiple tables at a time, and it seems like addBatch(String sql)
is not defined for PreparedStatement.
Is there any alternate way?
Upvotes: 1
Views: 960
Reputation: 9816
First of all, a PreparedStatement
is used to cache a single SQL statement. This has the advantage that the driver/database might optimize the statement since it is expecting many of them and since it is a parameterized statement. If you want to use it for two different SQL statements you need two PreparedStatement
s.
In order to add rows to the statement you need to set your parameters using set*(1,...)
, set*(2,...)
, set*(3,...)
, etc. and then call addBatch()
(no arguments!). Finally you submit the batch of statements using executeBatch()
.
Upvotes: 1