目录
-
数据库连接池
- 有点类似于线程连接池
- 使用完毕不是释放,而是归还给连接池
- 是一个接口
- 由数据库厂商来实现接口
- 方法:
- 归还连接:
- Connection.close():增强的方法,不是关闭,而是归还
- 归还连接:
-
Druid(德鲁伊):
- 由阿里巴巴提供,最牛逼的数据库连接池
- 使用步骤:
//1、导入jar包 //2、加载配置文件 Properties properties = new Properties(); properties.load(Main01.class.getResourceAsStream("druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); Connection connection = dataSource.getConnection();
- 导入jar包:
- 定义配置文件
- 必须是properties形式的,名字可以自己指定
- 可以放在任意位置
- 加载配置文件
- 需要用到属性集和类加载器
- 创建连接池对象
- 是使用德鲁伊连接池工厂来获取连接池对象
- 需要传递参数,参数为:属性集对象
- 是使用德鲁伊连接池工厂来获取连接池对象
- 获取链接
- druid工具类:
package com.lq.demo01_dataSource; 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; /* author: lq time: 2020-03-04 17:21 */ public class JDBC_tools { private static DataSource dataSource; static { Properties properties = new Properties(); try { //获取配置文件 properties.load(JDBC_tools.class.getResourceAsStream("druid.properties")); dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } //获取连接池 public static DataSource getDataSource() { return dataSource; } //获取连接对象 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } //释放资源 public static void close(Connection connection, Statement statement) { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection connection, Statement statement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } close(connection, statement); } }
-
c3p0:
- 不常用
- 使用步骤:
- 导入jar包:
- 需要导入两个包,因为其中一个是另一个的依赖
- 定义配置文件
- 不推荐使用硬编码的形式,推荐使用配置文件的形式
- 配置文件的名字是确定的,不能自己改变
- c3p0.properties 或者 c3p0-config.xml
- 创建核心对象
- 获取链接
- 导入jar包:
- 也可以使用其他配置
- DataSource ds = new ComboPooledDataSource("...");
- 传入对应的配置的名字即可
- 不过一般还是推荐使用默认配置
- DataSource ds = new ComboPooledDataSource("...");
-
JDBCTemplate
- 创建JDBCTemplate对象时需要向里面传递一个参数,这个参数是一个连接池对象
- 代码:
JdbcTemplate template= new JdbcTemplate(JDBC_tools.getDataSource()); String sql="update dept set dname = ? where id = ?"; int update = template.update(sql,"123",60); System.out.println(update);
-
主要方法:
-
update():执行 DML 语句。增、删、改语句
-
代码:
String sql = "update emp set salary = 10000 where id = 1001"; int update = template.update(sql); Assert.assertEquals(1, update);
-
-
queryForMap():查询结果将结果集封装为 map 集合,将列名作为 key,将值作为 value 将这条记录封
装为一个 map 集合-
这个方法查询的结果集长度只能是 1
-
代码:
String sql = "select * from emp where id=?"; Map<String, Object> map = template.queryForMap(sql, 1002); System.out.println(map);
-
-
queryForList():查询结果将结果集封装为 list 集合
-
将每一条记录封装为一个 Map 集合,再将 Map 集合装载到 List 集合中
-
一般也不用这种方法
-
代码:
String sql = "select * from emp"; List<Map<String, Object>> maps = template.queryForList(sql); for (Map<String, Object> map : maps) { System.out.println(map); }
-
-
query():查询结果,将结果封装为 JavaBean 对象
-
将每一条记录封装为一个 JavaBean对象,再将 Map 集合装载到 List 集合中
-
query 的参数:RowMapper
-
可以自己实现RowMapper接口,不推荐
-
代码:
String sql = "select * from emp"; List<Emp> empList = template.query(sql, new RowMapper<Emp>() { @Override public Emp mapRow(ResultSet resultSet, int i) throws SQLException { int id = resultSet.getInt("id"); int job_id = resultSet.getInt("job_id"); int mgr = resultSet.getInt("mgr"); int dept_id = resultSet.getInt("dept_id"); String ename = resultSet.getString("ename"); double salary = resultSet.getDouble("salary"); double bonus = resultSet.getDouble("bonus"); Date joindate = resultSet.getDate("joindate"); Emp emp = new Emp(id, ename, job_id, mgr, joindate, salary, bonus, dept_id); return emp; } }); for (Emp emp : empList) { System.out.println(emp); }
-
-
一般我们使用 BeanPropertyRowMapper 实现类。可以完成数据到 JavaBean 的自动封装
-
new BeanPropertyRowMapper<类型>(类型.class)
-
代码:
String sql = "select * from emp"; List<Emp> empList = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class)); for (Emp emp : empList) { System.out.println(emp); }
-
如果select有参数,直接写进去就行了
List<Emp> empList = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class),"p1","p2");
-
-
-
JavaBean中的成员变量不能是基本数据类型,因为基本数据类型不能设置控制,而查询出来的结果是有可能为空的,可以使用基本数据类型的包装类
-
JavaBean中的成员变量的名字必须和记录的字段名一一对应
-
-
queryForObject():查询结果,将结果封装为对象
-
一般用于聚合函数的查询
-
代码:
String sql = "select count(id) from emp"; Long aLong = template.queryForObject(sql, Long.class); System.out.println(aLong);
-
-