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);
}
}