使用c3p0连接池
首先我们需要知道为什么要使用连接池:因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒),连接就会被自动释放掉,每次新建连接都需要140毫秒左右的时间而C3P0连接池会池化连接,随时取用,平均每次取用只需要10-20毫秒,所以如果是很多客户端并发随机访问数据库的话,使用连接池的效率会高。
接下来我们看使用c3p0需要做那些准备:首先需要导入相对应的jar包:c3p0-0.9.1.2-jdk1.3.jar,然后就是链接数据库的配置文件:c3p0-config.xml,配置如下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- This is default config! -->
<default-config>
<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>
</default-config> <!-- This is my config for mysql-->
<named-config name="mysql">
<!-- 加载驱动 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 其中studio为数据库名称 -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/studio?useUnicode=true&characterEncoding=UTF8</property>
<!-- 连接用户名 -->
<property name="user">root</property>
<!-- 连接密码 -->
<property name="password"></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>
</named-config>
</c3p0-config>
接下来是c3p0链接数据库的工具类,调用此类之后我们就无需再手动关闭连接,代码如下
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Util {
static ComboPooledDataSource cpds=null;
static{
cpds = new ComboPooledDataSource("mysql");//这是mysql数据库
}
/**
* 获得数据库连接
*/
public static Connection getConnection(){
try {
return cpds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
} /**
* 数据库关闭操作
*/
public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
最后我们只需要在自己写的Dao层操作中获取到C3p0的连接就好了,这里我就只写一个查询的方法
public List<String> getSelect() {
// sql语句
String sql = "select * from user";
// 获取到连接
Connection conn = C3P0Util.getConnection();
PreparedStatement pst = null;
// 定义一个list用于接受数据库查询到的内容
List<String> list = new ArrayList<String>();
try {
pst = (PreparedStatement) conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
// 将查询出的内容添加到list中,其中userName为数据库中的字段名称
list.add(rs.getString("userName"));
}
} catch (Exception e) {
}
return list;
}
主要是第5行中获取链接的方式改变了,当然我们既然链接数据库就需要导入相对应的jar包,小伙伴可以自行百度
使用c3p0连接池的更多相关文章
- c3p0连接池]
<c3p0-config> <!-- 默认配置 --> <default-config> <property name="jdbcUrl" ...
- c3p0连接池获得的Connection执行close方法后是否真的销毁Connection对象?
问题描述: jfinal做的api系统中,在正常调用接口一段时间后,突然再调用接口的时候,该请求无响应api系统后台也无错误信息 (就是刚开始接口调用是正常的,突然就无响应了) 于是啊,就开始找错误. ...
- C3P0连接池在hibernate和spring中的配置
首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...
- C3P0连接池问题,APPARENT DEADLOCK!!! Creating emergency..... [问题点数:20分,结帖人lovekong]
采用c3p0连接池,每次调试程序,第一次访问时(Tomcat服务器重启后再访问)都会出现以下错误,然后连接库需要很长时间,最终是可以连上的,之后再访问就没问题了,请高手们会诊一下,希望能帮小弟解决此问 ...
- HQL查询及Hibernate对c3p0连接池的支持
//HQL查询 // auto-import要设置true,如果是false,写HQL时要指定类的全名 //查询全部列 Query query = session.createQuery(" ...
- C3P0连接池详解及配置
C3P0连接池详解及配置 本人使用的C3P0的jar包是:c3p0-0.9.1.jar <bean id = "dataSource" class = "com.m ...
- C3P0连接池详细配置
C3P0连接池详细配置 转自http://msq.javaeye.com/blog/60387 <c3p0-config> <default-config> <!--当连 ...
- Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用
Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...
- Spring框架中 配置c3p0连接池 完成对数据库的访问
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
随机推荐
- Using FastCGI to Host PHP Applications on IIS 7 -IIS7 怎么配置 PHP5
This article describes how to configure the FastCGI module and PHP to host PHP applications on IIS 7 ...
- Nginx和Tengine的详细安装图文教程(Linux下)
简洁安装 安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel 编译三步走./configure \ --prefix=/opt/sx ...
- 我的github
我的github:先来贴个图~ 这是我的github,新建了第一个repository,默认路径是aokoqingiz/code. 然后是里面的文件~ 里面有一个readme.txt,是我对这个r ...
- Spring EL Operators example
Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...
- HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...
- poj3687
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9028 Accepted: 2444 De ...
- 【Todo】ipcs命令学习
可以先看这一篇 http://www.jb51.net/article/40805.htm
- jquery 绑定省份和城市
前台代码: <asp:DropDownList runat="server" ID="ddlProvince"></asp:DropDownL ...
- MongoDB 快速入门--中级
索引 ensureIndex 用来创建索引,需要注意的就是一个集合最多也就64个索引 如果没加所有就是表扫表,速度很慢, 当然如果索引的键有多个,就必须考虑顺序 拓展索引 同样的也可以为内嵌文档 建立 ...
- php and web service with wsdl
Following are the links: Developing Web Services Using PHP PHP Web Services with WSDL Creating Web S ...