gaozhuang211 xiexie Dbutils 详解( gaozhuang)
CommonDbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成List。
DBUtils包括3个包:
org.apache.commons.dbutils
org.apache.commons.dbutils.handlers
org.apache.commons.dbutils.wrappers
DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
org.apache.commons.dbutils
DbUtils关闭链接等操作
QueryRunner进行查询的操作
org.apache.commons.dbutils.handlers
ArrayHandler:将ResultSet中第一行的数据转化成对象数组
ArrayListHandler将ResultSet中所有的数据转化成List,List中存放的是Object[]
BeanHandler:将ResultSet中第一行的数据转化成类对象
BeanListHandler:将ResultSet中所有的数据转化成List,List中存放的是类对象
ColumnListHandler:将ResultSet中某一列的数据存成List,List中存放的是Object对象
KeyedHandler:将ResultSet中存成映射,key为某一列对应为Map。Map中存放的是数据
MapHandler:将ResultSet中第一行的数据存成Map映射
MapListHandler:将ResultSet中所有的数据存成List。List中存放的是Map
ScalarHandler:将ResultSet中一条记录的其中某一列的数据存成Object
org.apache.commons.dbutils.wrappers
SqlNullCheckedResultSet:对ResultSet进行操作,改版里面的值
StringTrimmedResultSet:去除ResultSet中中字段的左右空格。Trim()
主要方法:
DbUtils类:启动类
ResultSetHandler接口:转换类型接口
MapListHandler类:实现类,把记录转化成List
BeanListHandler类:实现类,把记录转化成List,使记录为JavaBean类型的对象
QreryRunner类:执行SQL语句的类
建立三个Java文件
命名为BeanListExample.java
Guestbook.java
MapListExample.java
源码:
BeanListExample.java
Code
packagecom.sy;
importorg.apache.commons.dbutils.DbUtils;
importorg.apache.commons.dbutils.QueryRunner;
importorg.apache.commons.dbutils.handlers.BeanListHandler;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.util.List;
publicclassBeanListExample{
publicstaticvoidmain(String[]args){
Connectionconn=null;
Stringurl="jdbc:mysql://localhost:3306/people";
StringjdbcDriver="com.mysql.jdbc.Driver";
Stringuser="root";
Stringpassword="hicc";
DbUtils.loadDriver(jdbcDriver);
try{
conn=DriverManager.getConnection(url,user,password);
QueryRunnerqr=newQueryRunner();
Listresults=(List)qr.query(conn,"selectid,namefromguestbook",newBeanListHandler(Guestbook.class));
for(inti=0;i<results.size();i++){
Guestbookgb=(Guestbook)results.get(i);
System.out.println("id:"+gb.getId()+",name:"+gb.getName());
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
DbUtils.closeQuietly(conn);
}
}
}
Guestbook.java
Code
packagecom.sy;
publicclassGuestbook{
privateIntegerid;
privateStringname;
publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
MapListExample.java
Code
packagecom.sy;
importorg.apache.commons.dbutils.DbUtils;
importorg.apache.commons.dbutils.QueryRunner;
importorg.apache.commons.dbutils.handlers.MapListHandler;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.util.List;
importjava.util.Map;
publicclassMapListExample{
publicstaticvoidmain(String[]args){
Connectionconn=null;
Stringurl="jdbc:mysql://localhost:3306/people";
StringjdbcDriver="com.mysql.jdbc.Driver";
Stringuser="root";
Stringpassword="hicc";
DbUtils.loadDriver(jdbcDriver);
try{
conn=DriverManager.getConnection(url,user,password);
QueryRunnerqr=newQueryRunner();
Listresults=(List)qr.query(conn,"selectid,namefromguestmessage",newMapListHandler());
for(inti=0;i<results.size();i++){
Mapmap=(Map)results.get(i);
System.out.println("id:"+map.get("id")+",name:"+map.get("name"));
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
DbUtils.closeQuietly(conn);
}
}
}
使用组建好需要添加commons-dbutils-1.1.jar和mysql-connector-java-5.1.6-bin.jar两个jar包。
配置完毕!!!
//另一种方法
//使用dbutils1.0版本
importjava.util.*;
importjava.util.logging.*;
importjava.sql.*;
importorg.apache.commons.dbutils.*;
importorg.apache.commons.dbutils.handlers.*;
publicclassTestDBUnits{
publicstaticvoidmain(String[]args)throwsException{
TestDBUnitstest=newTestDBUnits();
for(inti=0;i<1;i++){
test.testQuery1();
test.testQuery2();
test.testUpdate();
}
}
publicvoidtestQuery1(){
try{
QueryRunnerqr=newQueryRunner();
ResultSetHandlerrsh=newArrayListHandler();
Stringstrsql="select*fromtest1";
ArrayListresult=(ArrayList)qr.query(getConnection(),strsql,rsh);
//System.out.print("");
}catch(Exceptionex){
ex.printStackTrace(System.out);
}
}
publicvoidtestQuery2(){
try{
QueryRunnerqr=newQueryRunner();
ResultSetHandlerrsh=newMapListHandler();
Stringstrsql="select*fromtest1";
ArrayListresult=(ArrayList)qr.query(getConnection(),strsql,rsh);
for(inti=0;i<result.size();i++){
Mapmap=(Map)result.get(i);
//System.out.println(map);
}
//System.out.print("");
}catch(Exceptionex){
ex.printStackTrace(System.out);
}
}
publicvoidtestUpdate(){
try{
QueryRunnerqr=newQueryRunner();
ResultSetHandlerrsh=newArrayListHandler();
Stringstrsql="inserttest1(page,writable,content)values('ttt','ttt','faskldfjklasdjklfjasdklj')";
qr.update(getConnection(),strsql);
//System.out.print("");
}catch(Exceptionex){
ex.printStackTrace(System.out);
}
}
privateConnectiongetConnection()throwsInstantiationException,
IllegalAccessException,ClassNotFoundException,SQLException{
StringstrDriver="org.gjt.mm.mysql.Driver";
StringstrUrl="jdbc:mysql://localhost:3306/test";
StringstrUser="root";
StringstrPass="";
Class.forName(strDriver).newInstance();
returnDriverManager.getConnection(strUrl,strUser,strPass);
}
}