1
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?

Vamshi
  • 510
  • 2
  • 10
  • 26
  • Maybe check this out if it applies: http://stackoverflow.com/questions/10797794/multiple-queries-executed-in-java-in-single-statement – demongolem Apr 17 '14 at 21:17

1 Answers1

1

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 PreparedStatements.

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().

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94