jdbc连接池和jdbcTemplate
一. jdbc连接池
- 程序和数据库之间的连接是底层技术,每一次连接都比较耗时,而用完后又得关闭连接释放资源,这样反复连接和释放其实是很浪费时间的,因此引出一个连接池技术,来管理这些连接对象。连接对象被使用完后,会归还给连接池,等待分配给下一次使用,而不是销毁。
1.就是一个容器
2.容器中装的是connection连接对象
3.这些连接对象创建后可以反复被使用,并不会销毁
4.它们是共享的
1. 常用的连接池
c3p0 hibernate 传统的orm框架
druid 国产的 阿里
2. 连接池的方法
1. ds.getConnection(); 获取连接对象 -- 传统的方式 DriverManager.getConnection();
2. con.close(); 表示归还连接对象给连接池(前提:con是从连接池获取的)
3. C3p0连接池的使用
- 准备工作
- 导jar包 – 两个连接池 一个数据库驱动 共3个
- 配置文件简介
1.导入两个c3p0的jar包+mysql驱动jar包,共3个
2.c3p0-config.xml 错误的写法:c3po-config.xml c3p0config.xml c3p0_config.xml
*文件必须正确的命名
*路径必须在src直接目录下,不能在其子目录
基本使用
<?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/db1</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><!--自定义配置--><name-config name="myc3p0"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</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></name-config>
</c3p0-config>
- 注意事项:
c3p0-config.xml 文件名必须这么命名c3p0-config.xml 文件必须放在src目录下,不能放在子目录
1.maxPoolSize 最大连接对象的个数
2.checkoutTimeout 最大的报错时间
3.named-config 指定名称配置
*如果 new CombopooledDataSource(“abc”)
-
可能会出错的地方
- 配置文件中书写错误
- 文件名或路径错误
- 数据库连接四大参数书写错误,或多加空格
- 忘记导入驱动jar包
@Test //测试连接池参数public void test2() throws SQLException {//创建连接池数据源对象ComboPooledDataSource ds = new ComboPooledDataSource();//获得链接对象Connection con = ds.getConnection();//输出链接对象的地址值表示连接正常System.out.println(con);}
4. druid连接池
4.1 基本使用
-
导入jar包-- 1个
-
定义配置文件 druid.properties
-
代码演示
public class Test2 {@Test //不用配置文件public void test1() throws SQLException {//创建连接池对象DruidDataSource ds = new DruidDataSource();ds.setDriverClassName("com.mysql.jdbc.Driver");ds.setUrl("jdbc:mysql://localhost:3306/db1");ds.setUsername("root");ds.setPassword("123456");//从连接池中获取对象System.out.println(ds.getConnection());}@Test //用配置文件public void test2() throws Exception {Properties p = new Properties();p.load(new FileInputStream("src/druid.properties"));DataSource ds = DruidDataSourceFactory.createDataSource(p);System.out.println(ds.getConnection());}
}
4.2 工具类
public class JdbcUtil {private static DataSource ds;/*** 文件的读取,只需要读取一次即可拿到这些值。使用静态代码块*/static{//读取资源文件,获取值。try {FileInputStream is = new FileInputStream("E:\\Javaweb\\day06-web\\src\\druid.properties");Properties p = new Properties();p.load(is);//创建数据源 给成员属性 ds赋值ds = DruidDataSourceFactory.createDataSource(p);//System.out.println(p);} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {System.out.println(getDs());}/*** 获取连接* @return 连接对象*/public static Connection getConnection() {try {return ds.getConnection();} catch (SQLException e) {e.printStackTrace();}return null;}//获取连接池对象 dspublic static DataSource getDs(){return ds;}/*** 释放资源* @param stmt* @param conn*/public static void close(ResultSet rs, Statement stmt, Connection conn){if( rs != null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if( stmt != null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if( conn != null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
4.3 工具类测试
@Test //修改的金额为5000public void test8(){//创建jdbcTemplate对象JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());//定义sqlint update = template.update("update account set balance=5000 where id=?",1);System.out.println(update);}
二.jdbcTemplate
1. 简介
Spring框架对jdbc的简单封装,提供了一个JdbcTemplate对象- jdbcTemplate的若干方法
update执行 dml增删改queryForMap() 查询结果,将结果集封装为map集合queryForList() 查询结果,将结果集封装为list集合queryForObject() 查询结果集,将结果集封装成某个数据类型query() 查询结果集,将结果集封装为Javabean对象
1.导包 spring的5个jar包 + mysql的驱动 + druid连接池包1个= 7个
2.使用jdbcTemplate对象,该对象的创建需要连接池对象ds
JdbcTemplate jdbcTemplate=new JdbcTemplate(DruidUtil.getDataSource());
jdbcTemplate.update();//dml
jdbcTemplate.query();
jdbcTemplate.queryForMap();
jdbcTemplate.queryForList();
jdbcTemplate.queryForObject();
2.入门
@Test //修改的金额为5000public void test8(){//创建jdbcTemplate对象JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());//定义sqlint update = template.update("update account set balance=5000 where id=?",1);System.out.println(update);}
3. jdbcTemplate综合练习
public class TestJdbcTemplate {@Test //修改的金额为5000public void test8(){//创建jdbcTemplate对象JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());//定义sqlint update = template.update("update account set balance=5000 where id=?",1);System.out.println(update);}@Testpublic void update(){//创建简化操作模板JdbcTemplate template = new JdbcTemplate(JdbcUtil.getDs());//调用updateint i = template.update("update dept set name=? where id=?", "教学部", 5);System.out.println(i);//受影响的行数 i}@Test //查询所有,封装成List中泛型为Mappublic void test2(){JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());List<Map<String, Object>> list = template.queryForList("select * from emp");System.out.println(list);}@Test //新增public void test3(){JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());int c = template.update("insert users values (null,?,?)", "曹操", "123123");System.out.println(c);}@Test //修改public void test4(){JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());int d = template.update("update users set NAME=? where id=?", "貂蝉", 3);System.out.println(d);}@Test //删除public void test5(){JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());int d = template.update("delete from users where id=?", 3);System.out.println(d);}@Test //查询一条 封装mappublic void test6(){JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());Map<String, Object> map = template.queryForMap("select * from users where id=?", 2);System.out.println(map);}@Test //查询一条 封装javaBeanpublic void test7(){JdbcTemplate template = new JdbcTemplate(com.JdbcUtil.getDs());Users users = template.queryForObject("select * from users where id=?", new BeanPropertyRowMapper<Users>(Users.class), 1);System.out.println(users);}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
