您的位置:首页 > 数据库

数据库连接池、读取properties配置文件小结

2016-07-20 22:59 295 查看
C3P0连接池:

直接通过java代码设置连接池(需要在工程目录下导入c3p0和数据库连接的jar包):

步骤:  1.创建C3P0数据源对象
2.设置数据库连接基本信息
3.设置数据库连接池参数
import java.sql.Connection;

import com.mchange.v2.c3p0.ComboPooledDataSource;
//C3P0Pool连接池

public class C3P0Pool {
public static void main(String[] args) throws Exception {
//第一步:创建C3P0数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//第二步:设置数据库连接基本信息
//设置数据库来接驱动
dataSource.setDriverClass("oracle.jdbc.OracleDriver");
//设置数据连接URL
dataSource.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
//设置数据库连接账号
dataSource.setUser("xiaogang");
//设置数据库连接账号的密码
dataSource.setPassword("xiaogang");
//第三步:设置数据库连接池参数
//设置数据库连接池中连接的最小数量
dataSource.setMinPoolSize(30);
//设置数据库连接池中连接的初始数量
dataSource.setInitialPoolSize(30);
//设置数据库连接池中连接的最大数量
dataSource.setMaxPoolSize(50);
//未达到最大连接数,当前需要新增连接数量设定
dataSource.setAcquireIncrement(10);

long start=System.currentTimeMillis();
for(int i=0;i<1000;i++){
Connection con=dataSource.getConnection();
con.close();
}
long end=System.currentTimeMillis();
System.out.println(end - start);

}
}


利用properties文件设置数据库连接基本信息:

步骤:  1.配置文件位于src目录下,配置文件路径应遵循类加载方式
InputStream in = null;
in=C3P0Pool.class.getClassLoader().getResourceAsStream("com/xiaogang/jdbcc3p0.properties")
2.将文件和Properties对象关联
Properties config = new Properties();
config.load(in);
3.可以通过config对象的config.getProperty("driver"));获取文件中的属性
备注:
//Properties属性信息(属性名和属性值)
Properties prop = new Properties();
//存储数据格式:属性名=属性值
//方法:put(attName,attValue);
prop.put("name", "xiaosan");
prop.put("now", new Date());
//获取属性的值: String getProperty(name)
String value=prop.getProperty("name");
System.out.println("name:"+value);
//获取属性的值:  Object  get(key)
String nowValue=prop.get("now").toString();

import java.beans.PropertyVetoException;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Pool {

private static ComboPooledDataSource dataSource;
private static Properties config = new Properties();

private static void init(){
try {
//装载配置文件
config.load(C3P0Pool.class.getClassLoader()
.getResourceAsStream("com/heres/jdbcc3p0.properties"));
//声明C3P0数据源对象
dataSource = new ComboPooledDataSource();
//设置数据库连接驱动
dataSource.setDriverClass(
config.getProperty("driver"));
//设置数据连接URL
dataSource.setJdbcUrl(
config.getProperty("url"));
//设置数据库连接用户账号
dataSource.setUser(
config.getProperty("user"));
//设置数据库连接用户账号的密码
dataSource.setPassword(
config.getProperty("password"));

//设置数据库连接池中的初始化连接对象数量
dataSource.setInitialPoolSize(30);
//设置数据库连接池中的最小连接对象数量
dataSource.setMinPoolSize(30);
//设置数据库连接池中的最大连接对象数量
dataSource.setMaxPoolSize(60);
//当连接不够,每次新增连接数量
dataSource.setAcquireIncrement(10);

} catch (IOException e) {
e.printStackTrace();
} catch(PropertyVetoException e){
e.printStackTrace();
}

}

public static void main(String[] args) throws SQLException{
init();

int i=1000;
long beginTime = System.currentTimeMillis();
while(i-->0){
Connection con = dataSource.getConnection();
con.close();
}
long endTime = System.currentTimeMillis();
long time = endTime - beginTime;
System.out.println("处理时间:"+time);
}

}


对应的properties文件:

user=scott

password=tiger

driver=oracle.jdbc.driver.OracleDriver

url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl

dbcp连接池(需要在工程目录下导入commons-pool.jar、commons-beanutils.jar、commons-dbcp.jar、commons-logging.jar和数据库连接的jar包):

import java.sql.Connection;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DBCPPool {

private static DataSource dataSource=null;
private static Properties dbconfig = new Properties();

public static void init(){
try {
//装载配置文件
dbconfig.load(DBCPPool.class.getClassLoader()
.getResourceAsStream("com/heres/jdbcdbcp.properties"));
System.out.println(dataSource);
//通过工厂类去创建数据源对象
dataSource=BasicDataSourceFactory.createDataSource(dbconfig);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) throws SQLException{
init();

int i=1000;
long beginTime = System.currentTimeMillis();
while(i-->0){
Connection con = dataSource.getConnection();
con.close();
}
long endTime = System.currentTimeMillis();
long time = endTime - beginTime;
System.out.println("处理时间:"+time);
}

}
对应得配置文件:

username=scott

password=tiger

driverClassName=oracle.jdbc.driver.OracleDriver

url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: