JDBC增删改查案例讲解

快来打我* 2024-03-31 11:23 187阅读 0赞

JDBC增删改查案例讲解

简介:这是一个网上非常常见的,JDBC的练习题,系统大家通过本文的讲解,熟悉JDBC的增删改查。

推荐学习路线:JDBC数据库的连接->Connection(数据库连接对象)->Driud数据库连接池的使用->ResultSet->通过PreparedStatement预防SQL注入->JDBC增删改查案例讲解 大家跟着敲完基本就可以JDBC基础毕业了。

题目要求为:
完成商品品牌数据的增删改查操作,把每个操作封装成为对应函数。

  • 查询:查询所有数据
  • 添加:添加品牌
  • 修改:根据id修改
  • 删除:根据id删除

环境准备

  • 数据库表 tb_brand
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

数据库建表语句

  1. -- 删除tb_brand
  2. drop table if exists tb_brand;
  3. -- 创建tb_brand
  4. create table tb_brand (
  5. -- id 主键
  6. id int primary key auto_increment,
  7. -- 品牌名称
  8. brand_name varchar(20),
  9. -- 企业名称
  10. company_name varchar(20),
  11. -- 排序字段
  12. ordered int,
  13. -- 描述信息
  14. description varchar(100),
  15. -- 状态:0:禁用 1:启用
  16. status int
  17. );
  18. -- 添加数据
  19. insert into tb_brand (brand_name, company_name, ordered, description, status)
  20. values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
  21. ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
  22. ('小米', '小米科技有限公司', 50, 'are you ok', 1);
  • 创建工具包pojo
    在这里插入图片描述
    在这里插入图片描述
    Brand类

    package com.study.pojo;

    /**

    • 品牌类
    • alt + 鼠标左键:整列编辑
    • 在实体类中,基本数据类型建议使用其对应的包装类型
      */
      public class Brand {

      // id 主键
      private Integer id;
      // 品牌名称
      private String brandName;
      // 企业名称
      private String companyName;
      // 排序字段
      private Integer ordered;
      // 描述信息
      private String description;
      // 状态:0:禁用 1:启用
      private Integer status;

      public Integer getId() {

      1. return id;

      }

      public void setId(Integer id) {

      1. this.id = id;

      }

      public String getBrandName() {

      1. return brandName;

      }

      public void setBrandName(String brandName) {

      1. this.brandName = brandName;

      }

      public String getCompanyName() {

      1. return companyName;

      }

      public void setCompanyName(String companyName) {

      1. this.companyName = companyName;

      }

      public Integer getOrdered() {

      1. return ordered;

      }

      public void setOrdered(Integer ordered) {

      1. this.ordered = ordered;

      }

      public String getDescription() {

      1. return description;

      }

      public void setDescription(String description) {

      1. this.description = description;

      }

      public Integer getStatus() {

      1. return status;

      }

      public void setStatus(Integer status) {

      1. this.status = status;

      }

      @Override
      public String toString() {

      1. return "Brand{" +
      2. "id=" + id +
      3. ", brandName='" + brandName + '\'' +
      4. ", companyName='" + companyName + '\'' +
      5. ", ordered=" + ordered +
      6. ", description='" + description + '\'' +
      7. ", status=" + status +
      8. '}';

      }
      }

