微信登录

JDBC - PS - 增删改 - 封装7句->2句

第一次封装1、2、3、7

1、读取配置文件中的4个基本信息,连接数据库
2.加载驱动
3.获取连接
7、关闭数据库
新建xxx.xxx.util包
新建util.java类

JDBCutil.java

  1. package com.atguigu3.util;
  2. import java.io.InputStream;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9. /**
  10. *
  11. * @Description 操作数据库的工具类
  12. * @author shkstart Email:shkstart@126.com
  13. * @version
  14. * @date 上午9:10:02
  15. *
  16. */
  17. public class JDBCUtils {
  18. /**
  19. *
  20. * @Description 获取数据库的连接
  21. * @author shkstart
  22. * @date 上午9:11:23
  23. * @return
  24. * @throws Exception
  25. */
  26. public static Connection getConnection() throws Exception {
  27. // 1.读取配置文件中的4个基本信息
  28. InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
  29. Properties pros = new Properties();
  30. pros.load(is);
  31. String user = pros.getProperty("user");
  32. String password = pros.getProperty("password");
  33. String url = pros.getProperty("url");
  34. String driverClass = pros.getProperty("driverClass");
  35. // 2.加载驱动
  36. Class.forName(driverClass);
  37. // 3.获取连接
  38. Connection conn = DriverManager.getConnection(url, user, password);
  39. return conn;
  40. }
  41. /**
  42. *
  43. * @Description 关闭连接和Statement的操作
  44. * @author shkstart
  45. * @date 上午9:12:40
  46. * @param conn
  47. * @param ps
  48. */
  49. public static void closeResource(Connection conn,Statement ps){
  50. try {
  51. if(ps != null)
  52. ps.close();
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. try {
  57. if(conn != null)
  58. conn.close();
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. /**
  64. *
  65. * @Description 关闭资源操作
  66. * @author shkstart
  67. * @date 上午10:21:15
  68. * @param conn
  69. * @param ps
  70. * @param rs
  71. */
  72. public static void closeResource(Connection conn,Statement ps,ResultSet rs){
  73. try {
  74. if(ps != null)
  75. ps.close();
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. try {
  80. if(conn != null)
  81. conn.close();
  82. } catch (SQLException e) {
  83. e.printStackTrace();
  84. }
  85. try {
  86. if(rs != null)
  87. rs.close();
  88. } catch (SQLException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }

第二次封装1、2、3、4、5、6、7

  1. //通用的增删改操作
  2. public void update(String sql,Object ...args){//sql中占位符的个数与可变形参的长度相同!
  3. Connection conn = null;
  4. PreparedStatement ps = null;
  5. try {
  6. //1.获取数据库的连接
  7. conn = JDBCUtils.getConnection();
  8. //2.预编译sql语句,返回PreparedStatement的实例
  9. ps = conn.prepareStatement(sql);
  10. //3.填充占位符
  11. for(int i = 0;i < args.length;i++){
  12. ps.setObject(i + 1, args[i]);//小心参数声明错误!!
  13. }
  14. //4.执行
  15. //方式一:
  16. //return ps.execute();
  17. //方式二:返回int
  18. return ps.executeUpdate();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }finally{
  22. //5.资源的关闭
  23. JDBCUtils.closeResource(conn, ps);
  24. }
  25. }
返回方法 ps.execute(): ps.executeUpdate();
查询操作 有返回结果,返回true
增、删、改操作 没有返回结果,返回false. 成功返回1,失败返回0

使用(2句:1句sql + ?、1句参数)

  1. @Test
  2. public void testCommonUpdate(){
  3. // String sql = "delete from customers where id = ?";
  4. // update(sql,3);
  5. String sql = "update `order` set order_name = ? where order_id = ?";
  6. int insertCount = update(sql,"DD","2");
  7. if(insertCount > 0){
  8. System.out.println("添加成功");
  9. }else{
  10. System.out.println("添加失败");
  11. }
  12. }
JDBC - PS - 增删改 - 封装7句-&gt;2句