JDBC中的批处理是什么

这篇文章将为大家详细讲解有关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中的批处理是什么”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。