代码讲解

  1. package com.study.jdbc;
  2. import com.alibaba.druid.pool.DruidDataSourceFactory;
  3. import com.study.pojo.Brand;
  4. import javax.sql.DataSource;
  5. import java.io.FileInputStream;
  6. import java.sql.*;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Properties;
  10. public class JDBCDemo {
  11. /**
  12. * 查询所有
  13. * 1. SQL:select * from tb_brand;
  14. * 2. 参数:不需要
  15. * 3. 结果:List<Brand>
  16. */
  17. public static void testSelectAll(Connection conn) throws Exception {
  18. //2. 定义SQL
  19. String sql = "select * from tb_brand;";
  20. //3. 获取pstmt对象
  21. PreparedStatement pstmt = conn.prepareStatement(sql);
  22. //4. 设置参数
  23. //5. 执行SQL
  24. ResultSet rs = pstmt.executeQuery();
  25. //6. 处理结果 List<Brand> 封装Brand对象,装载List集合
  26. Brand brand = null;
  27. List<Brand> brands = new ArrayList<>();
  28. while (rs.next()){
  29. //获取数据
  30. int id = rs.getInt("id");
  31. String brandName = rs.getString("brand_name");
  32. String companyName = rs.getString("company_name");
  33. int ordered = rs.getInt("ordered");
  34. String description = rs.getString("description");
  35. int status = rs.getInt("status");
  36. //封装Brand对象
  37. brand = new Brand();
  38. brand.setId(id);
  39. brand.setBrandName(brandName);
  40. brand.setCompanyName(companyName);
  41. brand.setOrdered(ordered);
  42. brand.setDescription(description);
  43. brand.setStatus(status);
  44. //装载集合
  45. brands.add(brand);
  46. }
  47. System.out.println(brands);
  48. //7. 释放资源
  49. rs.close();
  50. pstmt.close();
  51. }
  52. /**
  53. * 添加
  54. * 1. SQL:insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);
  55. * 2. 参数:需要,除了id之外的所有参数信息
  56. * 3. 结果:boolean
  57. */
  58. public static void testAdd(Connection conn) throws Exception {
  59. // 接收页面提交的参数
  60. String brandName = "香飘飘";
  61. String companyName = "香飘飘";
  62. int ordered = 1;
  63. String description = "绕地球一圈";
  64. int status = 1;
  65. //2. 定义SQL
  66. String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
  67. //3. 获取pstmt对象
  68. PreparedStatement pstmt = conn.prepareStatement(sql);
  69. //4. 设置参数
  70. pstmt.setString(1,brandName);
  71. pstmt.setString(2,companyName);
  72. pstmt.setInt(3,ordered);
  73. pstmt.setString(4,description);
  74. pstmt.setInt(5,status);
  75. //5. 执行SQL
  76. int count = pstmt.executeUpdate(); // 影响的行数
  77. //6. 处理结果
  78. System.out.println(count > 0);
  79. //7. 释放资源
  80. pstmt.close();
  81. }
  82. /**
  83. * 修改
  84. * 1. SQL:
  85. update tb_brand
  86. set brand_name = ?,
  87. company_name= ?,
  88. ordered = ?,
  89. description = ?,
  90. status = ?
  91. where id = ?
  92. * 2. 参数:需要,所有数据
  93. * 3. 结果:boolean
  94. */
  95. public static void testUpdate(Connection conn) throws Exception {
  96. // 接收页面提交的参数
  97. String brandName = "香飘飘";
  98. String companyName = "香飘飘";
  99. int ordered = 1000;
  100. String description = "绕地球三圈";
  101. int status = 1;
  102. int id = 4;
  103. //2. 定义SQL
  104. String sql = " update tb_brand\n" +
  105. " set brand_name = ?,\n" +
  106. " company_name= ?,\n" +
  107. " ordered = ?,\n" +
  108. " description = ?,\n" +
  109. " status = ?\n" +
  110. " where id = ?";
  111. //3. 获取pstmt对象
  112. PreparedStatement pstmt = conn.prepareStatement(sql);
  113. //4. 设置参数
  114. pstmt.setString(1,brandName);
  115. pstmt.setString(2,companyName);
  116. pstmt.setInt(3,ordered);
  117. pstmt.setString(4,description);
  118. pstmt.setInt(5,status);
  119. pstmt.setInt(6,id);
  120. //5. 执行SQL
  121. int count = pstmt.executeUpdate(); // 影响的行数
  122. //6. 处理结果
  123. System.out.println(count > 0);
  124. //7. 释放资源
  125. pstmt.close();
  126. }
  127. /**
  128. * 删除
  129. * 1. SQL:
  130. delete from tb_brand where id = ?
  131. * 2. 参数:需要,id
  132. * 3. 结果:boolean
  133. */
  134. public static void testDeleteById(Connection conn) throws Exception {
  135. // 接收页面提交的参数
  136. int id = 4;
  137. //2. 定义SQL
  138. String sql = " delete from tb_brand where id = ?";
  139. //3. 获取pstmt对象
  140. PreparedStatement pstmt = conn.prepareStatement(sql);
  141. //4. 设置参数
  142. pstmt.setInt(1,id);
  143. //5. 执行SQL
  144. int count = pstmt.executeUpdate(); // 影响的行数
  145. //6. 处理结果
  146. System.out.println(count > 0);
  147. //7. 释放资源
  148. pstmt.close();
  149. }
  150. public static void main(String[] args) throws Exception {
  151. Properties prop = new Properties();
  152. prop.load(new FileInputStream("src/com/study/druid.properties"));
  153. //4. 获取连接池对象
  154. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
  155. //5. 获取数据库连接 Connection
  156. Connection conn = dataSource.getConnection();
  157. // 测试查询全部
  158. testSelectAll(conn);
  159. // 测试添加
  160. testAdd(conn);
  161. // 测试更新
  162. testUpdate(conn);
  163. // 测试删除
  164. testDeleteById(conn);
  165. conn.close(); // 统一删除
  166. }
  167. }

