• Jdbc 事务


    package com.j1;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.PreparedStatement;
    
    
    public class JdbcTest1 {
    
    public void test1(){
    Connection conn=null;
    PreparedStatement smt=null;
    try {
    conn = JdbcUnit.getConnection();
    conn.setAutoCommit(false);//开启事物
    smt= (PreparedStatement) conn.prepareStatement("");
    smt.executeUpdate();
    conn.prepareStatement("");
    conn.commit();//提交事物
    } catch (Exception e) {
    try {
    conn.rollback();
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    e.printStackTrace();
    }finally{
    //释放资源
    JdbcUnit.release(null, smt, conn);
    }
    
    }
    
    }
    
    //公用类
    
    package com.j1;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ResourceBundle;
    
    import org.apache.taglibs.standard.tag.common.fmt.BundleSupport;
    
     
    
    public class JdbcUnit {
    private static String clazzName=null;
    private static String url=null;
    private static String username=null;
    private static String password=null;
    
    //读取配置文件
    static{
    
    ResourceBundle bundle=ResourceBundle.getBundle("dbinfo");
    clazzName=bundle.getString("clazzName");
    url=bundle.getString("url");
    username=bundle.getString("username");
    password=bundle.getString("password");
    }
    public static Connection getConnection(){
    
    
    loadDriver();
    try {
    return DriverManager.getConnection(url, username, password);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw new RuntimeException(e);
    }
    
    }
    private static void loadDriver() {
    try {
    Class.forName(clazzName);
    
    
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    
    
    }
    public static void release(ResultSet rs, Statement stmt, Connection conn){
    
    if(rs!=null){
    try {
    rs.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    rs=null;
    }
    if(stmt!=null){
    try {
    stmt.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    stmt=null;
    }
    if(conn!=null){
    try {
    conn.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    conn=null;
    }
    
    }
    }
    
     
    
     
    
    /**
    
    事务为同一个Connnection对象
    try catch时尽量抓到最大Exception
    
    */
  • 相关阅读:
    数据库默认隔离级别
    openldap安装
    new word
    ldap概念
    Oracle 计算函数
    informix 学习资料收集
    convert to groovy project
    ldap资料
    hibernate session
    IE BUG相关文章集合
  • 原文地址:https://www.cnblogs.com/wangchuanfu/p/5561451.html
Copyright © 2020-2023  润新知