tlsmile 2020-06-03
<?xml version="1.0" encoding="utf-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db3</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> <named-config name="otherc3p0"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db3</property> <property name="user">root</property> <property name="password">123456</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">8</property> <property name="checkoutTimeout">3000</property> </named-config> </c3p0-config>
package cn.itcast.datasource.c3p0; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class C3P0Demo1 { public static void main(String[] args){ //创建数据库连接池对象 DataSource dataSource=new ComboPooledDataSource(); //获取连接对象 try { Connection connection=dataSource.getConnection(); System.out.println(connection); } catch (SQLException e) { e.printStackTrace(); } //打印 } }
1.导入jar包 druid-1.0.9 2.定义配置文件 * 是properties形式的 * 可以叫任意名称,可以放在任意目录下 3.加载配置文件,Properties 4.获取数据库连接池对象:通过工厂类来获取 DruidDataSourceFactory 5.获取连接:getConnection
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8 username=root password=123456 # 初始化连接数量 initialSize=5 # 最大连接数 maxActive=10 # 最大等待时间 maxWait=3000 # maxIdle=10 minIdle=3
package cn.itcast.datasource.druid; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.util.Properties; public class DruidDemo { public static void main(String[] args) throws Exception { //导入jar包 //定义配置文件 //加载配置文件 Properties properties=new Properties(); InputStream inputStream=DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); properties.load(inputStream); //获取连接池对象 DataSource dataSource= DruidDataSourceFactory.createDataSource(properties); //获取连接 Connection connection=dataSource.getConnection(); System.out.println(connection); } }
---------定义一个类 JDBCUtils-------------------------- package cn.itcast.utils; import cn.itcast.datasource.druid.DruidDemo; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JDBCUtils { //定义成员变量 dataSource private static DataSource dataSource; static{ try { //定义配置文件 //加载配置文件 Properties properties=new Properties(); // InputStream inputStream= DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); // properties.load(inputStream); properties.load( DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties")); //获取连接池对象 dataSource= DruidDataSourceFactory.createDataSource(properties); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } //获取连接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } //释放资源 public static void close(Statement statement,Connection connection){ // if(statement!=null){ // try { // statement.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // if(connection!=null){ // try { // connection.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } close(null,statement,connection); } public static void close(ResultSet resultSet, Statement statement, Connection connection){ if(resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } //获取连接池方法 public static DataSource getDataSource(){ return dataSource; } } -------------提供静态代码块加载配置文件,初始化连接对象----------------------- driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8 username=root password=123456 # 初始化连接数量 initialSize=5 # 最大连接数 maxActive=10 # 最大等待时间 maxWait=3000 # maxIdle=10 minIdle=3 -----------------提供方法---------------------------------------------------- 1. 获取连接方法:通过数据库连接池获取连接 2. 释放资源 3. 获取连接池的方法 package cn.itcast.datasource.druid; import cn.itcast.utils.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; public class DruidDemo2 { public static void main(String[] args) throws Exception { //完成添加操作。给account表添加一条记录 //获取连接 Connection connection= JDBCUtils.getConnection(); //定义SQL String sql="insert into account values(null,?,?)"; //获取pstmt对象 PreparedStatement preparedStatement=connection.prepareStatement(sql); //给?赋值 preparedStatement.setString(1,"wangwu"); preparedStatement.setDouble(2,3000); //执行SQL int count=preparedStatement.executeUpdate(); //打印 System.out.println(count); } }