运行结果
在这里插入图片描述

分模块演示

查询所有

  1. public void testSelectAll() throws Exception {
  2. //1. 获取Connection
  3. //3. 加载配置文件
  4. Properties prop = new Properties();
  5. prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
  6. //4. 获取连接池对象
  7. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
  8. //5. 获取数据库连接 Connection
  9. Connection conn = dataSource.getConnection();
  10. //2. 定义SQL
  11. String sql = "select * from tb_brand;";
  12. //3. 获取pstmt对象
  13. PreparedStatement pstmt = conn.prepareStatement(sql);
  14. //4. 设置参数
  15. //5. 执行SQL
  16. ResultSet rs = pstmt.executeQuery();
  17. //6. 处理结果 List<Brand> 封装Brand对象,装载List集合
  18. Brand brand = null;
  19. List<Brand> brands = new ArrayList<>();
  20. while (rs.next()){
  21. //获取数据
  22. int id = rs.getInt("id");
  23. String brandName = rs.getString("brand_name");
  24. String companyName = rs.getString("company_name");
  25. int ordered = rs.getInt("ordered");
  26. String description = rs.getString("description");
  27. int status = rs.getInt("status");
  28. //封装Brand对象
  29. brand = new Brand();
  30. brand.setId(id);
  31. brand.setBrandName(brandName);
  32. brand.setCompanyName(companyName);
  33. brand.setOrdered(ordered);
  34. brand.setDescription(description);
  35. brand.setStatus(status);
  36. //装载集合
  37. brands.add(brand);
  38. }
  39. System.out.println(brands);
  40. //7. 释放资源
  41. rs.close();
  42. pstmt.close();
  43. conn.close();
  44. }

添加数据

  1. /**
  2. * 添加
  3. * 1. SQL:insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);
  4. * 2. 参数:需要,除了id之外的所有参数信息
  5. * 3. 结果:boolean
  6. */
  7. public void testAdd() throws Exception {
  8. // 接收页面提交的参数
  9. String brandName = "香飘飘";
  10. String companyName = "香飘飘";
  11. int ordered = 1;
  12. String description = "绕地球一圈";
  13. int status = 1;
  14. //1. 获取Connection
  15. //3. 加载配置文件
  16. Properties prop = new Properties();
  17. prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
  18. //4. 获取连接池对象
  19. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
  20. //5. 获取数据库连接 Connection
  21. Connection conn = dataSource.getConnection();
  22. //2. 定义SQL
  23. String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
  24. //3. 获取pstmt对象
  25. PreparedStatement pstmt = conn.prepareStatement(sql);
  26. //4. 设置参数
  27. pstmt.setString(1,brandName);
  28. pstmt.setString(2,companyName);
  29. pstmt.setInt(3,ordered);
  30. pstmt.setString(4,description);
  31. pstmt.setInt(5,status);
  32. //5. 执行SQL
  33. int count = pstmt.executeUpdate(); // 影响的行数
  34. //6. 处理结果
  35. System.out.println(count > 0);
  36. //7. 释放资源
  37. pstmt.close();
  38. conn.close();
  39. }

