增删改查——DBUtils

迈不过友情╰ 2023-08-17 16:09 142阅读 0赞

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

2、删除

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

3、修改

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

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. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.ArrayListHandler;
  7. import JDBCUtils.JDBCUtils;
  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 * from Student where studentno=?";
  15. Object[] select = { 20191811 };
  16. List<Object[]> list = qr.query(con, sql, new ArrayListHandler(),
  17. select);
  18. // 将记录封装到一个装有Object[]的List集合中
  19. for (Object[] arr : list) {
  20. System.out.println(Arrays.toString(arr));
  21. }
  22. } catch (SQLException e) {
  23. throw new RuntimeException(e);
  24. }
  25. JDBCUtils.close(con);
  26. }
  27. }

(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. import java.sql.Connection;
  67. import java.sql.SQLException;
  68. import java.util.List;
  69. import org.apache.commons.dbutils.QueryRunner;
  70. import org.apache.commons.dbutils.handlers.BeanListHandler;
  71. import JDBCUtils.JDBCUtils;
  72. public class SeleteDemo {
  73. public static void main(String[] args) {
  74. Connection con = null;
  75. try {
  76. con = JDBCUtils.getConnection();
  77. QueryRunner qr = new QueryRunner();
  78. String sql = "Select * from Student where studentno=?";
  79. Object[] select = { 20191811 };
  80. List<Student> list = qr.query(con, sql,new BeanListHandler<Student>((Student.class)), select);
  81. // 将记录封装到一个装有Object[]的List集合中
  82. for (Student s : list) {
  83. System.out.println(s);
  84. }
  85. } catch (SQLException e) {
  86. throw new RuntimeException(e);
  87. }
  88. JDBCUtils.close(con);
  89. }
  90. }

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

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

查询学生的学号:

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

(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. import JDBCUtils.JDBCUtils;
  6. public class SeleteDemo {
  7. public static void main(String[] args) {
  8. Connection con = null;
  9. try {
  10. con = JDBCUtils.getConnection();
  11. QueryRunner qr = new QueryRunner();
  12. String sql = "SELECT COUNT(sname) FROM Student";
  13. Object[] select = {};
  14. long count= qr.query(con, sql, new ScalarHandler<Long>(), select);
  15. System.out.println(count);
  16. } catch (SQLException e) {
  17. throw new RuntimeException(e);
  18. }
  19. JDBCUtils.close(con);
  20. }
  21. }

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

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

转载于:https://www.cnblogs.com/zhai1997/p/11382079.html

发表评论

表情:
评论列表 (有 0 条评论,142人围观)

还没有评论,来说两句吧...

相关阅读