日期:2014-05-16 浏览次数:20602 次
package com.jdbc.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.BeanProcessor; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; 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 com.jdbc.db.DBConnection; import com.jdbc.entity.Book; import com.jdbc.entity.School; import com.jdbc.entity.Student; public class Test { public static void main(String[] args) { Test t = new Test(); // dbutils 自带的一个结果集处理类,只把查询结果的第一行封装成数组 t.testArrayHandler(); // dbutils 自带的一个结果集处理类,把查询结果的每一个行都封装到数组再把数组装到集合中 t.testArrayListHandler(); // dbutils 自带的一个结果集处理类,把查询结果封装在bean中 t.testBeanHandler(); // dbutils 自带的一个结果集处理类,把查询结果封装在bean中再把bean装到集合中 t.testBeanListHandler(); // 两个表查封装到一个bean中 t.queryTwoTable(); // 把两个表联查的每行数据封装到bean中再装入集合 t.queryListTwoTable(); // 3个表联查封装成级联bean t.queryListThreeTable(); t.insert(); t.upload(); t.detele(); // 把数据插入两张表 t.insertTwoTable(); } public void testArrayHandler() { ResultSetHandler<Object[]> rsh = new ArrayHandler(); QueryRunner qr = new QueryRunner(new DBConnection()); String sql = "select * from student, book where student.book_id = book.book_id"; try { Object[] arr = qr.query(sql, rsh); for (int i = 0; i < arr.length; i++) System.out.print(arr[i].toString() + "\t"); System.out.println(""); } catch (SQLException e) { e.printStackTrace(); } } public void testArrayListHandler() { ResultSetHandler<List<Object[]>> rsh = new ArrayListHandler(); QueryRunner qr = new QueryRunner(new DBConnection()); String sql = "select * from student, book where student.book_id = book.book_id"; try { List<Object[]> list = qr.query(sql, rsh); int size = list.size(); for (int i = 0; i < size; i++) { Object[] arr = list.get(i); for (int j = 0; j < arr.length; j++) System.out.print(arr[j].toString() + "\t"); System.out.println(""); } System.out.println(""); } catch (SQLException e) { e.printStackTrace(); } } public void testBeanHandler() { ResultSetHandler<Student> rsh = new BeanHandler<Student>(Student.class); QueryRunner qr = new QueryRunner(new DBConnection()); String sql = "select * from student, book where student.book_id = book.book_id"; try { Student student = qr.query(sql, rsh); System.out.print(student.getStudent_id() + "\t"); System.out.print(student.getStudent_name() + "\t"); System.out.print(student.getStudent_age() + "\t"); System.out.print(student.getStudent_email() + "\n"); } catch (SQLException e) { e.printStackTrace(); } } public void queryTwoTable() { QueryRunner qr = new QueryRunner(new DBConnection()); String sql = "select * from student, book where student.book_id = book.book_id"; try { Student student = qr.query(sql, new ResultSetHandler<Student>() { public Student handle(ResultSet rs) throws SQLExce