JDBC编程之事务处理
2016-03-15 11:06
477 查看
当插入数据遇到唯一性约束时,数据插入失败,如下列操作,插入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 block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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 block
e.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 block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
insertTbUserData();
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 block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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 stub
Connection 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 exception
e2.printStackTrace();
}
}
finally {
try {
if (conn!=null) {
conn.close();
}
} catch (Exception e3) {
// TODO: handle exception
}
}
}
}
利用事务回滚,如果下面插入错误,则上面插入也将不成功。
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 block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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 block
e.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 block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
insertTbUserData();
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 block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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 stub
Connection 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 exception
e2.printStackTrace();
}
}
finally {
try {
if (conn!=null) {
conn.close();
}
} catch (Exception e3) {
// TODO: handle exception
}
}
}
}
利用事务回滚,如果下面插入错误,则上面插入也将不成功。
相关文章推荐
- python实现各个排序算法【转】
- PHP--根据手机号-淘宝平台获取归属地运营商信息
- Github——建立博客
- JAVA一些符号符的简单了解
- php 编码问题集锦
- Java中泛型的作用
- JAVA NIO汇总
- 2016世界最热门的编程语言与薪资揭秘
- PHP闭包函数传参及使用外部变量的方法
- mac下yii框架的搭建
- [Canvas绘图] 第34节 藏图阁(14) AlphaGo与李世石的围棋大战之第四局
- MATLAB GUI实现常见图像处理算法与视频跟踪算法
- Qt 实时读串口数据,并将读到的数据从网口发送出去
- [C#]Bencode编码算法/torrent文件解析
- PHP中标识(zhi)符的大小写要求
- C++ 内存池学习 -- C++ Memory Pool
- Struts2 - 常用的constant总结
- JAVA线程池
- Spring进阶之路(10)-Advice简介以及通过cglib生成AOP代理对象
- 用java.util.Observable实现Observer模式