2

Now I have to update this into my database, the problem is I don't know how to update it multiple times in the database.

public void generate() { KpiMsg804 upd = createKpiMsg804(); try { st = conn.createStatement(); System.out.println("Updating Values"); //Updating Values into DB String query = "insert into msg_new_to_bde(" + "tablename,action,keyinfo1,keyinfo2) values(?, ?, ?, ?)"; pstmt = conn.prepareStatement(query); // create a statement pstmt.setString(1,upd.getTableName()); pstmt.setInt(2,upd.getAction()); // set value of staus of action pstmt.setString(3,upd.getKeyInfo1()); // set keyinfo value1 pstmt.setString(4,upd.getKeyInfo2()); // set keyinfo value2 int rows = pstmt.executeUpdate(); System.out.println("Number of Rows Updated" +rows); // execute insert statement } catch (SQLException e) { e.printStackTrace(); } 
3
  • 2
    Do you need to execute the same INSERT statement with different values several times? Commented Dec 3, 2012 at 9:47
  • 2
    What do you mean by multiple updates? you are inserting a single record each time, so it just inserts one record each time. Commented Dec 3, 2012 at 9:50
  • Yes I need to do insert multiple times and change this status to another after doing a calculation, but first I have to insert multiple rows. Commented Dec 3, 2012 at 10:11

1 Answer 1

0

You probably want to use the JDBC addBatch() and executeBatch() functionality: JDBC insert multiple rows . This will let you queue up your multiple rows, then insert them in one group.

The other interpretation of your question is: you need to turn the autocommit off, then use the JDBC begin() and commit() functionality to insert into several different tables, and they all go in at the same time.

pstmt.executeUpdate(Table1); pstmt.executeUpdate(Table2); conn.commit(); 

This will insert all the rows as one transaction, that is all at once.

Sign up to request clarification or add additional context in comments.

2 Comments

This is not how you use batched statements. executeUpdate() is only called once per batch, not once for each insert. Additionally there is no Connection.begin() in JDBC
executeUpdate() needs to be execute once for each table in the group of inserts. I removed the (implicity) conn.begin().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.