Druid 连接池 JDBCUtils 工具类的使用
来源:互联网 发布:知乎什么时候成立的 编辑:程序博客网 时间:2024/06/06 01:34
Druid工具介绍
它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser。 支持所有JDBC兼容的数据库,包括Oracle、MySQL、Derby、Postgresql、SQL Server、H2等等。 Druid针对oracle和mysql做了特别优化,比如Oracle的PS Cache内存占用优化,MySql的ping检测优化。Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL的完整支持,这是一个手写的高性能SQL Parser,支持Visitor模式,使得分析SQL的抽象语法树很方便。简单SQL语句用时10微秒以内,复杂SQL用时30微秒。 通过Druid提供的SQL Parser可以在JDBC层拦截SQL做相应处理,比如说分库分表、审计等。Druid防御SQL注入攻击的WallFilter就是通过Druid的SQL Parser分析语义实现的
性能上图标分析
这个图标是从别人的blog上面copy过了 ,这个是sql语句进行1000次以后的查询提醒druid 连接池的性能分析,大家可以做为参考,是否准确待定。。
工具的使用
这个是在代码中去注册一些配置信息,不常用的 大家随便看看就好
package com.ruirui.druid;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import org.junit.Test;import com.alibaba.druid.pool.DruidDataSource;import com.ruirui.decoratedesgin.Utils;public class DruidDemo { @Test public void druidTest(){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///stu"); dataSource.setUsername("root"); dataSource.setPassword("123456"); try{ // 获得连接: conn = dataSource.getConnection(); // 编写SQL: String sql = "select * from student"; pstmt = conn.prepareStatement(sql); // 执行sql: rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getInt("id")+" "+rs.getString("name")); } }catch(Exception e){ e.printStackTrace(); }finally{ Utils.releaseResouce(rs, pstmt, conn); } }}
注册信息在配置文件中
@Test public void demo2(){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ Properties properties = new Properties(); properties.load(new FileInputStream("src/druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); // 获得连接: conn = dataSource.getConnection(); // 编写SQL: String sql = "select * from account"; pstmt = conn.prepareStatement(sql); // 执行sql: rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getDouble("money")); } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(rs, pstmt, conn); } }
配置文件
driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///day04username=rootpassword=123这里配置信息有很多 最大连接数量 , 最小连接数量等。。。配置的信息很多啊 大家可以去读一下官方文档。。 跟C3P0使用的情况 基本上都差不多> 这里是下载地址druid 文档 jar包 http://pan.baidu.com/s/1jHYEPpk
C3P0介绍
简介
目前使用C3P0连接池的开源项目有hibernate,spring等,其实所有的连接池都是为了提高应用于数据库自己的访问效率,资源管理等问题,使用的方法还有原理也不会有太大差别
示例代码
// 配置文件的方式 public void demo2(){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ // 核心类: ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 获得连接: conn = dataSource.getConnection(); // 编写SQL: String sql = "select * from account"; pstmt = conn.prepareStatement(sql); // 执行sql: rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getInt("id") } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(rs, pstmt, conn); } }
配置文件信息
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///day04</property> <property name="user">root</property> <property name="password">123</property> <property name="minPoolSize">5</property> <property name="initialPoolSize">5</property> </default-config> <named-config name="oracle"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///day04</property> <property name="user">root</property> <property name="password">123</property> <property name="minPoolSize">5</property> <property name="initialPoolSize">5</property> </named-config></c3p0-config>如果要ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("oracle"); 表示的是使用下面的配置文件信息,如果没有找到oracle的话就会使用上图模式配置信息
资源下载连接 http://pan.baidu.com/s/1kVFWsNP
JDBCUtils工具类的使用
package com.ruirui.jdbcutils;import java.sql.Connection;import java.sql.SQLException;import java.util.Arrays;import java.util.List;import java.util.Map;import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.ArrayHandler;import org.apache.commons.dbutils.handlers.ArrayListHandler;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import org.apache.commons.dbutils.handlers.ColumnListHandler;import org.apache.commons.dbutils.handlers.KeyedHandler;import org.apache.commons.dbutils.handlers.MapHandler;import org.apache.commons.dbutils.handlers.MapListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;import org.junit.Test;import com.mchange.v2.c3p0.ComboPooledDataSource;public class JDBCUtils { /** * QueryRunner:核心执行类 * ResultSetHandler:提供对查询结果封装 * DbUtils :工具类 */ //c3p0 连接池 public DataSource comboPooledDataSource = new ComboPooledDataSource(); public Connection getConn() throws SQLException{ return comboPooledDataSource.getConnection(); } //插入数据 public void saveData() throws SQLException { QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "insert into student values(null,'ruirui',?,?);"; queryRunner.update(sql, "shenyang",27); } // 修改表 public void fixData() throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "update student set name=? where id=?;"; queryRunner.update(sql,"pp", 13); } // 删除操作 public void demo3() throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql ="delete from account where id =?"; queryRunner.update(sql, 4); } // ArrayHandler:将查询到的一条记录封装到数组当中 public void demo1() throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select * from account where id = ?"; Object[] objs = queryRunner.query(sql, new ArrayHandler(), 1); // ”1“ 代表一条数据 System.out.println(Arrays.toString(objs)); } //ArrayListHandler //一条查询是ArrayHandler 一个数组 //多条查询 就是将多个数组 存入集合中 public void demo2() throws SQLException { QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select * from student"; List<Object[]> query = queryRunner.query(sql, new ArrayListHandler()); for (Object[] objects : query) { for (Object object : objects) { System.out.println(object.toString()); } } } //查询一条记录 返回的是一个bean对象 public void demo4() throws SQLException { QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select * from student where id = ?"; Man man = queryRunner.query(sql, new BeanHandler<>(Man.class),5); System.out.println(man.toString()); } //注意返回的是一个标准的javabean对象,所在定义bean对象时候成员变量必须用private定义 //查询一条记录 返回的是一个bean对象 public void demo5() throws SQLException { QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select * from student"; List<Man> query = queryRunner.query(sql, new BeanListHandler<>(Man.class )); System.out.println(query.toString()); } // MapHandler:封装一条记录到Map中 public void demo6()throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select * from student where id = ?"; Map<String,Object> map = queryRunner.query(sql, new MapHandler() ,2); System.out.println(map); } // MapListHandler: //查询多条 将map集合存入list 集合中 public void demo7()throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select * from student"; List<Map<String,Object>> list = queryRunner.query(sql, new MapListHandler()); for (Map<String, Object> map : list) { System.out.println(map); } } // ColumnListHandler ,返回的是一个列值的集合 public void demo8()throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select id from student"; List<Object> list = queryRunner.query(sql, new ColumnListHandler()); for (Object object : list) { System.out.println(object.toString()); } } //ScalarHandler:单值查询 public void demo9()throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select sum(age) from student;"; Long count = (Long)queryRunner.query(sql, new ScalarHandler()); System.out.println(count); } @Test // KeyedHandler: public void demo10()throws SQLException{ QueryRunner queryRunner = new QueryRunner(comboPooledDataSource); String sql = "select * from student"; Map<Object,Map<String,Object>> map= queryRunner.query(sql, new KeyedHandler("name")); for (Object key : map.keySet()) { System.out.println(key + " "+map.get(key)); } }}
JDBUtils 文档以及jar 下载链接
http://pan.baidu.com/s/1c13IXxu
阅读全文
0 0
- Druid 连接池 JDBCUtils 工具类的使用
- 使用 c3p0 连接池 改造 jdbcutils工具类
- JDBC模拟连接池、C3P0获取链接和最终的JdbcUtils工具类的版本
- 简易高重用的jdbcutils工具封装实现类以及简易连接池实现
- 简易高重用的jdbcutils工具封装实现类以及简易连接池实现
- druid连接池的使用。
- DRUID连接池的使用
- DRUID连接池的使用
- DRUID连接池的使用
- DRUID连接池的使用
- 使用工具类来简化代码JDBCUtils
- 使用工具类来简化代码JDBCUtils
- 使用工具类来简化代码JDBCUtils
- JDBC:编写通用的 JDBCUtils工具类
- JdbcUtils工具的抽取
- Java工具类-JdbcUtils
- 工具类JdbcUtils
- JDBCUtils工具类
- 记录最近遇到的坑
- BootStrap FileInput 插件实现多文件上传前端功能
- spring面向切面编程
- linux记录锁(范围锁)
- HDU 6047 Maximum Sequence 贪心 区间最值
- Druid 连接池 JDBCUtils 工具类的使用
- hive支持sql大全(9-11:复合类型构建操作/复杂类型访问操作/复杂类型长度统计函数)
- django 多并发,多线程。
- 简单实现高度平滑变化的ViewPager
- ArrayList和LinkedList的区别
- Floyd算法
- 牛客面经 | BAT、美团、360、滴滴新锐……2W字干货,这不仅仅是一篇面经
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)
- LeetCode