1

I would like to achieve the above using jdbctemplate.

I have the following using jdbc:

Connection conn = DBUtilities.getSQLConnection(); 
// get a connection
Statement stmt = conn.createStatement(); 
//create statement
stmt.addBatch(sql1);
//first sql statement to be added
stmt.addBatch(sql2);
//second sql to be added
stmt.executeBatch();

ResultSet rs = stmt.executeQuery(sql3); 
//third sql that uses previous sql 1 and 2

How do I add multiple SQL (batch) with jdbctemplate?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Have you taken a look at the API? [`JdbcTemplate.batchUpdate(String... sql)`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#batchUpdate-java.lang.String...-) – Mark Rotteveel Oct 19 '16 at 12:10
  • Yes I have.. the problem is that it is not an update ,but multiple select statements that select into temporary tables... – user3844111 Oct 20 '16 at 14:37
  • AFAIK selects into a table are still considered updates, not selects. – Mark Rotteveel Oct 20 '16 at 15:12
  • Please elaborate your question a bit more about what you are trying to achieve with actual Sqls and See [this SO Question](http://stackoverflow.com/questions/7899543/can-preparedstatement-addbatch-be-used-for-select-queries). Running Selects in batch shouldn't be supported at very first place. – Sabir Khan Oct 21 '16 at 07:35

1 Answers1

0

The following code example illustrates how to execute 3 SQL update statements in a batch using the JdbcTemplate class:

String sql1 = "INSERT INTO Users (email, pass, name) VALUES ('email0', 'pass0', 'name0')";
String sql2 = "UPDATE Users SET password='default' WHERE user_id < 10";
String sql3 = "DELETE FROM Users WHERE email = ''";
     
int[] updateCounts = template.batchUpdate(sql1, sql2, sql3);

As you can see, the batchUpdate() method takes an array of SQL statements as arguments, and returns an array of integer numbers indicating the number of rows affected by each query.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Ankita
  • 1