温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JDBC中的批处理是什么

发布时间:2021-11-19 16:19:11 来源:亿速云 阅读:138 作者:小新 栏目:数据库

这篇文章将为大家详细讲解有关JDBC中的批处理是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

直接上代码:

package
cn.zhou.epet.test;
import
java.sql.*;
public
class Addbatch {public
static void main(String[] args) {Connection connection =
null;PreparedStatement stmt =
null;try
{//
加载数据库相关驱动Class.forName("oracle.jdbc.driver.OracleDriver");//
连接到数据库connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:bdqn",
"epet", "admin");stmt =
connection.prepareStatement("insert
into master values(master_seq.nextval,?,?,?)");
connection.setAutoCommit(false);//
Transaction Begin,让程序不自动提交事务stmt.setString(1,
"QQ");stmt.setString(2,
"RR");stmt.setInt(3,
1);stmt.addBatch();//
添加批处理1
stmt.setString(1,
"WW");stmt.setString(2,
"EE");stmt.setInt(3,
1);stmt.addBatch();//
添加批处理2
stmt.setString(1,
"VV");stmt.setString(2,
"AA");stmt.setInt(3,
1);stmt.addBatch();//
添加批处理3stmt.executeBatch();//
执行批处理
connection.commit();//
提交事务
connection.setAutoCommit(true);//
Transaction End,让程序自动提交事务(默认)System.out.println("dd");} catch
(ClassNotFoundException e) {e.printStackTrace();System.out.println("未能成功加载驱动类!");} catch
(SQLException e) {e.printStackTrace();System.out.println("执行SQL语句是出现异常!");try
{if
(connection != null) {connection.rollback();//
事务回滚connection.setAutoCommit(true);//
让程序自动提交事务(默认)}} catch
(SQLException e1) {e1.printStackTrace();}} catch
(Exception e) {e.printStackTrace();System.out.println("其他异常!");}
finally {try
{if (stmt
!= null) {stmt.close();stmt =
null;}if
(connection != null) {connection.close();connection =
null;}} catch
(SQLException e2) {System.out.println("关闭数据库时出现异常!");}}}
}JDBC中的批处理是什么

关于“JDBC中的批处理是什么”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI