赞
踩
目录
- <c3p0-config>
- <!-- 使用默认的配置读取连接池对象 -->
- <default-config>
- <!-- 连接参数 -->
- <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/dp1</property>
- <property name="user">root</property>
- <property name="password">123456</property>
-
- <!-- 连接池参数 -->
- <!-- 初始连接数 -->
- <property name="initialPoolSize">5</property>
- <!-- 最大连接数 -->
- <property name="maxPoolSize">10</property>
- <!-- 最大等待时间 -->
- <property name="checkoutTimeout">3000</property>
- </default-config>
-
- <!-- 创建对象传参为otherc3p0时读取下面的配置信息 -->
- <named-config name="otherc3p0">
- <!-- 连接参数 -->
- <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/dp1</property>
- <property name="user">root</property>
- <property name="password">123456</property>
-
- <!-- 连接池参数 -->
- <property name="initialPoolSize">5</property>
- <property name="maxPoolSize">8</property>
- <property name="checkoutTimeout">1000</property>
- </named-config>
- </c3p0-config>
- package demo02.c3p0demo;
-
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- import javax.sql.DataSource;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- public class c3p0Test1 {
- public static void main(String[] args) throws Exception{
- //1.创建c3p0的数据库连接池对象
- DataSource dataSource = new ComboPooledDataSource();
- //2.通过连接池对象获取数据库连接
- Connection con = dataSource.getConnection();
- //3.查询学生表的全部信息
- String sql = "SELECT * FROM student";
- PreparedStatement pst = con.prepareStatement(sql);
- //4.执行sql语句,接收结果集
- ResultSet rs = pst.executeQuery();
- //5.处理结果集
- while(rs.next()){
- System.out.println(rs.getInt("sid")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")+"\t"+rs.getDate("birthday"));
- }
- //6.释放资源
- rs.close();
- pst.close();
- con.close();//用完以后,进行归还
- }
- }
- driverClassName=com.mysql.cj.jdbc.Driver
- url=jdbc:mysql://localhost:3306/dp1
- username=root
- password=123456
- initialSize=5
- maxActive=10
- maxWait=3000
- package demo02.druiddemo;
-
- import com.alibaba.druid.pool.DruidDataSourceFactory;
-
- import javax.sql.DataSource;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Properties;
-
- public class druidTest {
- public static void main(String[] args) throws Exception{
- //获取配置文件的流对象
- InputStream is = druidTest.class.getClassLoader().getResourceAsStream("druid.properties");
- //1.通过Properties集合加载配置文件
- Properties prop = new Properties();
- prop.load(is);
- //2.通过Druid连接池工厂类获取数据库连接池对象
- DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
- //3.通过连接池对象获取数据库连接进行使用
- Connection con = dataSource.getConnection();
- //4.查询学生表的全部信息
- String sql = "SELECT * FROM student";
- PreparedStatement pst = con.prepareStatement(sql);
- //5.执行sql语句,接收结果集
- ResultSet rs = pst.executeQuery();
- //6.处理结果集
- while(rs.next()){
- System.out.println(rs.getInt("sid")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")+"\t"+rs.getDate("birthday"));
- }
- //6.释放资源
- rs.close();
- pst.close();
- con.close();//用完以后,进行归还
- }
- }
- package demo02.utils;
-
- import com.alibaba.druid.pool.DruidDataSourceFactory;
-
- import javax.sql.DataSource;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
-
- public class DataSourceUtils {
- //1.私有构造方法
- private DataSourceUtils(){}
- //2.声明数据源变量
- private static DataSource dataSource;
- //3.提供静态代码块,完成配置文件的加载
- static{
- try {
- //完成配置文件的加载
- InputStream is = DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties");
- Properties prop = new Properties();
- prop.load(is);
- //获取数据库连接池对象
- dataSource = DruidDataSourceFactory.createDataSource(prop);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //4.提供一个获取数据库连接的方法
- public static Connection getConnection(){
- Connection con = null;
- try {
- con = dataSource.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return con;
- }
- //5.提供一个获取数据库连接池对象的方法
- public static DataSource getDataSource(){
- return dataSource;
- }
- //6.释放资源
- public static void close(Connection con, Statement stat, ResultSet rs) {
- if(con != null) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- if(stat != null) {
- try {
- stat.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- if(rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
-
- public static void close(Connection con, Statement stat) {
- if(con != null) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- if(stat != null) {
- try {
- stat.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- package demo02.druiddemo;
-
- import demo02.utils.DataSourceUtils;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- public class druidTest2 {
- public static void main(String[] args) throws Exception{
- //1.通过连接池工具类获取一个数据库连接
- Connection con = DataSourceUtils.getConnection();
- //2.查询学生表的全部信息
- String sql = "SELECT * FROM student";
- PreparedStatement pst = con.prepareStatement(sql);
- //3.执行sql语句,接收结果集
- ResultSet rs = pst.executeQuery();
- //4.处理结果集
- while(rs.next()){
- System.out.println(rs.getInt("sid")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")+"\t"+rs.getDate("birthday"));
- }
- //5.释放资源
- DataSourceUtils.close(con,pst,rs);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。