jdbcUtils

来源:互联网 发布:上海交通事故数据统计 编辑:程序博客网 时间:2024/06/06 14:11

JDBCUtils

这里给出三个jdbc操作的工具类


首先给出c3p0-config.xml文件

<?xml version="1.0" encoding="UTF-8"?><c3p0-config>    <!-- 这是默认配置信息 -->    <default-config>         <!-- 连接四大参数配置 -->        <property name="jdbcUrl">jdbc:mysql://localhost:3306/nixgame</property>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="user">root</property>        <property name="password">123456</property>        <!-- 池参数配置 -->        <property name="acquireIncrement">3</property>        <property name="initialPoolSize">10</property>        <property name="minPoolSize">2</property>        <property name="maxPoolSize">10</property>    </default-config></c3p0-config>

使用数据库连接池,并且开启了事务

import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class JdbcUtils {    // 配置文件的默认配置!要求你必须给出c3p0-config.xml!!!    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();    // 它是事务专用连接!    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();    /**     * 使用连接池返回一个连接对象     * @return     * @throws SQLException     */    public static Connection getConnection() throws SQLException {        Connection con = tl.get();        // 当con不等于null,说明已经调用过beginTransaction(),表示开启了事务!        if(con != null) return con;        return dataSource.getConnection();    }    /**     * 返回连接池对象!     * @return     */    public static DataSource getDataSource() {        return dataSource;    }    /**     * 开启事务     * 1. 获取一个Connection,设置它的setAutoComnmit(false)     * 2. 还要保证dao中使用的连接是我们刚刚创建的!     * --------------     * 1. 创建一个Connection,设置为手动提交     * 2. 把这个Connection给dao用!     * 3. 还要让commitTransaction或rollbackTransaction可以获取到!     * @throws SQLException      */    public static void beginTransaction() throws SQLException {        Connection con = tl.get();        if(con != null) throw new SQLException("已经开启了事务,就不要重复开启了!");        /*         * 1. 给con赋值!         * 2. 给con设置为手动提交!         */        con = getConnection();//给con赋值,表示事务已经开始了        con.setAutoCommit(false);        tl.set(con);//把当前线程的连接保存起来!    }    /**     * 提交事务     * 1. 获取beginTransaction提供的Connection,然后调用commit方法     * @throws SQLException      */    public static void commitTransaction() throws SQLException {        Connection con = tl.get();//获取当前线程的专用连接        if(con == null) throw new SQLException("还没有开启事务,不能提交!");        /*         * 1. 直接使用con.commit()         */        con.commit();        con.close();        // 把它设置为null,表示事务已经结束了!下次再去调用getConnection()返回的就不是con了        tl.remove();//从tl中移除连接    }    /**     * 提交事务     * 1. 获取beginTransaction提供的Connection,然后调用rollback方法     * @throws SQLException      */    public static void rollbackTransaction() throws SQLException {        Connection con = tl.get();        if(con == null) throw new SQLException("还没有开启事务,不能回滚!");        /*         * 1. 直接使用con.rollback()         */        con.rollback();        con.close();        tl.remove();    }    /**     * 释放连接      * @param connection     * @throws SQLException      */    public static void releaseConnection(Connection connection) throws SQLException {        Connection con = tl.get();        /*         * 判断它是不是事务专用,如果是,就不关闭!         * 如果不是事务专用,那么就要关闭!         */        // 如果con == null,说明现在没有事务,那么connection一定不是事务专用的!        if(con == null) connection.close();        // 如果con != null,说明有事务,那么需要判断参数连接是否与con相等,若不等,说明参数连接不是事务专用连接        if(con != connection) connection.close();    }}

测试

@Testpublic void fun1(){    try {        Connection con = JDBCUtils.getConnection();        System.out.println(con);    } catch (SQLException e) {        e.printStackTrace();    }}

工具类2 使用数据库连接池,但是没有包含事务

import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class JdbcUtils {    // 配置文件的默认配置!要求你必须给出c3p0-config.xml!!!    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();    /**     * 使用连接池返回一个连接对象     * @return     * @throws SQLException     */    public static Connection getConnection() throws SQLException {        return dataSource.getConnection();    }    /**     * 返回连接池对象!     * @return     */    public static DataSource getDataSource() {        return dataSource;    }}

工具类3 加载的是properties文件,也没有使用数据库连接池

给出properties文件

driverClassName=com.mysql.jdbc.Driverurl=jdbc\:mysql\://localhost\:3306/***?rewriteBatchedStatements\=trueusername=rootpassword=****
package demo1.jdbc;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;public class jdbcUtils_3 {    private static Properties props = null;    // 只在JdbcUtils类被加载时执行一次!    static {        // 给props进行初始化,即加载dbconfig.properties文件到props对象中        try {            InputStream in = jdbcUtils_3.class.getClassLoader()                    .getResourceAsStream("dbconfig.properties");            props = new Properties();            props.load(in);        } catch(IOException e) {            throw new RuntimeException(e);        }        // 加载驱动类        try {            Class.forName(props.getProperty("driverClassName"));        } catch (ClassNotFoundException e) {            throw new RuntimeException(e);        }    }    // 获取连接!    public static Connection getConnection() throws SQLException {        // 得到Connection        return DriverManager.getConnection(props.getProperty("url"),                props.getProperty("username"),                 props.getProperty("password"));    }}
0 0
原创粉丝点击