首页 > 学院 > 开发设计 > 正文

自定义jdbc

2019-11-14 23:04:51
字体:
来源:转载
供稿:网友
自定义jdbc
package com.util.jdbc;import java.sql.Connection;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.alibaba.druid.pool.DruidDataSource;/** * JDBC封装类 * @author DC * */public class JDBCUtils{    PRivate static DruidDataSource dataSource  = new DruidDataSource();    //声明线程共享变量    public static ThreadLocal<Connection> container = new ThreadLocal<Connection>();    //配置说明,参考官方网址    //http://blog.163.com/hongwei_benbear/blog/static/1183952912013518405588/    static{        dataSource.setUrl("jdbc:MySQL://182.92.222.140:3306/idotest?useUnicode=true&characterEncoding=UTF-8");        dataSource.setUsername("");//用户名        dataSource.setPassWord("");//密码        dataSource.setInitialSize(2);        dataSource.setMaxActive(20);        dataSource.setMinIdle(0);        dataSource.setMaxWait(60000);        dataSource.setValidationQuery("SELECT 1");        dataSource.setTestOnBorrow(false);        dataSource.setTestWhileIdle(true);        dataSource.setPoolPreparedStatements(false);    }    //禁止实体化该类    private JDBCUtils() {}    /**     * 获取数据连接     * @return     */    public static Connection connStart(boolean start){        Connection conn =container.get();        try{            if(conn!=null){                System.out.println(Thread.currentThread().getName()+"从map缓存中取得连接,Hashcode="+conn.hashCode());            }else{                System.out.println(Thread.currentThread().getName()+"从连接池中得到连接");                conn = dataSource.getConnection();                container.set(conn);            }            if(start){                conn.setAutoCommit(false);//开启事务,默认事务级别为TRANSACTION_REPEATABLE_READ(4)            }        }catch(Exception e){            System.out.println("连接获取失败");            e.printStackTrace();        }        return conn;    }    /***关闭连接*/    public static void connEnd(boolean end){        Connection conn=container.get();        if(conn!=null){            try{                if(end){                    conn.commit();                    conn.setAutoCommit(true);                }            }catch(Exception e){                System.out.println("事物提交异常");                try {                    if(end){                        conn.rollback();                        System.out.println(Thread.currentThread().getName()+"事务已回滚.....hashcode+"+conn.hashCode());                    }                } catch (Exception e1) {                    System.out.println("事物回滚异常");                    e1.printStackTrace();                }                e.printStackTrace();            }finally{                try {                    conn.close();                    System.out.println(Thread.currentThread().getName()+"连接关闭");                    container.remove();//从当前线程移除连接切记                    System.out.println(Thread.currentThread().getName()+"成功移除连接。。。。");                } catch (Exception e2) {                    System.out.println("关闭连接异常,或者是移除缓存中的连接出错..............");                    e2.printStackTrace();                }            }        }else{            System.out.println("关闭连接时,连接为空");        }    }    public void aa(List<Integer> a,int b){        a.set(0, 2);        b=2;    }    //简单使用方式    public static void main(String[] args) throws SQLException {        JDBCUtils j = new JDBCUtils();        List<Integer> a = new ArrayList<Integer>();        a.add(1);        int b =1;        j.aa(a,b);        System.out.println(a.get(0));        System.out.println(b);    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表