简体   繁体   English

在jdbc中的单个事务中将数据插入到多个表中

[英]Insert data into multiple tables in single transaction in jdbc

I have a table Order which has fields Orderid|status|cid| 我有一个表Order ,其中包含字段Orderid | status | cid |

and another table Order_Details 和另一个表Order_Details

Oder_details_id|Order_id|Item id Oder_details_id | Order_id |项目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. 我想在单个事务中的表中插入值,就像客户下订单一样,因此应该生成订单ID,并且order_id应该插入Order和Order_Details表中。 But how do i do in JDBC?? 但是我如何在JDBC中做?

You can do it setting autocommit to false : 您可以将autocommit设置为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 看一下这里的完整示例: JDBC Transaction示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有FK关系的单笔交易插入多个表 - Insert into multiple tables in single transaction with FK relationship 如何使用事务在多个表中插入数据 - how to insert data in multiple tables using transaction 在JAVA中使用JDBC在单个查询中将多行插入到两个表中 - Insert multiple rows into two tables in a single query using JDBC in JAVA 在单个查询中将数据插入多个表 - Insert data to multiple tables in single query 将多个表中的数据插入单个表中 - Insert Data from multiple tables into a single table PreparedStatement:如何使用JDBC将数据插入多个表 - PreparedStatement: How to insert data into multiple tables using JDBC 数据供应过程从多个表插入单个表 - Data provisioning process Insert from multiple tables in a single table 如何在 SQL 服务器中使用单个查询在多个表中插入数据? - How to insert data in multiple tables using single query in SQL Server? 在多个表中插入数据 - insert data in multiple tables 使用单个sql查询如何在未连接的三个表中插入数据。 或者我们可以将数据插入未连接的多个表中 - using single sql query how to insert data in three tables which are not joined . or can we insert data into multiple tables which are not joined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM