JDBC的事物管理
2012-11-03 10:21
316 查看
package jdbc;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdcbManager {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties jdbcProperty=new Properties();
try{
jdbcProperty.load(new FileInputStream("D:/tec_workspace/Cache/src/jdbc.properties"));
}catch(Exception e)
{
e.printStackTrace();
}
String driver=jdbcProperty.getProperty("jdbc.driver");
String url=jdbcProperty.getProperty("jdbc.url");
String username=jdbcProperty.getProperty("jdbc.username");
String pwd=jdbcProperty.getProperty("jdbc.password");
Connection conn = null;
Statement stmt=null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=DriverManager.getConnection(url,username,pwd);
conn.setAutoCommit(false);
stmt=conn.createStatement();
String psql="insert into t_order (ID,Amount,clientname) values(?,?,?)";
/*PreparedStatement pstmt = conn.prepareStatement(psql);
pstmt.setInt(1, 1);
pstmt.setInt(2, 1);
pstmt.setString(3, "helloword");
pstmt.executeUpdate();
*/
String sql="insert into t_order (ID,Amount,clientname) values(8,4,'helloword')";
stmt.executeUpdate(sql);
System.out.print("Transaction Roll Back");
conn.commit();
//conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn !=null){
/*try {
//conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
}
}
1.当 conn.setAutoCommit(false)的时候,如果conn.close(); 连接关闭的时候,会自动提交数据
2.如果连接不关闭的话,则数据不会提交
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdcbManager {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties jdbcProperty=new Properties();
try{
jdbcProperty.load(new FileInputStream("D:/tec_workspace/Cache/src/jdbc.properties"));
}catch(Exception e)
{
e.printStackTrace();
}
String driver=jdbcProperty.getProperty("jdbc.driver");
String url=jdbcProperty.getProperty("jdbc.url");
String username=jdbcProperty.getProperty("jdbc.username");
String pwd=jdbcProperty.getProperty("jdbc.password");
Connection conn = null;
Statement stmt=null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=DriverManager.getConnection(url,username,pwd);
conn.setAutoCommit(false);
stmt=conn.createStatement();
String psql="insert into t_order (ID,Amount,clientname) values(?,?,?)";
/*PreparedStatement pstmt = conn.prepareStatement(psql);
pstmt.setInt(1, 1);
pstmt.setInt(2, 1);
pstmt.setString(3, "helloword");
pstmt.executeUpdate();
*/
String sql="insert into t_order (ID,Amount,clientname) values(8,4,'helloword')";
stmt.executeUpdate(sql);
System.out.print("Transaction Roll Back");
conn.commit();
//conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn !=null){
/*try {
//conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
}
}
1.当 conn.setAutoCommit(false)的时候,如果conn.close(); 连接关闭的时候,会自动提交数据
2.如果连接不关闭的话,则数据不会提交
相关文章推荐
- jdbc实现事物管理并搬表
- 传智播客---什么是JDBC事物管理与数据库连接池
- 《Java》-------用AOP思想实现JDBC事物的管理
- hibernate 用spring实现jdbc事物管理
- JDBC——事物管理
- 详细解读Spring2.5 +Struts1.3 框架(使用Spring声明式事物管理和springjjdbc模板)
- 在spring中同时管理hibernate和jdbc
- Java JDBC学习实战(三): 事务管理
- Java Web 文章管理系统(Jsp+Ajax+JDBC+MySql实现)
- JDBC的事务管理
- JDBC、JTA、Spring的事务管理
- 课堂笔记--JDBC 事务控制和管理
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
- spring 事物管理的几种方法
- spring boot(七):springboot事物管理
- JDBC实现客户关系管理系统模块
- spring+springMVC 事物管理陷阱, springMVC 加载组件会覆盖父容器组件
- easyui + jdbc 实现简单的数据库管理。
- JDBC应用中的事务管理
- Spring基于HibernateTransactionManager的事物管理