标签:定义 override .com 属性 lex ini 而不是 tst enc
1、C3P0基本概念。
C3P0是一个开源的JDBC
连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的
开源项目有Hibernate,Spring等。
如果不知道什么是线程池,可以百度一下,我理解的呢,就是一个大水池,里面有很多鱼,每条鱼对应一个
数据库连接,
你拿到一条鱼你就获得一个连接,当然,这鱼不能吃,你摸完了还要放回去的。这例子有点变态哈哈,能理解就行。
所需jar包:c3p0-0.9.2.1.jar,mchange-commons-java-0.2.3.4.jar,mysql-connector-java-5.1.40-bin.jar;
2、C3P0的三种配置方式。
c3p0的配置方式分为三种,分别是
1.类路径下提供一个c3p0-config.xml文件
2.类路径下提供一个c3p0.properties文件
3.setters一个个地设置各个配置项
2.1、xml文件配置
使用读取配置文件的方式,要求是, 配置文件必须命名为c3p0-config.xml,并且放在src目录下 ,配置文件如下:
- <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
- <c3p0-config>
- <default-config>
- <property name="user">root</property>
- <property name="password">123456</property>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
- <property name="driverClass">com.mysql.jdbc.Driver</property>
- <property name="checkoutTimeout">30000</property>
- <property name="idleConnectionTestPeriod">30</property>
- <property name="initialPoolSize">10</property>
- <property name="maxIdleTime">30</property>
- <property name="maxPoolSize">100</property>
- <property name="minPoolSize">10</property>
- <property name="maxStatements">200</property>
- <user-overrides user="test-user">
- <property name="maxPoolSize">10</property>
- <property name="minPoolSize">1</property>
- <property name="maxStatements">0</property>
- </user-overrides>
- </default-config>
-
- <named-config name="test">
- <property name="user">root</property>
- <property name="password">123456</property>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
- <property name="driverClass">com.mysql.jdbc.Driver</property>
-
- <property name="acquireIncrement">5</property>
-
- <property name="initialPoolSize">20</property>
-
- <property name="maxPoolSize">25</property>
-
- <property name="minPoolSize">5</property>
- </named-config>
- </c3p0-config></span>
获取connection的java代码为:
- <span style="font-size:14px;">package com.wqc.util;
-
- import java.sql.Connection;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- public class C3P0Utils {
-
-
- private static ComboPooledDataSource cpds;
-
- static {
- cpds = new ComboPooledDataSource("test");
- }
-
- public static DataSource getDataSource() {
- return cpds;
- }
-
- public static Connection getConnection() throws SQLException {
- return cpds.getConnection();
- }
-
- }</span>
获取到Connection对象后就可以通过类似Apache的DbUtils这样的工具类(或者原生的Preparedstatement)来访问操作数据库。
2.2、c3p0.properties文件配置
使用读取属性文件的方式,要求是, 属性文件必须命名为c3p0.properties,(注意:名字是固定的,c3p0默认只认识这个名字)并且放在src目录下 ,配置文件如下:
- <span style="font-size:14px;">c3p0.driverClass=com.mysql.jdbc.Driver
- c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test
- c3p0.user=root
- c3p0.password=123456</span>
获取connection对象的代码:
- <span style="font-size:14px;">package com.wqc.test;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- public class C3P0Utils {
- public void getConnection() throws SQLException {
- DataSource ds = new ComboPooledDataSource();
- Connection con = ds.getConnection();
- System.out.println("con:"+con);
-
- String sql = "select * from user";
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
-
- while(rs.next())
- {
- System.out.println(rs.getString(1));
- System.out.println(rs.getString(2));
- System.out.println(rs.getString(3));
- System.out.println(rs.getString(5));
- System.out.println(rs.getString(6));
- System.out.println(rs.getString(7));
- }
-
- con.close();
- rs.close();
- ps.close();
- }
- }</span>
2.3、setters一个个地设置各个配置项
通过读取properties配置文件。然后获取c3p0的连接。
首先我们准备一个db.properties
- <span style="font-size:14px;">jdbcdriver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/test
- username=root
- password=123465</span>
获取Connection对象的Java代码:
- package com.wqc.test;
-
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Properties;
-
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- public class C3P0Utils {
- public void getConnection() throws SQLException {
- Properties prop = new Properties();
- InputStream in = C3P0Utils.class.getClassLoader().getResourceAsStream("db.properties");
- prop.load(in);
-
- ComboPooledDataSource comb = new ComboPooledDataSource();
- comb.setDriverClass(prop.getProperty("jdbcdriver"));
- comb.setJdbcUrl(prop.getProperty("url"));
- comb.setUser(prop.getProperty("jdbc:mysql://localhost:3306/test"));
- comb.setPassword(prop.getProperty("root"));
-
- Connection con = comb.getConnection();
-
- String sql = "select * from user";
- PreparedStatement ps = con.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
-
- while(rs.next())
- {
- System.out.println(rs.getString(1));
- System.out.println(rs.getString(2));
- System.out.println(rs.getString(3));
- System.out.println(rs.getString(5));
- System.out.println(rs.getString(6));
- System.out.println(rs.getString(7));
- }
-
- con.close();
- rs.close();
- ps.close();
- }
- }
C3P0 Java 数据库系列
标签:定义 override .com 属性 lex ini 而不是 tst enc
原文地址:https://www.cnblogs.com/albertzhangyu/p/8962941.html