jdbc-连接池的简单使用
2017-05-24 18:36
357 查看
/*这里使用的是c3p0的连接池,使用的jar包为c3p0-0.9.1.2.jar,使用的数据库为oracle*/
下面直接上代码:
连接池的设置代码
public class ConnPool {
private static ComboPooledDataSource dataSource;//数据源
static {
try {
dataSource=new ComboPooledDataSource();
//参数设置
dataSource.setUser("scott");//用户名
dataSource.setPassword("tiger");//密码
dataSource.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:xe");//连接字符串
dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");//驱动类
//连接池设置
dataSource.setInitialPoolSize(3);//连接数
dataSource.setMinPoolSize(3);//连接池最少保留的连接数
dataSource.setMaxPoolSize(3);//连接池最多的连接数上限
dataSource.setMaxIdleTime(60);//最大空闲时间60秒
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通过连接池对象返回数据库连接
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
}
使用连接池:
/**
* 测试连接池
* @author Administrator
*
*/
public class TestConnPool {
public static void main(String[] args) throws SQLException {
Connection conn1=ConnPool.getConnection();
System.out.println("conn1获得连接");
Connection conn2=ConnPool.getConnection();
System.out.println("conn2获得连接");
Connection conn3=ConnPool.getConnection();
System.out.println("conn3获得连接");
//在这里当我们创建第4个连接时,超过了我们设置的连接池的数量,模拟一个等待的时间,5秒后连接1会断开,这时连接4才能获得连接
new Thread(){
public void run(){
try {
Thread.sleep(5000);
conn1.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
Connection conn4=ConnPool.getConnection();
System.out.println("conn4获得连接");
}
}
下面直接上代码:
连接池的设置代码
public class ConnPool {
private static ComboPooledDataSource dataSource;//数据源
static {
try {
dataSource=new ComboPooledDataSource();
//参数设置
dataSource.setUser("scott");//用户名
dataSource.setPassword("tiger");//密码
dataSource.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:xe");//连接字符串
dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");//驱动类
//连接池设置
dataSource.setInitialPoolSize(3);//连接数
dataSource.setMinPoolSize(3);//连接池最少保留的连接数
dataSource.setMaxPoolSize(3);//连接池最多的连接数上限
dataSource.setMaxIdleTime(60);//最大空闲时间60秒
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通过连接池对象返回数据库连接
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
}
使用连接池:
/**
* 测试连接池
* @author Administrator
*
*/
public class TestConnPool {
public static void main(String[] args) throws SQLException {
Connection conn1=ConnPool.getConnection();
System.out.println("conn1获得连接");
Connection conn2=ConnPool.getConnection();
System.out.println("conn2获得连接");
Connection conn3=ConnPool.getConnection();
System.out.println("conn3获得连接");
//在这里当我们创建第4个连接时,超过了我们设置的连接池的数量,模拟一个等待的时间,5秒后连接1会断开,这时连接4才能获得连接
new Thread(){
public void run(){
try {
Thread.sleep(5000);
conn1.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
Connection conn4=ConnPool.getConnection();
System.out.println("conn4获得连接");
}
}
相关文章推荐
- JDBC之连接池DBCP与c3p0的简单使用
- C#使用简单的连接池完成P2P聊天程序(核心代码)
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- JDBC中连接池的使用
- java使用jdbc查询mysql数据最简单的例子
- Tomcat jdbc pool - Tomcat 7 新的数据库连接池使用教程
- oracle occi代码连接池的简单使用
- 封装JDBC,简单快捷的使用PreparedStatement对象
- 31、最简单的mvc框架tiny,增加Ioc,jdbc工具类(1个类),连接池(1个类)
- 在JDBC和Spring中使用proxool连接池
- Spring -- spring 中使用jdbc, c3p0连接池, dao概念
- 在JDBC、Spring或者Hibernate中使用proxool连接池
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 简单JDBC连接池的实现
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null' 及can not load org.gjt.mm.mysql.Driver)
- JDBC 查询日志变得简单 使用增强型PreparedStatement向JDBC代码增加日志功能
- 使用 Tomcat 7 新的连接池 —— Tomcat jdbc pool
- 第十七天dbutils的使用------Commons DbUtils(Apache)第三方的:只是对JDBC编码进行了简单的封装