目录
连接池
概念:类似于常量池、线程池,指在关闭连接后不回收连接,而是放回连接池等待下一次调用,减少内存消耗
C3P0
准备步骤:
- 导入jar包
- 编写c3p0-config.xml配置文件(文件放置的位置一定要在src目录下!并且必须命名为c3p0-config.xml !)
<!--驱动、url、账号、密码一定要填--> <c3p0-config> <default-config> <property name="driverclass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/java2206?serverTimezone=GMT</property> <property name="user">root</property> <property name="password">123456</property> </default-config> <!-- 下面是自定义配置,选填 --> <!-- <named-config name="intergalactoApp">--> <!-- <property name="acquireIncrement">50</property>--> <!-- <property name="initialPoolSize">100</property>--> <!-- <property name="minPoolSize">50</property>--> <!-- <property name="maxPoolSize">1000</property>--> <!-- <!– intergalactoApp adopts a different approach to configuring statement caching –>--> <!-- <property name="maxStatements">0</property>--> <!-- <property name="maxStatementsPerConnection">5</property>--> <!-- <!– he's important, but there's only one of him –>--> <!-- <user-overrides user="master-of-the-universe">--> <!-- <property name="acquireIncrement">1</property>--> <!-- <property name="initialPoolSize">1</property>--> <!-- <property name="minPoolSize">1</property>--> <!-- <property name="maxPoolSize">5</property>--> <!-- <property name="maxStatementsPerConnection">50</property>--> <!-- </user-overrides>--> <!-- </named-config>--> </c3p0-config>
- 创建ComboPoolDataSource连接池对象(使用单例模式)
ComboPooledDataSource cpds = new ComboPooledDataSource();
- 调用连接池对象的getConnection()方法获得Connection对象
try { Connection conn = cpds.getConnection(); } catch (SQLException throwables) { return null; }
Druid
- 导入jar包
- 编写.propreties文件(文件名和存放位置任意)
##驱动、url、账号、密码一定不能错 driverClassName = com.mysql.cj.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/java2206?serverTimezone=GMT username = root password = 123456
- 创建Propreties对象查找.propreties配置
Properties prop = new Properties(); prop.load(DruidUtil.class.getResourceAsStream("druid.properties"));//配置文件路径
- 用DruidDataSourceFactory.createDataSource()创建dataSource连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
- 调用连接池对象的getConnection()方法获得Connectiondui对象
try { Connection conn = dataSource.getConnection(); } catch (SQLException throwables) { return null; }
Template
功能:自动解析一个对象的set和get方法,根据set/get方法找到结果集里对应的字段赋值,将每一条记录都转化成一个对象,注意:类定义时要赋值的变量名一定要和数据库中对应的字段名同名!不同名会导致赋值为null
query()
利用sql语句和BeanPropertyRowMapper<>()查找,每条记录变成一个类对象,将结果以List形式返回,当没有结果查询到时返回一个空List
JdbcTemplate template = new JdbcTemplate(C3p0Util.getDataSource());//传入一个连接池
String sql = "select * from emp";
List<Emp> list= template.query(sql,new BeanPropertyRowMapper<>(Emp.class));//必须要填写类的字节码文件,作为返回的List的泛型
list.forEach(System.out::println);
System.out.println(list.size());
update()
利用sql语句对表格进行增删改,可在最后用形参列表添加元素
String sql = "insert into jdbctest values(?,?,?,?)";
template.update(sql,1002,"男","小王","2022-8-9");
queryForObject()
当结果是单行单列或者单条记录时使用,返回单个对象,当没有结果查询到时报错
String sql = "select ename from emp where empno = ?";
String emp = template.queryForObject(sql, String.class, 7369);
//String sql = "select * from emp where empno = ?";
//Emp emp = template.queryForObject(sql, new BeanPropertyRowMapper<>(Emp.class), 7369);
System.out.println(emp);
总结
- 连接池的目的是为了节省内存,通常一个程序不会有多个连接池
- c3p0和druidi效果一样
- template可以节省很多步骤,但一定要使变量名和数据库中对应字段同名