数据库连接池、读取properties配置文件小结
2016-07-20 22:59
295 查看
C3P0连接池:
直接通过java代码设置连接池(需要在工程目录下导入c3p0和数据库连接的jar包):
步骤: 1.创建C3P0数据源对象
2.设置数据库连接基本信息
3.设置数据库连接池参数
利用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();
对应的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包):
username=scott
password=tiger
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
直接通过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
相关文章推荐
- Oracle语句
- MySQL长事务导致的Table Metadata Lock
- zabbix 用 LLD 完全自动化监控 Oracle
- 【android学习笔记】与MYSQL交互之JSONARRAY
- 数据库系统的组成
- ORACLE PL/SQL 实例精解之第二章 通用编程语言基础
- PostgreSQL update from other table
- [参考]Oracle 11g的安装
- MySQL隔离级别
- Can’t connect to local MySQL server through socket 解决办法
- ORACLE AWR报告数据的导入导出实践
- SQLite清空表并将自增列归零
- ORACLE PL/SQL 实例精解之第一章 PL/SQL概念
- mysql操作
- Oracle数据库创建实例
- PL/SQL知识总结(1):PL/SQL块结构和流程控制语句
- MySQL联合查询
- MAC中Django中runserver提示Can't connect to local MySQL server through socket '/tmp/mysql.sock错误
- navicat for mysql 安装和使用
- Oracle常见错误:ORA-01403 的根本原因和解决方案