JDBC编程之事务处理
来源:互联网 发布:java agent premain 编辑:程序博客网 时间:2024/06/05 23:55
当插入数据遇到唯一性约束时,数据插入失败,如下列操作,插入tbl_user成功,但是插入tbl_address失败,因为id为1已经存在。
package com.djx.jdbc;import java.sql.DriverManager;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;public class TransactionTest {public static Connection getConnection() {Connection conn=null;try {Class.forName("com.mysql.jdbc.Driver");conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db?autoReconnect=true&useSSL=false","root","1234");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void insertTbUserData() {Connection conn=getConnection();String sql="insert into tbl_user(id,name,password,email) values(10,'Tom','1234','tom@163.com')";int count;try {Statement statement=(Statement)conn.createStatement();count = statement.executeUpdate(sql);System.out.println("向用户表中插入"+count+"条记录"); statement.close(); conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void insertTbAddressData() {Connection conn=getConnection();String sql="insert into tbl_address(id,city,country,user_id) values(1,'深圳','中国',10)";int count;try {Statement statement=(Statement)conn.createStatement();count = statement.executeUpdate(sql);System.out.println("向用户表中插入"+count+"条记录"); statement.close(); conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {// TODO Auto-generated method stubinsertTbUserData();insertTbAddressData();}}
利用事务回滚就可以防止错误的插入,如下代码
package com.djx.jdbc;import java.sql.DriverManager;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;public class TransactionTest {public static Connection getConnection() {Connection conn=null;try {Class.forName("com.mysql.jdbc.Driver");conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db?autoReconnect=true&useSSL=false","root","1234");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void insertTbUserData(Connection conn) throws SQLException {String sql="insert into tbl_user(id,name,password,email) values(10,'Tom','1234','tom@163.com')";int count;Statement statement=(Statement)conn.createStatement();count = statement.executeUpdate(sql);System.out.println("向用户表中插入"+count+"条记录"); statement.close();}public static void insertTbAddressData(Connection conn) throws SQLException{String sql="insert into tbl_address(id,city,country,user_id) values(1,'深圳','中国',10)";int count;Statement statement=(Statement)conn.createStatement();count = statement.executeUpdate(sql);System.out.println("向用户表中插入"+count+"条记录"); statement.close();}public static void main(String[] args) {// TODO Auto-generated method stubConnection conn=null;try {conn=getConnection();conn.setAutoCommit(false);insertTbUserData(conn);insertTbAddressData(conn);conn.commit();//提交事务} catch (SQLException e) {System.out.println("======捕获到SQL异常======");e.printStackTrace();try {conn.rollback();//事务回滚System.out.println("======事务回滚成功======");} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}finally {try {if (conn!=null) {conn.close();}} catch (Exception e3) {// TODO: handle exception}}}}
利用事务回滚,如果下面插入错误,则上面插入也将不成功。
0 0
- JDBC编程之事务处理
- JDBC编程之事务处理
- JDBC之简单事务处理
- JDBC之事务处理
- JDBC之事务处理
- 【Spring七】JDBC编程之声明式事务处理
- JDBC--编程之事务处理--回滚的简单实例
- JAVA事务处理 之 JDBC事务
- JAVA利用JDBC对数据库的操作和JDBC编程之事务处理
- JDBC进阶之事务处理基本原理及示例
- JDBC进阶之事务处理基本原理及示例
- jdbc事务处理
- jdbc事务处理
- jdbc事务处理
- JDBC ---- 事务处理
- JDBC事务处理
- jdbc事务处理
- jdbc事务处理
- tomcat-users.xml文件配置
- quick-cocos2d-x 中的 socket 技术选择:LuaSocket 和 WebSocket
- 线程常用接口
- java分布式通信系统(J2EE分布式服务器架构)
- 浅析return false的正确使用
- JDBC编程之事务处理
- Command /usr/bin/codesign failed with exit code 1
- 正则表达式快速入门(python示例)
- 电视信号——行场同步
- C++指针函数与函数指针
- 网站流量与性能分析指标:PV/UV/PR/IP/QPS/并发数/吞吐量/响应时间
- iOS 取消延时执行 performSelector: withObject: afterDelay:
- Web前端开发笔试题
- © 正常显示 ©