1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【JavaWeb】JDBC优化 之 数据库连接池 Spring JDBC

【JavaWeb】JDBC优化 之 数据库连接池 Spring JDBC

时间:2022-09-18 02:15:47

相关推荐

【JavaWeb】JDBC优化 之 数据库连接池 Spring JDBC

1 数据库连接池

为什么要使用数据库连接池?

数据库连接是一件费时的操作,连接池可以使多个操作共享一个连接使用连接池可以提高对数据库连接资源的管理节约资源且高效

概念:数据库连接池其实就是一个容器,存放数据库连接的容器。当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户访问数据库时,从容器中获取连接对象,用户访问完后,将连接对象归还给数据库。

实现:

标准接口:DataSource:javax.sql包下。使用getConnection()获取连接,如果连接对象是从连接池中获取的。调用Connection.close方法则不会关闭连接,而是归还连接。一般不去实现它,由数据库厂商来实现。实现技术由:C3P0和Druid【阿里巴巴提供 最好的数据库连接池实现技术】

2 C3P0的使用

步骤

导入两个c3p0的jar包

mchange-commons-java-0.2.12

c3p0-0.9.5.2

还需要数据库驱动jar包定义配置文件

名称:c3p0.properties或者c3p0-config.xml

路径:直接将文件放在src目录下即可创建核心对象,数据库连接池对象 ComboPooledDataSource获取连接:getConnection

public static void main(String[] args) throws SQLException {DataSource ds = new ComboPooledDataSource();ds.getConnection();System.out.println(ds);}

3 Druid的使用

步骤

导入druid-1.0.9.jar包定义配置文件:druid.properties,可以叫任意名称,可以放在任意目录获取数据库连接池对象,通过工厂类DruidDataSourceFactory获取获取连接:getConnection

public class JDBCUtils {//1.定义成员变量 DataSourceprivate static DataSource ds ;static{try {//1.加载配置文件Properties pro = new Properties();pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));//2.获取DataSourceds = DruidDataSourceFactory.createDataSource(pro);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}/*** 获取连接*/public static Connection getConnection() throws SQLException {return ds.getConnection();}/*** 释放资源*/public static void close(Statement stmt,Connection conn){close(null,stmt,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();}}}/*** 获取连接池方法*/public static DataSource getDataSource(){return ds;}}

4 Spring JDBC

Spring框架对JDBC的简单封装

提供了一个JDBCTemplate对象来简化JDBC的开发

步骤:

导入jar包

创建JdbcTemplate对象,依赖于数据源DataSource使用JdbcTemplate对象的方法来完成CRUD的操作update():执行DML语句增删改qureyForMap:查询结果并将结果集封装为map集合

注意:这个方法查询的结果集长度只能是1qureyForList:查询结果并将结果集封装为list集合

注意:将每一条记录封装为Map集合,再将Map集合装载到List集合中qurey():查询结果,将结果封装为JavaBean对象

注意:qurey的参数:RowMapper。一般使用BeanPropertyRowMapper实现类,可以完成数据到JavaBean的自动封装。

-new BeanPropertyRowMapper<类型>(类型.class)qureyForObject:查询结果,将结果封装为对象

注意:一般用于聚合函数的查询

4.1 JDBC Template入门使用

已经导入的数据库连接jar包JDBCUtils是用druid实现的工具类,druid包也需要导入导入JDBC Template的五个jar包

import org.springframework.jdbc.core.JdbcTemplate;public class JDBCTemplateDemo {public static void main(String[] args) {JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());String sql = "update account set balance=5000 where id=?";int count = template.update(sql, 1);System.out.println(count);}}

4.2 JDBCTemplate执行DML语句

修改1号数据的balance为10000添加一条记录删除第2步添加的记录查询id为1的记录,将结果封装为map集合查询所有记录,将结果封装为list集合查询所有记录,将结果封装为Account对象的list集合查询总记录数

public class JDBCTemplateDemo2 {private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());//更新记录@Testpublic void test1(){String sql = "update account set balance=? where id=?";int count = template.update(sql,999,1);Assert.assertEquals(1,count);}//添加记录@Testpublic void test2(){String sql = "insert into account values(null,?,?)";int count = template.update(sql, "赵四", 888);Assert.assertEquals(1,count);}//删除记录@Testpublic void test3(){String sql = "delete from account where id = ?";int count = template.update(sql, 4);Assert.assertEquals(1,count);}//查询id为1的记录 封装为map//注意queryForMap查询的结果集长度只能是1@Testpublic void test4(){String sql = "select * from account where id = ?";Map<String, Object> map = template.queryForMap(sql,1);System.out.println(map);}@Test//查询所有的记录 封装到list中 list中是map对应的一条条结果public void test5(){String sql = "select * from account";List<Map<String, Object>> maps = template.queryForList(sql);System.out.println(maps);}@Test//将结果封装为类再存入list中public void test6(){String sql = "select * from account";List<Account> query = template.query(sql, new BeanPropertyRowMapper<Account>(Account.class));for(Account acc: query){System.out.println(acc);}}//查询总记录数@Testpublic void test7(){String sql = "select count(id) from account";Long total = template.queryForObject(sql, Long.class);System.out.println(total);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。