博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C3P0连接池工具类使用
阅读量:4641 次
发布时间:2019-06-09

本文共 1883 字,大约阅读时间需要 6 分钟。

c3p0的基本连接配置文件 c3p0-config.xml

com.mysql.jdbc.Driver
jdbc:mysql:///mybase
root
123456
5
20
com.mysql.jdbc.Driver
jdbc:mysql:///mybase
root
123456

c3p0工具类

package cn.cc.jdbc.utls;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3P0Utils {    private static ComboPooledDataSource dataSource=new ComboPooledDataSource();    public  static DataSource getDataSource(){        return dataSource;    }        public static Connection getConnection(){        try{            return dataSource.getConnection();        }catch (SQLException e){            throw new RuntimeException(e);        }    }}

测试类

public class TestC3p0 {        @Test    public void testAddUser1(){        Connection conn=null;        PreparedStatement ps=null;                try{            //2.从池子中获取连接            conn=C3P0Utils.getConnection();            String sql="insert into tbl_user values(null,?,?)";            //3.必须在自定义的connection实现类中重写preparedStatement方法            ps=conn.prepareStatement(sql);            ps.setString(1, "吕布3");            ps.setString(2, "貂蝉3");            int rows=ps.executeUpdate();            if(rows>0){                System.out.println("添加成功");            }else{                System.out.println("添加失败");            }        }catch (Exception e){            throw new RuntimeException(e);        }finally {            JDBCUtils_v3.release(conn, ps, null);        }    }    }

 

转载于:https://www.cnblogs.com/benjamin77/p/9166875.html

你可能感兴趣的文章
LAMP环境搭建
查看>>
C语言的变量的内存分配
查看>>
clientcontainerThrift Types
查看>>
链接全局变量再说BSS段的清理
查看>>
hdu 1728 逃离迷宫
查看>>
HTML5与CSS3权威指南之CSS3学习记录
查看>>
docker安装部署
查看>>
AVL树、splay树(伸展树)和红黑树比较
查看>>
多媒体音量条显示异常跳动
查看>>
运算符及题目(2017.1.8)
查看>>
React接入Sentry.js
查看>>
ssh自动分发密匙脚本样板
查看>>
转 小辉_Ray CORS(跨域资源共享)
查看>>
Linux安装postgresql
查看>>
MyBatis启动:MapperStatement创建
查看>>
【 全干货 】5 分钟带你看懂 Docker !
查看>>
[转]优化Flash性能
查看>>
popStar手机游戏机机对战程序
查看>>
Java Web项目结构
查看>>
lambda表达式树
查看>>