利用QueryRunner类实现对数据库的增删改查操作,需要先导入jar包:commons-dbutils-1.6。利用QueryRunner类可以实现对数据步骤的简化。

1、添加

运用JDBC工具类实现连接:

  1. package JDBCUtils;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Properties;
  10.  
  11. public class JDBCUtils {
  12. private static Connection con;
  13. private static String driver;
  14. private static String url;
  15. private static String username;
  16. private static String password;
  17.  
  18. static {// 静态代码块只执行一次,获取一次信息即可
  19. try {
  20. readConfig();
  21. Class.forName(driver);
  22. con = DriverManager.getConnection(url, username, password);
  23. } catch (Exception ex) {
  24. throw new RuntimeException("数据库连接失败");
  25. }
  26. }
  27. /*
  28. * getClassLoader();返回该类的加载器
  29. * getResourceAsStream();查找具有给定名称的资源
  30. */
  31. private static void readConfig() {
  32. InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("JDBC.properties");
  33. Properties pro = new Properties();
  34. try {
  35. pro.load(in);
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. driver = pro.getProperty("driver");
  40. url = pro.getProperty("url");
  41. username = pro.getProperty("username");
  42. password = pro.getProperty("password");
  43. }
  44.  
  45. public static Connection getConnection() {
  46. return con;
  47. }
  48. public static void close(Connection con) {
  49.  
  50. if (con != null) {
  51. try {
  52. con.close();
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. System.out.println("con流关闭异常!");
  56. }
  57. }
  58.  
  59. }
  60. public static void close(Connection con, Statement stat) {
  61.  
  62. if (stat != null) {
  63. try {
  64. stat.close();
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. System.out.println("stat流关闭异常!");
  68. }
  69. }
  70.  
  71. if (con != null) {
  72. try {
  73. con.close();
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. System.out.println("con流关闭异常!");
  77. }
  78. }
  79.  
  80. }
  81.  
  82. public static void close(Connection con, Statement stat, ResultSet rs) {
  83. if (rs != null) {
  84. try {
  85. rs.close();
  86. } catch (SQLException e) {
  87. e.printStackTrace();
  88. System.out.println("rs流关闭异常!");
  89. }
  90. }
  91.  
  92. if (stat != null) {
  93. try {
  94. stat.close();
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. System.out.println("stat流关闭异常!");
  98. }
  99. }
  100.  
  101. if (con != null) {
  102. try {
  103. con.close();
  104. } catch (SQLException e) {
  105. e.printStackTrace();
  106. System.out.println("con流关闭异常!");
  107. }
  108. }
  109.  
  110. }
  111. }
  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import org.apache.commons.dbutils.QueryRunner;
  4.  
  5. import JDBCUtils.JDBCUtils;
  6.  
  7. public class add {
  8.  
  9. public static void main(String[] args) {
  10. Connection con = null;
  11. try {
  12. con = JDBCUtils.getConnection();
  13. QueryRunner qr = new QueryRunner();
  14. String sql = "INSERT INTO student(studentno,sname,sex,birthday,classno,point,phone,email) VALUES(?,?,?,?,?,?,?,?)";
  15. Object[] ", "Jack", "", "1988-12-01",
  16. ", "Tom.@3218n.com" };
  17.  
  18. int num = qr.update(con, sql, params);
  19. System.out.println("添加了" + num + "行");
  20.  
  21. } catch (SQLException e) {
  22. throw new RuntimeException(e);
  23. }
  24. JDBCUtils.close(con);
  25. }
  26. }

2、删除

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3.  
  4. import org.apache.commons.dbutils.QueryRunner;
  5.  
  6. import JDBCUtils.JDBCUtils;
  7.  
  8. public class DeleteDemo {
  9. public static void main(String[] args) {
  10. Connection con = null;
  11. try {
  12. con = JDBCUtils.getConnection();
  13. QueryRunner qr = new QueryRunner();
  14. String sql = "DELETE from Student where sname =?";
  15. Object[] delete = { "Tom" };
  16. qr.update(con, sql, delete);
  17.  
  18. } catch (SQLException e) {
  19. throw new RuntimeException(e);
  20. }
  21. JDBCUtils.close(con);
  22. }
  23. }

3、修改

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3.  
  4. import org.apache.commons.dbutils.QueryRunner;
  5.  
  6. import JDBCUtils.JDBCUtils;
  7.  
  8. public class UpdateDemo {
  9. public static void main(String[] args) {
  10. Connection con = null;
  11. try {
  12. con = JDBCUtils.getConnection();
  13. QueryRunner qr = new QueryRunner();
  14. String sql = "Update Student set classno=? Where sname='韩吟秋'";
  15. Object[] update = { " };
  16. qr.update(con, sql, update);
  17.  
  18. } catch (SQLException e) {
  19. throw new RuntimeException(e);
  20. }
  21. JDBCUtils.close(con);
  22. }
  23. }

4、查询

(1)

ArrayHandler: 将结果集的第一行存储到Object[]数组中

ArrayListHandler: 将结果集的每一行存储到Object[]数组中

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import java.util.Arrays;
  4. import java.util.List;
  5.  
  6. import org.apache.commons.dbutils.QueryRunner;
  7. import org.apache.commons.dbutils.handlers.ArrayListHandler;
  8.  
  9. import JDBCUtils.JDBCUtils;
  10.  
  11. public class SeleteDemo {
  12. public static void main(String[] args) {
  13. Connection con = null;
  14. try {
  15. con = JDBCUtils.getConnection();
  16. QueryRunner qr = new QueryRunner();
  17. String sql = "Select * from Student where studentno=?";
  18. Object[] };
  19. List<Object[]> list = qr.query(con, sql, new ArrayListHandler(),
  20. select);
  21. // 将记录封装到一个装有Object[]的List集合中
  22. for (Object[] arr : list) {
  23. System.out.println(Arrays.toString(arr));
  24. }
  25.  
  26. } catch (SQLException e) {
  27. throw new RuntimeException(e);
  28. }
  29. JDBCUtils.close(con);
  30. }
  31.  
  32. }

(2)

BeanHandler:结果集中第一条记录封装到一个指定的javaBean中。

BeanListHandler:结果集中每一条记录封装到javaBean中,再将javaBean封装到list集合中。

  1. public class Student {
  2. private String studentno;
  3. private String sname;
  4. private String sex;
  5. private String birthday;
  6. private String classno;
  7. private String point;
  8. private String phone;
  9. private String email;
  10. public String getStudentno() {
  11. return studentno;
  12. }
  13. public void setStudentno(String studentno) {
  14. this.studentno = studentno;
  15. }
  16. public String getSname() {
  17. return sname;
  18. }
  19. public void setSname(String sname) {
  20. this.sname = sname;
  21. }
  22. public String getSex() {
  23. return sex;
  24. }
  25. public void setSex(String sex) {
  26. this.sex = sex;
  27. }
  28. public String getBirthday() {
  29. return birthday;
  30. }
  31. public void setBirthday(String birthday) {
  32. this.birthday = birthday;
  33. }
  34. @Override
  35. public String toString() {
  36. return "Student [studentno=" + studentno + ", sname=" + sname + ", sex="
  37. + sex + ", birthday=" + birthday + ", classno=" + classno
  38. + ", point=" + point + ", phone=" + phone + ", email=" + email
  39. + "]";
  40. }
  41. public String getClassno() {
  42. return classno;
  43. }
  44. public void setClassno(String classno) {
  45. this.classno = classno;
  46. }
  47. public String getPoint() {
  48. return point;
  49. }
  50. public void setPoint(String point) {
  51. this.point = point;
  52. }
  53. public String getPhone() {
  54. return phone;
  55. }
  56. public void setPhone(String phone) {
  57. this.phone = phone;
  58. }
  59. public String getEmail() {
  60. return email;
  61. }
  62. public void setEmail(String email) {
  63. this.email = email;
  64. }
  65.  
  66. }
  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4.  
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;
  7.  
  8. import JDBCUtils.JDBCUtils;
  9.  
  10. public class SeleteDemo {
  11. public static void main(String[] args) {
  12. Connection con = null;
  13. try {
  14. con = JDBCUtils.getConnection();
  15. QueryRunner qr = new QueryRunner();
  16. String sql = "Select * from Student where studentno=?";
  17. Object[] };
  18. List<Student> list = qr.query(con, sql,new BeanListHandler<Student>((Student.class)), select);
  19. // 将记录封装到一个装有Object[]的List集合中
  20. for (Student s : list) {
  21. System.out.println(s);
  22. }
  23.  
  24. } catch (SQLException e) {
  25. throw new RuntimeException(e);
  26. }
  27. JDBCUtils.close(con);
  28. }
  29.  
  30. }

(3)ColumnListHandler将结果集中指定的列封装到List集合。

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4.  
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.ColumnListHandler;
  7.  
  8. import JDBCUtils.JDBCUtils;
  9.  
  10. public class SeleteDemo {
  11. public static void main(String[] args) {
  12. Connection con = null;
  13. try {
  14. con = JDBCUtils.getConnection();
  15. QueryRunner qr = new QueryRunner();
  16. String sql = "Select * from Student where studentno=?";
  17. Object[] };
  18. List<String> list = qr.query(con, sql,new ColumnListHandler<String>(), select);
  19. // 将记录封装到一个装有Object[]的List集合中
  20. for (String str: list) {
  21. System.out.println(str);
  22. }
  23.  
  24. } catch (SQLException e) {
  25. throw new RuntimeException(e);
  26. }
  27. JDBCUtils.close(con);
  28. }
  29.  
  30. }

查询学生的学号:

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4.  
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.ColumnListHandler;
  7.  
  8. import JDBCUtils.JDBCUtils;
  9.  
  10. public class SeleteDemo {
  11. public static void main(String[] args) {
  12. Connection con = null;
  13. try {
  14. con = JDBCUtils.getConnection();
  15. QueryRunner qr = new QueryRunner();
  16. String sql = "Select studentno from Student ";
  17. Object[] select = {};
  18. List<String> list = qr.query(con, sql,new ColumnListHandler<String>(), select);
  19. // 将记录封装到一个装有Object[]的List集合中
  20. for (String str: list) {
  21. System.out.println(str);
  22. }
  23.  
  24. } catch (SQLException e) {
  25. throw new RuntimeException(e);
  26. }
  27. JDBCUtils.close(con);
  28. }
  29.  
  30. }

(4)ScalarHandler返回一个数据

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import org.apache.commons.dbutils.QueryRunner;
  4. import org.apache.commons.dbutils.handlers.ScalarHandler;
  5.  
  6. import JDBCUtils.JDBCUtils;
  7.  
  8. public class SeleteDemo {
  9. public static void main(String[] args) {
  10. Connection con = null;
  11. try {
  12. con = JDBCUtils.getConnection();
  13. QueryRunner qr = new QueryRunner();
  14. String sql = "SELECT COUNT(sname) FROM Student";
  15. Object[] select = {};
  16. long count= qr.query(con, sql, new ScalarHandler<Long>(), select);
  17. System.out.println(count);
  18. } catch (SQLException e) {
  19. throw new RuntimeException(e);
  20. }
  21. JDBCUtils.close(con);
  22. }
  23.  
  24. }

(5)MapHandler:将结果集的第一行封装到Map集合中

MapListHandler:将结果集的多条记录封装到一个集合中

  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import org.apache.commons.dbutils.QueryRunner;
  7. import org.apache.commons.dbutils.handlers.MapListHandler;
  8.  
  9. import JDBCUtils.JDBCUtils;
  10.  
  11. public class SeleteDemo {
  12. public static void main(String[] args) {
  13. Connection con = null;
  14. try {
  15. con = JDBCUtils.getConnection();
  16. QueryRunner qr = new QueryRunner();
  17. String sql = "Select studentno from Student ";
  18. Object[] select = {};
  19. List<Map<String,Object>> list = qr.query(con, sql, new MapListHandler(),select);
  20.  
  21. // 将记录封装到一个装有Object[]的List集合中
  22. for (Map<String,Object> map : list) {
  23. for(String key : map.keySet()){
  24. System.out.print(key+"..."+map.get(key));
  25. }
  26. System.out.println();
  27. }
  28.  
  29. } catch (SQLException e) {
  30. throw new RuntimeException(e);
  31. }
  32. JDBCUtils.close(con);
  33. }
  34.  
  35. }

