C3P0连接池
C3P0连接池是一种主流连接池,C3P0连接池需要一些jar包还有一些配置文件。这里面我提供给大家一个下载地址:C3P0支持
大家把三个jar包构建到lib中,把xml配置文件放在src文件夹下面
如果大家忘记怎么引入jar包了请看我写的这一篇博客IDE引入jar包
C3P0应用
正常你在你的项目中安装好C3P0后是这样的:
打开c3p0-config.xml文件(进行配置)
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1/af_school?useUnicode=true&characterEncoding=UTF-8</property>
<property name="user">root</property>
<property name="password"></property>
<property name="initialPoolSize">2</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="idleConnectionTestPeriod">200</property>
</default-config>
</c3p0-config>
大家可以对照上面的模板进行修改(修改数据库、用户名、密码啥的)
然后我们看一段使用代码:
简单C3P0使用
package my;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class Test
{
public static void testQuery() throws Exception
{
// 一个DataSource指代一个数据源, 内部有一个连接池, 自动读取 c3p0-config.xml的配置
ComboPooledDataSource pool = new ComboPooledDataSource();
// 一个Connection代表一次访问
Connection conn = pool.getConnection();
// 查询操作
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
while(rs.next())
{
// 取出这一行记录
int id = rs.getInt("id");
String name = rs.getString("name");
String phone = rs.getString("phone"); // 可能为null
Date birthday = rs.getDate("birthday");
System.out.println(id + "\t" + name + "\t" + phone );
}
conn.close(); // 连接放回池子
pool.close(); // 数据源如果关闭,相应的池子也就销毁了
}
public static void main(String[] args)
{
try
{
testQuery();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
如果你想用我这个数据库做练习的话:
我给下SQL语句,大家在MySQL上运行下就有这个af_school数据库了
/*
SQLyog Ultimate v11.24 (32 bit)
MySQL - 5.6.39 : Database - af_school
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`af_school` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `af_school`;
/*Table structure for table `admin` */
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
`username` varchar(32) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `admin` */
insert into `admin`(`username`,`password`) values ('shaofa','123456');
/*Table structure for table `book` */
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`author` varchar(32) DEFAULT NULL,
`title` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `book` */
/*Table structure for table `exam` */
DROP TABLE IF EXISTS `exam`;
CREATE TABLE `exam` (
`id` int(11) NOT NULL COMMENT '学号',
`chinese` int(11) DEFAULT NULL COMMENT '语文成绩',
`english` int(11) DEFAULT NULL COMMENT '英语成绩',
`math` int(11) DEFAULT NULL COMMENT '数学成线',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `exam` */
insert into `exam`(`id`,`chinese`,`english`,`math`) values (20180001,89,90,98),(20180002,78,82,93),(20180003,90,73,95),(20180004,88,98,83),(20180005,96,79,75),(20180006,77,98,82);
/*Table structure for table `leave_event` */
DROP TABLE IF EXISTS `leave_event`;
CREATE TABLE `leave_event` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '请假记录ID',
`stuId` int(11) NOT NULL COMMENT '学生ID',
`daysFrom` date DEFAULT NULL COMMENT '哪天开始',
`daysTo` date DEFAULT NULL COMMENT '哪天结束',
`type` tinyint(4) DEFAULT NULL COMMENT '类型,0病假,1事假',
`reason` varchar(256) DEFAULT NULL COMMENT '事由',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
/*Data for the table `leave_event` */
insert into `leave_event`(`id`,`stuId`,`daysFrom`,`daysTo`,`type`,`reason`) values (1,20180001,'2018-01-02','2018-01-03',0,'感冒'),(2,20180001,'2018-03-06','2018-03-08',0,'发烧'),(3,20180003,'2018-03-01','2018-03-07',1,'出国旅游'),(4,20180005,'2018-03-07','2018-03-07',1,'家里有事'),(5,20180003,'2018-03-17','2018-03-18',0,'不舒服'),(7,20190001,'2019-07-05','2019-07-10',1,'休息');
/*Table structure for table `student` */
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL COMMENT '学号',
`name` varchar(32) NOT NULL COMMENT '姓名',
`sex` tinyint(1) DEFAULT NULL COMMENT '性别',
`phone` varchar(16) DEFAULT '13800000000' COMMENT '手机号',
`birthday` date DEFAULT NULL COMMENT '生日',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `student` */
insert into `student`(`id`,`name`,`sex`,`phone`,`birthday`) values (20180001,'盖聂',1,'1409900089','1982-03-09'),(20180002,'卫庄',1,'1282399999','1993-10-01'),(20180003,'张良',NULL,NULL,'1996-03-11'),(20180004,'伏念',1,NULL,'1982-09-24'),(20180005,'颜路',1,'13699292899','1983-11-21'),(20180006,'赤练',0,'13819289890','1998-03-12'),(20180007,'端木蓉',0,'13800000000','1978-05-12'),(20180008,'盗跖',1,'13410012908','1993-09-10'),(20180009,'白凤',1,'13509890090','1994-04-20'),(20180010,'天明',1,'18799891829','2002-04-19'),(20180011,'月儿',0,'13882938990','2003-06-10');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
更多的C3P0技术请参考:
官方网址:C3P0官网
学习文档:C3P0配置文档(邵发老师)