DButils工具类
- DBUtils是java编程中的数据库操作实用工具,小巧简单实用。
- DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
- DBUtils就是JDBC的简化开发工具包。需要项目导入commons-dbutils-1.6.jar才能够正常使用DBUtils工具。
commons-dbutils
commons-dbutils-1.6-bin.zip
mysql-connector-java-5.1.37.zip
Dbutils核心功能
- QueryRunner中提供对sql语句操作的API
- update(Connection conn, String sql, Object... params) ,用来完成表数据的增加、删除、更新操作
- query(Connection conn, String sql, ResultSetHandler rsh, Object... params) ,用来完成表数据的查询操作
- ResultSetHandler接口,用于定义select操作后,怎样封装结果集.
- DbUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法
QueryRunner类
获取数据库连接对象
- 数据库连接部分的代码,之前是使用文件读取,这次直接写成参数,另外关闭连接使用另外的方法所以原来关闭连接的方法也删去了
| import java.sql.*; |
| |
| public class Jdbctool { |
| private Jdbctool(){} |
| private static Connection con; |
| static{ |
| try{ |
| Class.forName("com.mysql.jdbc.Driver"); |
| String url = "jdbc:mysql://localhost:3306/javastudy"; |
| String user = "root"; |
| String password = "123456"; |
| con = DriverManager.getConnection(url, user, password); |
| } |
| catch(Exception e) |
| { |
| throw new RuntimeException(e+"连接失败"); |
| } |
| } |
| |
| public static Connection getsql(){ |
| return con; |
| } |
| } |
QueryRunner类实现insert update delete
| import java.sql.Connection; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| |
| public class Test { |
| |
| public static void main(String[] args) throws Exception { |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql; |
| int row; |
| |
| |
| sql = "INSERT INTO sort (sname,sprice,sdesc)VALUES(?,?,?)"; |
| |
| Object[] params = {"体育用品",289.32,"购买体育用品"}; |
| |
| row = qr.update(con, sql, params); |
| System.out.println(row); |
| |
| sql = "UPDATE sort SET sname=?,sprice=?,sdesc=? WHERE sid=?"; |
| row = qr.update(con, sql,"花卉",100.88,"情人节玫瑰花",4); |
| System.out.println(row); |
| |
| sql = "DELETE FROM sort WHERE sid>?"; |
| row = qr.update(con, sql, 7); |
| System.out.println(row); |
| |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
JavaBean类
- JavaBean就是一个类,在开发中常用封装数据。具有如下特性
- 需要实现接口:java.io.Serializable 通常实现接口这步骤省略了,不会影响程序。
- 提供私有字段:private 类型 字段名
- 提供getter/setter方法
- 提供无参构造
DBUtils工具类select结果集处理的方式
-
query(Connection conn, String sql, ResultSetHandler rsh, Object... params)
-
ResultSetHandler结果集处理类
-
ArrayHandler
将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值
-
ArrayListHandler
将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。
-
BeanHandler
将结果集中第一条记录封装到一个指定的javaBean中。
-
BeanListHandler
将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中
-
ColumnListHandler
将结果集中指定的列的字段值,封装到一个List集合中
-
ScalarHandler
它是用于单数据。例如select count(*) from 表操作。
-
MapHandler
将结果集第一行封装到Map集合中,Key 列名, Value 该列数据
-
MapListHandler
将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合
数据库sort表数据
+-----+----------+--------+-------------------------+
| sid | sname | sprice | sdesc |
+-----+----------+--------+-------------------------+
| 1 | 家电 | 2000 | 优惠的促销 |
| 2 | 家具 | 8900 | 家具价格上调,原材料涨价 |
| 3 | 儿童玩具 | 290 | 赚家长的钱 |
| 4 | 花卉 | 100.88 | 情人节玫瑰花 |
| 5 | 服装 | 24000 | 换季销售 |
| 6 | 洗涤 | 50 | 洗发水促销 |
| 7 | 汽车美容 | 49988 | 疯狂涨价 |
+-----+----------+--------+-------------------------+
结果集处理ArrayHandler
| import java.sql.Connection; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.ArrayHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT * FROM sort"; |
| |
| Object[] result = qr.query(con, sql, new ArrayHandler()); |
| for(Object obj : result){ |
| System.out.print(obj); |
| } |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
结果集处理ArrayListHandler
| import java.sql.Connection; |
| import java.util.List; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.ArrayListHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT * FROM sort"; |
| |
| List<Object[]> result= qr.query(con, sql, new ArrayListHandler()); |
| |
| for( Object[] objs : result){ |
| |
| for(Object obj : objs){ |
| System.out.print(obj+" "); |
| } |
| System.out.println(); |
| } |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
结果集处理BeanHandler
-
创建一个类具有get、set、toString
| public class Sort { |
| private int sid; |
| private String sname; |
| private double sprice; |
| private String sdesc; |
| public Sort(int sid, String sname, double sprice, String sdesc) { |
| this.sid = sid; |
| this.sname = sname; |
| this.sprice = sprice; |
| this.sdesc = sdesc; |
| } |
| public Sort(){} |
| public int getSid() { |
| return sid; |
| } |
| public void setSid(int sid) { |
| this.sid = sid; |
| } |
| public String getSname() { |
| return sname; |
| } |
| public void setSname(String sname) { |
| this.sname = sname; |
| } |
| public double getSprice() { |
| return sprice; |
| } |
| public void setSprice(double sprice) { |
| this.sprice = sprice; |
| } |
| public String getSdesc() { |
| return sdesc; |
| } |
| public void setSdesc(String sdesc) { |
| this.sdesc = sdesc; |
| } |
| @Override |
| public String toString() { |
| return "Sort [sid=" + sid + ", sname=" + sname + ", sprice=" + sprice + ", sdesc=" + sdesc + "]"; |
| } |
| } |
-
代码实现部分
| import java.sql.Connection; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.BeanHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT * FROM sort"; |
| |
| |
| Sort s = qr.query(con, sql, new BeanHandler<Sort>(Sort.class)); |
| System.out.println(s); |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
结果集处理BeanListHandler
| public class Sort { |
| private int sid; |
| private String sname; |
| private double sprice; |
| private String sdesc; |
| public Sort(int sid, String sname, double sprice, String sdesc) { |
| this.sid = sid; |
| this.sname = sname; |
| this.sprice = sprice; |
| this.sdesc = sdesc; |
| } |
| public Sort(){} |
| public int getSid() { |
| return sid; |
| } |
| public void setSid(int sid) { |
| this.sid = sid; |
| } |
| public String getSname() { |
| return sname; |
| } |
| public void setSname(String sname) { |
| this.sname = sname; |
| } |
| public double getSprice() { |
| return sprice; |
| } |
| public void setSprice(double sprice) { |
| this.sprice = sprice; |
| } |
| public String getSdesc() { |
| return sdesc; |
| } |
| public void setSdesc(String sdesc) { |
| this.sdesc = sdesc; |
| } |
| @Override |
| public String toString() { |
| return "Sort [sid=" + sid + ", sname=" + sname + ", sprice=" + sprice + ", sdesc=" + sdesc + "]"; |
| } |
| } |
| import java.sql.Connection; |
| import java.util.List; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.BeanListHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT * FROM sort"; |
| |
| |
| List<Sort> list = qr.query(con, sql, new BeanListHandler<Sort>(Sort.class)); |
| for(Sort s : list) |
| { |
| System.out.println(s); |
| } |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
结果集处理ColumnListHandler
| import java.sql.Connection; |
| import java.util.List; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.ColumnListHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT * FROM sort"; |
| |
| |
| List<Object> list = qr.query(con, sql, new ColumnListHandler<Object>("sname")); |
| for(Object obj : list){ |
| System.out.println(obj); |
| } |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
结果集处理ScalarHandler
| import java.sql.Connection; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.ScalarHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT COUNT(*) FROM sort"; |
| |
| long count = qr.query(con, sql, new ScalarHandler<Long>()); |
| System.out.println(count); |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
结果集处理MapHandler
| import java.sql.Connection; |
| import java.util.Map; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.MapHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT * FROM sort"; |
| |
| |
| Map<String,Object> map = qr.query(con, sql, new MapHandler()); |
| |
| for(String key : map.keySet()){ |
| System.out.println(key+".."+map.get(key)); |
| } |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
结果集处理MapListHandler
| import java.sql.Connection; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.apache.commons.dbutils.DbUtils; |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.MapListHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| |
| |
| |
| |
| |
| QueryRunner qr = new QueryRunner(); |
| Connection con = Jdbctool.getsql(); |
| String sql = "SELECT * FROM sort"; |
| |
| |
| List<Map<String,Object>> list = qr.query(con, sql, new MapListHandler()); |
| |
| for( Map<String,Object> map : list ){ |
| for(String key : map.keySet()){ |
| System.out.print(key+"..."+map.get(key)); |
| } |
| System.out.println(); |
| } |
| |
| DbUtils.closeQuietly(con); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
连接池
- 实际上就是存放连接的池子(容器)
- 在开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程
- 为了解决此类性能问题,通常情况我们采用连接池技术,来共享连接Connection。
- 这样我们就不需要每次都创建连接、释放连接了,这些操作都交给了连接池
连接池概念规范
- 用池来管理Connection,这样可以重复使用Connection。
- 不用自己来创建Connection,而是通过池来获取Connection对象
- 使用完Connection后,调用Connection的close()方法也不会真的关闭Connection,而是把Connection“归还”给池
- 连接池技术可以完成Connection对象的再次利用
DataSource接口
- Java为数据库连接池提供了公共的接口:javax.sql.DataSource
- 各个厂商需要让自己的连接池实现这个接口。这样应用程序可以方便的切换不同厂商的连接池
- 常见的连接池:DBCP、C3P0
- DBCP也是一个开源的连接池,是Apache Common成员之一,在企业开发中也比较常见,tomcat内置的连接池
导入jar包
- mysql-connector-java-5.1.37-bin.jar:数据库驱动
- commons-dbutils-1.6.jar:提供QueryRunner类方便进行增删改查操作
- commons-dbcp-1.4.jar:
- commons-pool-1.5.6.jar:提供高效的数据库连接池技术
commons-dbutils-1.6-bin.zip
commons-dbcp-1.4-bin.zip
commons-pool-1.5.6-bin.zip
mysql-connector-java-5.1.37.zip
BasicDataSource类 实现数据库连接池工具类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import javax.sql.DataSource; |
| |
| import org.apache.commons.dbcp.BasicDataSource; |
| |
| public class Jdbctool { |
| private Jdbctool(){} |
| |
| private static BasicDataSource datasource = new BasicDataSource(); |
| |
| |
| static{ |
| |
| datasource.setDriverClassName("com.mysql.jdbc.Driver"); |
| datasource.setUrl("jdbc:mysql://localhost:3306/javastudy"); |
| datasource.setUsername("root"); |
| datasource.setPassword("123456"); |
| |
| datasource.setInitialSize(10); |
| datasource.setMaxActive(8); |
| datasource.setMaxIdle(5); |
| datasource.setMinIdle(1); |
| } |
| |
| public static DataSource getDataSource(){ |
| return datasource; |
| } |
| } |
工具类的测试实现查询和添加
| import java.util.List; |
| |
| import org.apache.commons.dbutils.QueryRunner; |
| import org.apache.commons.dbutils.handlers.ArrayListHandler; |
| |
| public class Test { |
| public static void main(String[] args) throws Exception { |
| QueryRunner qr = new QueryRunner(Jdbctool.getDataSource()); |
| |
| List<Object[]> list = qr.query("SELECT * FROM sort", new ArrayListHandler()); |
| for( Object[] obj : list ){ |
| for(Object str : obj){ |
| System.out.print(str+"..."); |
| } |
| System.out.println(); |
| } |
| |
| String sql = "INSERT INTO sort (sname,sprice,sdesc)VALUES(?,?,?)"; |
| Object[] params = {"水果",100.12,"刚刚上市的核桃"}; |
| int row = qr.update(sql, params); |
| System.out.println(row); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
0 条评论