300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > javaweb mysql 连接池 c3p0 配置_javaWeb_JDBC_c3p0数据库连接池

javaweb mysql 连接池 c3p0 配置_javaWeb_JDBC_c3p0数据库连接池

时间:2020-08-14 01:09:15

相关推荐

javaweb mysql 连接池 c3p0 配置_javaWeb_JDBC_c3p0数据库连接池

JDBC_c3p0数据库连接池

1.一个基本的c3p0数据库连接池

/**

* 一个基本的从池c3p0数据库连接池

*/

public static void testC3P0() throws Exception{

ComboPooledDataSource cpds = new ComboPooledDataSource();

cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver

cpds.setJdbcUrl( "jdbc:mysql:///test" );

cpds.setUser("root");

cpds.setPassword("123456");

System.out.println(cpds.getConnection());

}

2.通过配置文件来设置c3p0数据库连接池

(1).配置文件

c3p0-config.xml

root

1230

com.mysql.jdbc.Driver

jdbc:mysql:///atguigu

5

5

5

10

20

5

(2).测试代码

/**

* 1. 创建 c3p0-config.xml 文件,

* 参考帮助文档中 Appendix B: Configuation Files 的内容

* 2. 创建 ComboPooledDataSource 实例;

* DataSource dataSource =

*new ComboPooledDataSource("helloc3p0");

* 3. 从 DataSource 实例中获取数据库连接.

*/

public void testC3poWithConfigFile() throws Exception{

DataSource dataSource =

new ComboPooledDataSource("helloc3p0");

System.out.println(dataSource.getConnection());

ComboPooledDataSource comboPooledDataSource =

(ComboPooledDataSource) dataSource;

System.out.println(comboPooledDataSource.getMaxStatements());

}

3.数据库连接池应用

(1).创建连接的方法

private static DataSource dataSource = null;

//数据库连接池应只被初始化一次.

static{

dataSource = new ComboPooledDataSource("helloc3p0");

}

public static Connection getConnection() throws Exception {

return dataSource.getConnection();

}

(2).测试代码

public void testJdbcTools() throws Exception{

Connection connection = JDBCTools.getConnection();

System.out.println(connection);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。