来源:自学PHP网 时间:2015-04-16 10:50 作者: 阅读:次
[导读] 事务处理流程1、屏蔽自动提交功能2、处理事务3、恢复自动提交功能代码实例执行程序之前数据表的样子pre name=code class=javaimport java sql *;public class GetConnection{public sta...
|
事务处理流程 1、屏蔽自动提交功能 2、处理事务 3、恢复自动提交功能 代码实例 执行程序之前数据表的样子
public class GetConnection{
public static void main(String[] args){
Access2Database adb=new Access2Database();
Connection conn=adb.getConn();
//transaction dealing
PreparedStatement pstam=null;
try{
conn.setAutoCommit(false);
String sql="delete from student where name='a' and major=?";
pstam=conn.prepareStatement(sql);
pstam.setString(1, "Chinese");
pstam.executeUpdate();
conn.rollback();
conn.commit();
}catch(SQLException e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//release the resource of the program
try{
pstam.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
之后的样子
====================================================================== 除此应用外,还可以保存事务处理的中间态,最后可以恢复到此中间保存状态 数据表之前的状态
看代码
import java.sql.*;
public class GetConnection{
public static void main(String[] args){
Access2Database adb=new Access2Database();
Connection conn=adb.getConn();
//transaction dealing
PreparedStatement pstam=null;
try{
conn.setAutoCommit(false);
String sql="delete from student where name='a' and major=?";
pstam=conn.prepareStatement(sql);
pstam.setString(1, "Chinese");
pstam.executeUpdate();
//conn.commit();
Savepoint sp=conn.setSavepoint();
sql="insert into student(name,major,score) values('g','Math','99');";
pstam=conn.prepareStatement(sql);
pstam.executeUpdate();
conn.rollback(sp);
conn.commit();
}catch(SQLException e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//release the resource of the program
try{
pstam.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
![]() |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com