c3p0数据库连接池的简单使用
2017-10-28 14:29
323 查看
第一种,加载配置文件
private static ComboPooledDataSource ds = null;
static{
//加载配置文件,得到数据库配置的一些信息
Properties prop = new Properties();
InputStream stream = JDBCUtil2.class.getResourceAsStream("/db.properties");
try {
prop.load(stream);
ds = new ComboPooledDataSource();
ds.setDriverClass(prop.getProperty("driverClassName"));
ds.setJdbcUrl(prop.getProperty("url"));
ds.setUser(prop.getProperty("username"));
ds.setPassword(prop.getProperty("password"));
ds.setCheckoutTimeout(3000);
ds.setInitialPoolSize(5);
ds.setMaxPoolSize(10);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new Exception("连接失败");
}
}
配置文件如下(可灵活编写):
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
maxWait=3000
initialSize=5
maxActive=10
第二种,也是加载配置文件(自动加载,然后直接获取连接对象就好但文件的名字一定要为c3p0-properties);
第三种,加载xml文件(自动加载,文件名要名为c3p0-config.xml)
如下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password">123456</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="initialPoolSize">5</property>
<property name="checkoutTimeout">3000</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="mySource">//
<property name="user">root</property>
<property name="password">123456</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="initialPoolSize">5</property>
<property name="checkoutTimeout">3000</property>
<property name="maxPoolSize">15</property>
<property name="minPoolSize">5</property>
</named-config>
</c3p0-config>
ComboPooledDataSource ds = new ComboPooledDataSource("mySource");
不指定mySource的话,默认使用default-config配置的
private static ComboPooledDataSource ds = null;
static{
//加载配置文件,得到数据库配置的一些信息
Properties prop = new Properties();
InputStream stream = JDBCUtil2.class.getResourceAsStream("/db.properties");
try {
prop.load(stream);
ds = new ComboPooledDataSource();
ds.setDriverClass(prop.getProperty("driverClassName"));
ds.setJdbcUrl(prop.getProperty("url"));
ds.setUser(prop.getProperty("username"));
ds.setPassword(prop.getProperty("password"));
ds.setCheckoutTimeout(3000);
ds.setInitialPoolSize(5);
ds.setMaxPoolSize(10);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new Exception("连接失败");
}
}
配置文件如下(可灵活编写):
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
maxWait=3000
initialSize=5
maxActive=10
第二种,也是加载配置文件(自动加载,然后直接获取连接对象就好但文件的名字一定要为c3p0-properties);
第三种,加载xml文件(自动加载,文件名要名为c3p0-config.xml)
如下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password">123456</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="initialPoolSize">5</property>
<property name="checkoutTimeout">3000</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="mySource">//
<property name="user">root</property>
<property name="password">123456</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="initialPoolSize">5</property>
<property name="checkoutTimeout">3000</property>
<property name="maxPoolSize">15</property>
<property name="minPoolSize">5</property>
</named-config>
</c3p0-config>
ComboPooledDataSource ds = new ComboPooledDataSource("mySource");
不指定mySource的话,默认使用default-config配置的
相关文章推荐
- c3p0数据库连接池无法连接数据库—错误使用了username关键字
- Druid数据库连接池的简单使用
- c3p0数据库连接池的使用详解
- 数据库连接池 dbcp与c3p0的使用
- c3p0数据库连接池使用小结
- 使用c3p0数据库连接池时出现com.mchange.v2.resourcepool.TimeoutException
- springboot 使用c3p0数据库连接池的方法
- C3P0和dbutils数据库连接池封装的使用(一)
- 12、数据库连接池C3P0的使用
- java 使用c3p0数据库连接池配置实例
- c3p0数据库连接池使用报错【 You can't operate on a closed Connection!!!】解决方案
- Java使用C3P0数据库连接池的方法
- 数据库连接池DBCP和C3P0的使用
- JDBC之连接池DBCP与c3p0的简单使用
- c3p0简单使用
- debug笔记:使用c3p0数据库连接池配置datasource出现ClassNotFoundException的解决方法
- 详解springboot 使用c3p0数据库连接池的方法
- C3p0数据库连接池的使用
- C3p0数据库连接池的使用
- JDBC_数据库连接池(二) DNCP与C3P0的使用方法及代码演示