首先我們的明白Apache DBUtils是幹什麼用的:它是dao層的一個幫助類,簡化了dao層jdbc的開發,下面直接開始講述實現步驟:
1.下載commons-dbutils-1.7.jar
其中包含了三個基本類:
DbUtils輔助,QueryRunner:增刪改查. ResultSetHandler查詢返回集合
還需要引入一種數據源的jar,和jdbc的jar。
下面是各自的常用方法
1.DbUtils常用方法:
2.QueryRunner類常用方法:
3.ResultSetHandler:
測試:
數據庫的表:
Student類:
package Clss.apacheddbutil;
public class Student {
private int stuno;
private String stuname;
private int stuage;
private String gname;
public Student(int stuno, String stuname, int stuage, String gname) {
this.stuno = stuno;
this.stuname = stuname;
this.stuage = stuage;
this.gname = gname;
}
public Student() {
}
public int getStuno() {
return stuno;
}
public void setStuno(int stuno) {
this.stuno = stuno;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
}
查詢:
package Clss.apacheddbutil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import util.DataSourceUtil;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.List;
import java.util.Map;
public class query {
//查詢,自動提交事務
//返回單行Object
public static void testArrayHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
Object[] student = runner.query("select *from student where stuno >? and stuname like ?",new ArrayHandler(),new Object[]{10,"%z%"});//ArrayHandaler只取一行
System.out.println(student.length);
System.out.println(student[0]+","+student[1]+","+student[2]+","+student[3]);
}//順序默認是oracle 表的順序
//返回多行Object
public static void testArrayListHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
List<Object[]> students = runner.query("select *from student where stuno >?",new ArrayListHandler(),5);//ArrayHandaler只取一行
for(Object[] student:students){
System.out.println(student[0]+","+student[1]+","+student[2]+","+student[3]);
}//順序默認是oracle表的順序
}
//Student
//查詢單行數據(放入對象中)
public static void testBeanHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
Student student = runner.query("select *from student where stuno >?",new BeanHandler<Student>(Student.class),10);//ArrayHandaler只取一行
System.out.println(student.getStuname()+","+student.getStuno()+","+student.getStuage()+","+student.getGname());
}//注意創建的Student類裏面的參數必須和該表的屬性一模一樣 比如說必須是Student類裏面必須定義stuno ,不能寫成stu
//輸出可以根據自己的喜好 調整輸出參數的位置
//查詢多行數據
public static void testBeanListHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
List<Student> students = runner.query("select *from student where stuno >?",new BeanListHandler<Student>(Student.class),10);//ArrayHandaler只取一行
for(Student student:students) {
System.out.println(student.getStuname() + "," + student.getStuno() + "," + student.getStuage() + "," + student.getGname());
}
}
//查詢多行數據(放入map中)
public static void testBeanMapHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml()); //注意Oracle默認數據類型是BigDecimal,不能用Integer
Map<BigDecimal,Student> students = runner.query("select *from student where stuno >?",new BeanMapHandler<BigDecimal,Student>(Student.class,"stuno"),10);//ArrayHandaler只取一行
Student stu = students.get(new BigDecimal(13));//坑!!!!!不能寫13 必須轉成BigDecimal類型
System.out.println(stu.getStuname() + "," + stu.getStuno() + "," + stu.getStuage() + "," + stu.getGname());
}
////////////////.......................................Map ->返回的結果爲前面有屬性名
//返回第一行數據
public static void testMapHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
Map<String,Object> student = runner.query("select *from student where stuno >?",new MapHandler(),656);
System.out.println(student);
}
//返回第多行數據
public static void testMapListHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
List<Map<String,Object>> students = runner.query("select *from student where stuno >?",new MapListHandler(),10);
for(Map<String,Object> student:students)
System.out.println(student);
}
//多行加字段 給{}一個key值
public static void testKeyedHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
Map<String,Map<String,Object>> students = runner.query("select *from student where stuno >?",new KeyedHandler<String>("stuname"),10);
System.out.println(students);
}
///////////////////................................................把結果集中的某一列 保存到List中
public static void testColumnListHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
List<String> students = runner.query("select *from student where stuno >?",new ColumnListHandler<String>("stuname"),10);
System.out.println(students);
}
//ScalarHandler :單值結果
public static void testScalarHandler() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
BigDecimal result = runner.query("select count(1) from student where stuno >?",new ScalarHandler<BigDecimal>(),10);
System.out.println(result);//sql: select stuno from student where stu0= ?; param:13
}
public static void main(String[] args) throws SQLException {
// testArrayHandler();
testArrayListHandler();
//testBeanHandler();
// testBeanListHandler();
//testBeanMapHandler();
// testMapHandler();
// testMapListHandler();
// testKeyedHandler();
// testColumnListHandler();
//testScalarHandler();
}
}
增刪改:
package Clss.apacheddbutil;
import org.apache.commons.dbutils.QueryRunner;
import util.DataSourceUtil;
import java.sql.SQLException;
public class UpdateDemo {
//自動提交
//增加
public static void add() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
int count = runner.update("insert into student(stuno,stuname,stuage,gname) values(?,?,?,?)",new Object[]{50,"huhuilin",18,"sss"});
System.out.println(count);//返回增刪改的 條數
}
//刪除
public static void delete() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
int count = runner.update("delete from student where stuno = ?",999);
System.out.println(count);//返回增刪改的 條數
}
//修改
public static void update() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSourceWithC3P0ByXml());
int count = runner.update("update student set stuname=? where stuno=?",new Object[]{"wangyin",4});
System.out.println(count);//返回增刪改的 條數
}
public static void main(String[] args) throws SQLException {
// add();
// delete();
update();
}
}
注意,有些ojdbc.jar包和本項目不兼容!