jdbc 的executeupdate 之 oracle

本文介绍了Oracle JDBC中两种批量更新的方法:标准模型和Oracle特定模型,并提供了代码示例。标准模型适用于跨数据库和JDBC驱动程序的代码移植性较高的场景;而Oracle特定模型则更加高效,但仅支持PreparedStatement。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Oracle JDBC: Update Batching

Contrary to what the name suggests, batch updates or update batching allows invoking multiple DML operations including Insert, Update, Delete, in one database call, minimizing network roundtrips. Oracle JDBC supports two distinct models of update batching: the standard model and Oracle-specific model.
Standard Update Batching (explicit model)
In the standard update batching, you manually add operations to the batch and then explictly choose when to execute the batch. Recommended when code portability across different database and JDBC drivers is a higher priority than performance.
Oracle Update Batching (implicit and faster model)
Defines a batch value, the number of operations you want to process per round trip, which causes the statements to be automatically and implicitly processed as soon as that many operations have been added to the batch. Oracle update batching is usually faster than the standard update batching but only supports PreparedStatement.
Best Practices
Always disable auto-commit mode with 'Update Batching';
Use a batch size of around 10;
Don't mix the standard and Oracle models of 'Update Batching'.
Explicitly commit the transaction
Example of Standard Update Batching
The following example illustrates how you use the Standard JDBC 'Update Batching' feature. It assumes you have imported the oracle.driver.*interfaces.
//ds is a DataSource object
Connection conn = ds.getConnection();

//Always disable auto-commit when using update batching
conn.setAutoCommit(false);
Statement s = conn.createStatement();
s.addBatch("insert into dept values ('23', 'Sales', 'USA')");
s.addBatch("insert into dept values ('24', 'Blue Sky', 'Montana')");
s.addBatch("insert into dept values ('25', 'Applications', 'India')");
//Manually execute the bacth
s.executeBatch();
s.addBatch("insert into dept values ('26', 'HR', 'Mongolia')");
s.executeBatch();

conn.commit();
ps.close();
...


Example of Oracle Update Batching
The same example using the Oracle 'Update Batching' extension. It assumes you have imported the oracle.driver.*interfaces.
//ds is a DataSource object
Connection conn = ds.getConnection();

//Always disable auto-commit when using update batching
conn.setAutoCommit(false);
PreparedStatement ps =
conn.prepareStatement("insert into dept values (?, ?, ?)");
//Change batch size for this statement to 3
((OraclePreparedStatement)ps).setExecuteBatch (3);
//--------#1------------
ps.setInt(1, 23);
ps.setString(2, "Sales");
ps.setString(3, "USA");
ps.executeUpdate(); //JDBC queues this for later execution
//--------#2------------
ps.setInt(1, 24);
ps.setString(2, "Blue Sky");
ps.setString(3, "Montana");
ps.executeUpdate(); //JDBC queues this for later execution
//--------#3------------
ps.setInt(1, 25);
ps.setString(2, "Applications");
ps.setString(3, "India");
ps.executeUpdate(); //The queue size equals the batch value of 3
//JDBC sends the requests to the database
//--------#1------------
ps.setInt(1, 26);
ps.setString(2, "HR");
ps.setString(3, "Mongolia");
ps.executeUpdate(); //JDBC queues this for later execution

((OraclePreparedStatement)ps).sendBatch(); // JDBC sends the queued request
conn.commit();
ps.close();
...




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值