commons-dbutils的操作方法


这张是数据库表,一共三张,每个表的ID都是自动增长列
 
 

这个是项目结构 
 

测试代码 
Java代码   收藏代码
  1. package com.jdbc.dao;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import org.apache.commons.dbutils.BeanProcessor;  
  11. import org.apache.commons.dbutils.DbUtils;  
  12. import org.apache.commons.dbutils.QueryRunner;  
  13. import org.apache.commons.dbutils.ResultSetHandler;  
  14. import org.apache.commons.dbutils.handlers.ArrayHandler;  
  15. import org.apache.commons.dbutils.handlers.ArrayListHandler;  
  16. import org.apache.commons.dbutils.handlers.BeanHandler;  
  17. import org.apache.commons.dbutils.handlers.BeanListHandler;  
  18.   
  19. import com.jdbc.db.DBConnection;  
  20. import com.jdbc.entity.Book;  
  21. import com.jdbc.entity.School;  
  22. import com.jdbc.entity.Student;  
  23.   
  24. public class Test {  
  25.   
  26.     public static void main(String[] args) {  
  27.   
  28.         Test t = new Test();  
  29.   
  30.         // dbutils 自带的一个结果集处理类,只把查询结果的第一行封装成数组  
  31.         t.testArrayHandler();  
  32.    
  33.         // dbutils 自带的一个结果集处理类,把查询结果的每一个行都封装到数组再把数组装到集合中  
  34.         t.testArrayListHandler();  
  35.   
  36.         // dbutils 自带的一个结果集处理类,把查询结果封装在bean中  
  37.         t.testBeanHandler();  
  38.                     
  39.         // dbutils 自带的一个结果集处理类,把查询结果封装在bean中再把bean装到集合中  
  40.         t.testBeanListHandler();  
  41.                     
  42.         // 两个表查封装到一个bean中  
  43.         t.queryTwoTable();  
  44.   
  45.         // 把两个表联查的每行数据封装到bean中再装入集合  
  46.         t.queryListTwoTable();  
  47.   
  48.         // 3个表联查封装成级联bean  
  49.         t.queryListThreeTable();      
  50.       
  51.         t.insert();  
  52.         t.upload();  
  53.         t.detele();  
  54.   
  55.         // 把数据插入两张表  
  56.         t.insertTwoTable();  
  57.   
  58.     }  
  59.   
  60.     public void testArrayHandler() {  
  61.   
  62.         ResultSetHandler<Object[]> rsh = new ArrayHandler();  
  63.         QueryRunner qr = new QueryRunner(new DBConnection());  
  64.   
  65.         String sql = "select * from student, book where student.book_id = book.book_id";  
  66.         try {  
  67.             Object[] arr = qr.query(sql, rsh);  
  68.             for (int i = 0; i < arr.length; i++)  
  69.                 System.out.print(arr[i].toString() + "\t");  
  70.             System.out.println("");  
  71.         } catch (SQLException e) {  
  72.             e.printStackTrace();  
  73.         }  
  74.   
  75.     }  
  76.   
  77.     public void testArrayListHandler() {  
  78.   
  79.         ResultSetHandler<List<Object[]>> rsh = new ArrayListHandler();  
  80.         QueryRunner qr = new QueryRunner(new DBConnection());  
  81.   
  82.         String sql = "select * from student, book where student.book_id = book.book_id";  
  83.   
  84.         try {  
  85.             List<Object[]> list = qr.query(sql, rsh);  
  86.             int size = list.size();  
  87.             for (int i = 0; i < size; i++) {  
  88.                 Object[] arr = list.get(i);  
  89.                 for (int j = 0; j < arr.length; j++)  
  90.                     System.out.print(arr[j].toString() + "\t");  
  91.                 System.out.println("");  
  92.             }  
  93.             System.out.println("");  
  94.         } catch (SQLException e) {  
  95.             e.printStackTrace();  
  96.         }  
  97.   
  98.     }  
  99.   
  100.     public void testBeanHandler() {  
  101.   
  102.         ResultSetHandler<Student> rsh = new BeanHandler<Student>(Student.class);  
  103.         QueryRunner qr = new QueryRunner(new DBConnection());  
  104.   
  105.         String sql = "select * from student, book where student.book_id = book.book_id";  
  106.   
  107.         try {  
  108.             Student student = qr.query(sql, rsh);  
  109.             System.out.print(student.getStudent_id() + "\t");  
  110.             System.out.print(student.getStudent_name() + "\t");  
  111.             System.out.print(student.getStudent_age() + "\t");  
  112.             System.out.print(student.getStudent_email() + "\n");  
  113.   
  114.         } catch (SQLException e) {  
  115.             e.printStackTrace();  
  116.         }  
  117.   
  118.     }  
  119.   
  120.     public void queryTwoTable() {  
  121.   
  122.         QueryRunner qr = new QueryRunner(new DBConnection());  
  123.   
  124.         String sql = "select * from student, book where student.book_id = book.book_id";  
  125.   
  126.         try {  
  127.             Student student = qr.query(sql, new ResultSetHandler<Student>() {  
  128.   
  129.                 public Student handle(ResultSet rs) throws SQLException {  
  130.                     Student student = null;  
  131.                     if (rs.next()) {  
  132.                         BeanProcessor bp = new BeanProcessor();  
  133.                         student = bp.toBean(rs, Student.class);  
  134.                         Book book = bp.toBean(rs, Book.class);  
  135.                         student.setBook(book);  
  136.                     }  
  137.                     return student;  
  138.                 }  
  139.             });  
  140.   
  141.             System.out.print(student.getStudent_id() + "\t");  
  142.             System.out.print(student.getStudent_name() + "\t");  
  143.             System.out.print(student.getStudent_age() + "\t");  
  144.             System.out.print(student.getStudent_email() + "\t");  
  145.   
  146.             System.out.print(student.getBook().getBook_id() + "\t");  
  147.             System.out.print(student.getBook().getBook_name() + "\t");  
  148.             System.out.print(student.getBook().getBook_price() + "\n");  
  149.   
  150.         } catch (SQLException e) {  
  151.             e.printStackTrace();  
  152.         }  
  153.   
  154.     }  
  155.   
  156.     public void testBeanListHandler() {  
  157.   
  158.         ResultSetHandler<List<Student>> rsh = new BeanListHandler<Student>(Student.class);  
  159.         QueryRunner qr = new QueryRunner(new DBConnection());  
  160.   
  161.         String sql = "select * from student, book where student.book_id = book.book_id";  
  162.   
  163.         try {  
  164.             List<Student> list = qr.query(sql, rsh);  
  165.             int size = list.size();  
  166.             for (int i = 0; i < size; i++) {  
  167.                 Student student = list.get(i);  
  168.                 System.out.print(student.getStudent_id() + "\t");  
  169.                 System.out.print(student.getStudent_name() + "\t");  
  170.                 System.out.print(student.getStudent_age() + "\t");  
  171.                 System.out.print(student.getStudent_email() + "\n");  
  172.             }  
  173.         } catch (SQLException e) {  
  174.             e.printStackTrace();  
  175.         }  
  176.   
  177.     }  
  178.   
  179.     public void queryListTwoTable() {  
  180.   
  181.         QueryRunner qr = new QueryRunner(new DBConnection());  
  182.   
  183.         String sql = "select * from student, book where student.book_id = book.book_id";  
  184.   
  185.         try {  
  186.   
  187.             List<Student> list = qr.query(sql,  
  188.                     new ResultSetHandler<List<Student>>() {  
  189.   
  190.                         public List<Student> handle(ResultSet rs) throws SQLException {  
  191.                             List<Student> list = new ArrayList<Student>();  
  192.                             BeanProcessor bp = new BeanProcessor();  
  193.                             while (rs.next()) {  
  194.   
  195.                                 Student student = bp.toBean(rs, Student.class);  
  196.                                 Book book = bp.toBean(rs, Book.class);  
  197.                                 student.setBook(book);  
  198.                                 list.add(student);  
  199.                             }  
  200.                             return list;  
  201.                         }  
  202.   
  203.                     });  
  204.   
  205.             if (list != null && !list.isEmpty()) {  
  206.                 for (Student student : list) {  
  207.                     System.out.print(student.getStudent_id() + "\t");  
  208.                     System.out.print(student.getStudent_name() + "\t");  
  209.                     System.out.print(student.getStudent_age() + "\t");  
  210.                     System.out.print(student.getStudent_email() + "\t");  
  211.   
  212.                     System.out.print(student.getBook().getBook_id() + "\t");  
  213.                     System.out.print(student.getBook().getBook_name() + "\t");  
  214.                     System.out.print(student.getBook().getBook_price() + "\n");  
  215.                 }  
  216.             }  
  217.   
  218.         } catch (SQLException e) {  
  219.             e.printStackTrace();  
  220.         }  
  221.   
  222.     }  
  223.   
  224.     public void queryListThreeTable() {  
  225.   
  226.         Connection conn = null;  
  227.         QueryRunner qr = new QueryRunner();  
  228.           
  229.         List<School> schoolList = null;  
  230.         try {  
  231.   
  232.             conn = new DBConnection().getConnection();  
  233.             String sql = "select * from school";  
  234.             schoolList = qr.query(conn, sql, new BeanListHandler<School>(School.class));  
  235.   
  236.             if (schoolList != null && !schoolList.isEmpty()) {  
  237.                 for (School school : schoolList) {  
  238.                     sql = "select * from student, book where student.book_id = book.book_id and student.school_id = ?";  
  239.                     ResultSetHandler<List<Student>> rsh = new ResultSetHandler<List<Student>>() {  
  240.   
  241.                         public List<Student> handle(ResultSet rs) throws SQLException {  
  242.   
  243.                             List<Student> list = new ArrayList<Student>();  
  244.                             BeanProcessor bp = new BeanProcessor();  
  245.                             while (rs.next()) {  
  246.   
  247.                                 Student student = bp.toBean(rs, Student.class);  
  248.                                 Book book = bp.toBean(rs, Book.class);  
  249.                                 student.setBook(book);  
  250.                                 list.add(student);  
  251.                             }  
  252.                             return list;  
  253.                         }  
  254.   
  255.                     };  
  256.                     List<Student> studentList = qr.query(conn, sql, rsh, school.getSchool_id());  
  257.                     school.setStudentList(studentList);  
  258.                 }  
  259.   
  260.             }  
  261.   
  262.         } catch (SQLException e) {  
  263.             e.printStackTrace();  
  264.         } finally {  
  265.             DbUtils.closeQuietly(conn);  
  266.         }  
  267.           
  268.         if (schoolList != null && !schoolList.isEmpty()) {  
  269.             for (School school : schoolList) {  
  270.                 System.out.print(school.getSchool_id() + "\t");  
  271.                 System.out.print(school.getSchool_name() + "\t");  
  272.                 System.out.print(school.getSchool_address() + "\n");  
  273.                 for (Student student : school.getStudentList()) {  
  274.                     System.out.print("\t" + student.getStudent_id() + "\t");  
  275.                     System.out.print(student.getStudent_name() + "\t");  
  276.                     System.out.print(student.getStudent_age() + "\t");  
  277.                     System.out.print(student.getStudent_email() + "\t");  
  278.   
  279.                     System.out.print(student.getBook().getBook_id() + "\t");  
  280.                     System.out.print(student.getBook().getBook_name() + "\t");  
  281.                     System.out.print(student.getBook().getBook_price() + "\n");  
  282.                 }  
  283.             }  
  284.   
  285.         }  
  286.   
  287.     }  
  288.   
  289.     public void insert() {  
  290.   
  291.         QueryRunner qr = new QueryRunner(new DBConnection());  
  292.   
  293.         String sql = "insert into student values(?, ?, ?, ?)";  
  294.         Object[] params = new Object[] { "seven"23"seven123@qq.com"1 };  
  295.   
  296.         try {  
  297.             System.out.println(qr.update(sql, params));  
  298.         } catch (SQLException e) {  
  299.             e.printStackTrace();  
  300.         }  
  301.   
  302.     }  
  303.   
  304.     public void insertTwoTable() {  
  305.   
  306.         Connection conn = null;  
  307.         PreparedStatement pst = null;  
  308.         ResultSet rs = null;  
  309.   
  310.         try {  
  311.             conn = new DBConnection().getConnection();  
  312.             QueryRunner qr = new QueryRunner();  
  313.   
  314.             conn.setAutoCommit(false);  
  315.   
  316.             String sql = "insert into book values(?, ?)";  
  317.             Object[] params = new Object[] { "C#"99.36 };  
  318.   
  319.             pst = conn.prepareStatement(sql,  
  320.                     PreparedStatement.RETURN_GENERATED_KEYS);  
  321.             qr.fillStatement(pst, params);  
  322.             pst.execute();  
  323.             rs = pst.getGeneratedKeys();  
  324.             int book_id = rs.next() ? rs.getInt(1) : 0;  
  325.   
  326.             sql = "insert into student values(?, ?, ?, ?)";  
  327.             params = new Object[] { "seven"23"seven123@qq.com", book_id };  
  328.   
  329.             pst = conn.prepareStatement(sql);  
  330.             qr.fillStatement(pst, params);  
  331.             pst.execute();  
  332.   
  333.             conn.commit();  
  334.   
  335.         } catch (SQLException e) {  
  336.             try {  
  337.                 DbUtils.rollback(conn);  
  338.             } catch (SQLException e1) {  
  339.                 e1.printStackTrace();  
  340.             }  
  341.             e.printStackTrace();  
  342.         } finally {  
  343.             DbUtils.closeQuietly(conn, pst, rs);  
  344.         }  
  345.   
  346.     }  
  347.   
  348.     public void upload() {  
  349.   
  350.         QueryRunner qr = new QueryRunner(new DBConnection());  
  351.   
  352.         String sql = "update student set student_name = ? where student_id = ?";  
  353.         Object[] params = new Object[] { "seven+++"11 };  
  354.   
  355.         try {  
  356.             System.out.println(qr.update(sql, params));  
  357.         } catch (SQLException e) {  
  358.             e.printStackTrace();  
  359.         }  
  360.   
  361.     }  
  362.   
  363.     public void detele() {  
  364.   
  365.         QueryRunner qr = new QueryRunner(new DBConnection());  
  366.   
  367.         String sql = "delete from student where student_id = ?";  
  368.         Object[] params = new Object[] { 11 };  
  369.   
  370.         try {  
  371.             System.out.println(qr.update(sql, params));  
  372.         } catch (SQLException e) {  
  373.             e.printStackTrace();  
  374.         }  
  375.   
  376.     }  
  377.   
  378. }  


这张是t.queryListThreeTable()的查询效果 
 

这张是t.queryListTwoTable()的查询效果 
 

这张是t.queryTwoTable()的查询效果 
 


最后附上示例代码 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值