TechChain
TechChain

Reputation: 8952

Insert data into multiple tables in single transaction in jdbc

I have a table Order which has fields Orderid|status|cid|

and another table Order_Details

Oder_details_id|Order_id|Item id

I want to insert values in both the table in single transaction like if a customer place an order so the an order id should be generated and that order_id should be inserted into Order and Order_Details table. But how do i do in JDBC??

Upvotes: 0

Views: 3193

Answers (1)

Ashot Karakhanyan
Ashot Karakhanyan

Reputation: 2830

You can do it setting autocommit to false:

dbConnection.setAutoCommit(false); // to start a transaction block.

// do your two inserts here

dbConnection.commit(); // to end a transaction block.

Take a look here for full example: JDBC Transaction example

Upvotes: 3

Related Questions