数据库连接池
- 作用:可以避免重复连接,提高程序的执行效率
c3p0数据库连接池
-
使用步骤:
-
导包
c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar
-
配置信息(c3p0-config.xml)
<c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <default-config> <!-- 连接参数 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://192.168.23.129:3306/db9</property> <property name="user">root</property> <property name="password">itheima</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/db15</property> <property name="user">root</property> <property name="password">itheima</property> <!-- 连接池参数 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">8</property> <property name="checkoutTimeout">1000</property> </named-config> </c3p0-config>
-
创建CombopooledDataSource
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(); } }
-
c3p0的测试
- 是否能够获取到maxPoolSize个连接
- 连接归还后是否还能获取
- 当连接超过最大连接时是否会报错
Druid
-
使用步骤
-
导包(导入druid包+mysql的驱动)
-
编写配置文件
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://192.168.23.129:3306/db9 username=root password=root # 初始化池子数量 initialSize=5 # 最大连接池数量 maxActive=10 # 超时等待时间 maxWait=3000
-
代码编写:
-
通过IO流来读取配置文件
//获取配置文件的流对象 InputStream is = DruidTest1.class.getClassLoader().getResourceAsStream("druid.properties"); //1.通过Properties集合,加载配置文件 Properties prop = new Properties(); prop.load(is);
-
通过DruidDataSourceFactory获取DataSource
//2.通过Druid连接池工厂类获取数据库连接池对象 DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //3.通过连接池对象获取数据库连接进行使用 Connection con = dataSource.getConnection();
-
-
数据库连接池工具类
-
目的:
- 把DataSource的创建步骤给省略
- 以后维护程序时只需要维护DataSource就可以了
-
步骤
-
写一个私有工具的构造方法
//1.私有构造方法 private DataSourceUtils(){}
-
在静态代码块中把DataSource初始化
-
通过IO流来读取配置文件
-
创建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(); } }
-
-
提供获取Connection的方法
//4.提供一个获取数据库连接的方法 public static Connection getConnection() { Connection con = null; try { con = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return con; }
-
释放资源
/* 5. 提供 释放资源的方法 */ public static void close(ResultSet rs, Statement stat, Connection conn) throws SQLException { if (rs != null) { rs.close(); } if (stat != null) { stat.close(); } if (conn != null) { conn.close(); } }
-
执行SQL可能的返回值类型
- int :执行增删改的时候,int代表影响的行数
- ResultSet: 让用户传入SQL,以及需要的类型
- 查询所有学生信息,(SQL, Student)
源/元信息
- 指数据库的结构类型(字段、数据类型)
JDBCTemplate
- JDBCTemplate是spring对JDBC的一种封装,便于执行sql语句
- JDBCTemplate执行sql语句:
- execute:可以执行所有的sql语句,一般用来执行DDL
- update:用来执行DML(insert、update、delete)
- Query:用来执行DQL的sql语句