C3P0数据库Jar包的使用

摘要:
C3P0数据库Jar包的使用0.导入数据库相关jar包commons-dbutils-1.4.jarc3p0-0.9.1.2.jar1.配置C3P0-config.xml文件rootadminscom.mysql.jdbc.Driverjdbc:mysql:///shop2.然后写一个DataSourceUtils工具类packagecom.shop.utils;importcom.mchange.v2.c3p0.ComboPooledDataSource;importjavax.sql.DataSource;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;/***数据库连接工具*/publicclassDataSourceUtils{privatestaticDataSourcedataSource=newComboPooledDataSource();privatestaticThreadLocaltl=newThreadLocal();//直接可以获取一个连接池publicstaticDataSourcegetDataSource(){returndataSource;}//获取连接对象publicstaticConnectiongetConnection()throwsSQLException{Connectioncon=tl.get();if{con=dataSource.getConnection();tl.set;}returncon;}//开启事务publicstaticvoidstartTransaction()throwsSQLException{Connectioncon=getConnection();if(con!=null){con.rollback();}}//提交并且关闭资源及从ThreadLocal中释放publicstaticvoidcommitAndRelease()throwsSQLException{Connectioncon=getConnection();if(con!=null){con.commit();//事务提交con.close();//关闭资源tl.remove();//从线程绑定中移除}}//关闭资源方法publicstaticvoidcloseConnection()throwsSQLException{Connectioncon=getConnection();if(con!)";runner.update;}这里查询是用query方法,增删改用update方法;查询需要映射到一个实体类,而增删改往往需要预编译参数

C3P0数据库Jar包的使用

0.导入数据库相关jar包

commons-dbutils-1.4.jar

c3p0-0.9.1.2.jar

C3P0数据库Jar包的使用第1张

1.配置C3P0-config.xml文件

<?xml version="1.0" encoding="UTF-8"?>  
<c3p0-config>  
    <!-- 数据库连接池 -->  
    <default-config>  
        <property name="user">root</property>  
        <property name="password">admins</property>  
        <property name="driverClass">com.mysql.jdbc.Driver</property>  
        <property name="jdbcUrl">jdbc:mysql:///shop</property>  
    </default-config>   
</c3p0-config>   

2.然后写一个DataSourceUtils工具类

packagecom.shop.utils;

importcom.mchange.v2.c3p0.ComboPooledDataSource;

importjavax.sql.DataSource;
importjava.sql.Connection;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;

/*** 数据库连接工具
 */
public classDataSourceUtils {

    private static DataSource dataSource = newComboPooledDataSource();

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    //直接可以获取一个连接池
    public staticDataSource getDataSource() {
        returndataSource;
    }

    //获取连接对象
    public static Connection getConnection() throwsSQLException {
        Connection con =tl.get();
        if (con == null) {
            con =dataSource.getConnection();
            tl.set(con);
        }
        returncon;
    }

    //开启事务
    public static void startTransaction() throwsSQLException {
        Connection con =getConnection();
        if (con != null) {
            con.setAutoCommit(false);
        }
    }

    //事务回滚
    public static void rollback() throwsSQLException {
        Connection con =getConnection();
        if (con != null) {
            con.rollback();
        }
    }

    //提交并且关闭资源及从ThreadLocal中释放
    public static void commitAndRelease() throwsSQLException {
        Connection con =getConnection();
        if (con != null) {
            con.commit(); //事务提交
            con.close();//关闭资源
            tl.remove();//从线程绑定中移除
}
    }

    //关闭资源方法
    public static void closeConnection() throwsSQLException {
        Connection con =getConnection();
        if (con != null) {
            con.close();
        }
    }

    public static void closeStatement(Statement st) throwsSQLException {
        if (st != null) {
            st.close();
        }
    }

    public static void closeResultSet(ResultSet rs) throwsSQLException {
        if (rs != null) {
            rs.close();
        }
    }

}

3.最后在相关dao中就可以直接使用了

如:查询商品所有分类

//查询商品所有分类  
public List<Category> findAllCategory() throwsSQLException {  
  QueryRunner runner=newQueryRunner(DataSourceUtils.getDataSource());  
  String sql="select * from category";  
  return runner.query(sql,new BeanListHandler<Category>(Category.class));  
}  

如:添加商品分类

//添加商品分类  
public void saveProductCategory(Category category) throwsSQLException{  
    QueryRunner runner=newQueryRunner(DataSourceUtils.getDataSource());  
    String sql="insert into category values(?,?)";  
    runner.update(sql,category.getCid(),category.getCname());  
}  

这里查询是用query方法,增删改用update方法;

查询需要映射到一个实体类,而增删改往往需要预编译参数

免责声明:文章转载自《C3P0数据库Jar包的使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇haskell模块(modules)python(openpyxl)复制excel数据到另一个excel数据表下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c3p0配置详解

数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理。 一、实现方式: C3P0有三种方式实现: 1.自己动手写代码,实现数据源 例如:在类路径下配置一个属性文件,config.properties,内容如下: driverClass=xxx jdbcUrl=xxx user=xxx pass...

Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z is abstract

问题原因:本地用的c3p0为0.9.1.2版本,版本不兼容。 解决方法: maven方式:pom引用升级到 c3p0-0.9.5.5,并且加入 mchange-commons-java-0.2.19 的依赖。 lib方式:直接 下载c3p0-0.9.5.5.bin.zip,解压后将 c3p0-0.9.5.5lib 下的 c3p0-0.9.5.5.jar 和...

C3P0线程死锁问题探讨

报错信息: inas] 2019-01-11 10:15:59,955 WARN [C3P0PooledConnectionPoolManager[identityToken->2zioz0a0ga5qw7onzyne|10cbafaa]-AdminTaskTimer] ThreadPoolAsynchronousRunner.log(211) |...

Hibernate用Mysql数据库时链接关闭异常的解决

在一个项目中,客户要求除操作系统外全部使用免费软件,因此我使用了Mysql 4.0作为数据库服务器,其JDBC驱动为3.0.9版本,在给客户安装后调试一切正常。可是到了第二天,只要一登录就提示“No operations allowed after connection closed”异常,显示在浏览器上。在经过一番检查后我发现,在这种情况下只要重新启动T...

Spring c3p0连接池无法释放解决方案

通过c3p0配置连接池的时候,在进行压力测试的时候,日志出现了这样一个错误:Data source rejected establishment of connection, message from server: “Too many connections” 数据库的连接过多。 然后查看mysql的最大连接数,为100。 于是便更改最大连接池。 由于...

C3P0连接池

C3P0连接池: 最常用的连接池技术!Spring框架,默认支持C3P0连接池技术! C3P0连接池,核心类: CombopooledDataSource ds; 使用: 下载,引入jar文件: c3p0-0.9.1.2.jar 使用连接池,创建连接 a) 硬编码方式 b) 配置方式(xml) packagecom.loaderman.demo.d_...