增删改查——DBUtils的更多相关文章

  1. dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)

    jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...

  2. MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. mvc模式jsp+servel+dbutils oracle基本增删改查demo

    mvc模式jsp+servel+dbutils oracle基本增删改查demo 下载地址

  4. 开源工具DbUtils的使用(数据库的增删改查)

    开源工具DbUtils的使用(数据库的增删改查) 一.DbUtils简介: DBUtils是apache下的一个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果 ...

  5. Java Web(十) JDBC的增删改查,C3P0等连接池,dbutils框架的使用

    前面做了一个非常垃圾的小demo,真的无法直面它,菜的抠脚啊,真的菜,好好努力把.菜鸡. --WH 一.JDBC是什么? Java Data Base Connectivity,java数据库连接,在 ...

  6. python操作mysql数据库增删改查的dbutils实例

    python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...

  7. 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类

    在上一篇文章中<使用DbUtils实现增删改查>,发现运行runner.query()这行代码时.须要自己去处理查询到的结果集,比較麻烦.这行代码的原型是: public Object q ...

  8. Android 利用xUtils框架实现对sqllite的增删改查

    首先下载xUtils,下载地址:https://github.com/wyouflf/xUtils  把下载好的文件压缩,把里面的jar包拷进项目中如图所示: 这里新建一个User类进行测试增删改查 ...

  9. MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

随机推荐

  1. 8.15 day33 进程池与线程池_协程_IO模型(了解)

    进程池和线程池 开进程开线程都需要消耗资源,只不过两者比较的情况线程消耗的资源比较少 在计算机能够承受范围之内最大限度的利用计算机 什么是池? ​ 在保证计算机硬件安全的情况下最大限度地利用计算机 ​ ...

  2. 8.12 day31 进程间通信 Queue队列使用 生产者消费者模型 线程理论 创建及对象属性方法 线程互斥锁 守护线程

    进程补充 进程通信 要想实现进程间通信,可以用管道或者队列 队列比管道更好用(队列自带管道和锁) 管道和队列的共同特点:数据只有一份,取完就没了 无法重复获取用一份数据 队列特点:先进先出 堆栈特点: ...

  3. c++智能指针介绍

    C++11标准引入了boost库中的智能指针,给C++开发时的内存管理提供了极大的方便.接下来这篇文件介绍shared_ptr/weak_ptr内部实现原理及使用细节. C++不像java有内存回收机 ...

  4. 《Java 8 in Action》Chapter 5:使用流

    流让你从外部迭代转向内部迭代,for循环显示迭代不用再写了,流内部管理对集合数据的迭代.这种处理数据的方式很有用,因为你让Stream API管理如何处理数据.这样Stream API就可以在背后进行 ...

  5. Oracle面对“数据倾斜列使用绑定变量”场景的解决方案

    1.背景知识介绍 2.构造测试用例 3.场景测试 4.总结 1.背景知识介绍     我们知道,Oracle在传统的OLTP(在线事务处理)类系统中,强烈推荐使用绑定变量,这样可以有效的减少硬解析从而 ...

  6. 随笔编号-08 MYSQL 存储过程,5分钟执行调用过程;

    delimiter //DROP PROCEDUREIF EXISTS jdt.day_instan_data_task// CREATE PROCEDURE jdt.day_instan_data_ ...

  7. python 20 规范化目录

    目录 规范化目录 1. 划归固定的路径: 2. 划分文件 2.1 seetings 配置文件 2.2 common 公共组件文件 2.3 src 主文件 2.4 starts 项目启动文件 2.5 类 ...

  8. HDU 6313

    题意略. 思路:数论题. #include<bits/stdc++.h> using namespace std; ; const int maxn = p * p; ][maxn + ] ...

  9. 整数 布尔值 字符串 for循环

    整型和布尔值的转换 整型---数字(int) 用于比较和运算 32位范围 -2** 31到2 **32-1 64位范围-2** 63到2** 63-1 十进制转二进制计算方法: 15的二进制为 15% ...

  10. JavaScript String 字符串方法

    JavaScript String 字符串方法汇总   1.str.indexOf() 方法查找字符串中的字符串  返回   字符串中指定文本首次出现的索引(位置)       JavaScript ...