修改数据

  1. /**
  2. * 修改
  3. * 1. SQL:
  4. update tb_brand
  5. set brand_name = ?,
  6. company_name= ?,
  7. ordered = ?,
  8. description = ?,
  9. status = ?
  10. where id = ?
  11. * 2. 参数:需要,所有数据
  12. * 3. 结果:boolean
  13. */
  14. public void testUpdate() throws Exception {
  15. // 接收页面提交的参数
  16. String brandName = "香飘飘";
  17. String companyName = "香飘飘";
  18. int ordered = 1000;
  19. String description = "绕地球三圈";
  20. int status = 1;
  21. int id = 4;
  22. //1. 获取Connection
  23. //3. 加载配置文件
  24. Properties prop = new Properties();
  25. prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
  26. //4. 获取连接池对象
  27. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
  28. //5. 获取数据库连接 Connection
  29. Connection conn = dataSource.getConnection();
  30. //2. 定义SQL
  31. String sql = " update tb_brand\n" +
  32. " set brand_name = ?,\n" +
  33. " company_name= ?,\n" +
  34. " ordered = ?,\n" +
  35. " description = ?,\n" +
  36. " status = ?\n" +
  37. " where id = ?";
  38. //3. 获取pstmt对象
  39. PreparedStatement pstmt = conn.prepareStatement(sql);
  40. //4. 设置参数
  41. pstmt.setString(1,brandName);
  42. pstmt.setString(2,companyName);
  43. pstmt.setInt(3,ordered);
  44. pstmt.setString(4,description);
  45. pstmt.setInt(5,status);
  46. pstmt.setInt(6,id);
  47. //5. 执行SQL
  48. int count = pstmt.executeUpdate(); // 影响的行数
  49. //6. 处理结果
  50. System.out.println(count > 0);
  51. //7. 释放资源
  52. pstmt.close();
  53. conn.close();
  54. }

删除数据

  1. /**
  2. * 删除
  3. * 1. SQL:
  4. delete from tb_brand where id = ?
  5. * 2. 参数:需要,id
  6. * 3. 结果:boolean
  7. */
  8. public void testDeleteById() throws Exception {
  9. // 接收页面提交的参数
  10. int id = 4;
  11. //1. 获取Connection
  12. //3. 加载配置文件
  13. Properties prop = new Properties();
  14. prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
  15. //4. 获取连接池对象
  16. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
  17. //5. 获取数据库连接 Connection
  18. Connection conn = dataSource.getConnection();
  19. //2. 定义SQL
  20. String sql = " delete from tb_brand where id = ?";
  21. //3. 获取pstmt对象
  22. PreparedStatement pstmt = conn.prepareStatement(sql);
  23. //4. 设置参数
  24. pstmt.setInt(1,id);
  25. //5. 执行SQL
  26. int count = pstmt.executeUpdate(); // 影响的行数
  27. //6. 处理结果
  28. System.out.println(count > 0);
  29. //7. 释放资源
  30. pstmt.close();
  31. conn.close();
  32. }

发表评论

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

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

相关阅读

    相关 JDBC增删案例讲解

    JDBC增删改查案例讲解 简介:这是一个网上非常常见的,JDBC的练习题,系统大家通过本文的讲解,熟悉JDBC的增删改查。 推荐学习路线:[JDBC数据库的连接][JD

    相关 JDBC进行增删

    JDBC进行增删改查 本篇博客较为基础,只作学习记录之用。 本篇分为: 下载JDBC的jar包 导入JDBC的jar包 封装数据库连接和关闭