• JDBC增删改查练习案例


    目录

    先创建数据库表

    查询全部信息

    步骤: 非红色为不变的步骤

    添加

    根据ID更新数据

    根据ID删除数据


    先创建数据库表

    1. CREATE DATABASE if not EXISTS info char SET utf8;
    2. use info;
    3. CREATE table tb_brand(
    4. id int PRIMARY key auto_increment,
    5. brand_name varchar(20) COMMENT '商品名称',
    6. company_name VARCHAR(20) COMMENT' 企业名称',
    7. ordered int COMMENT '排序字段',
    8. description VARCHAR(100) COMMENT'描述信息',
    9. `status` int -- 状态信息, 0禁用,1启用
    10. );
    11. INSERT into tb_brand (brand_name,company_name,ordered,description,status)
    12. VALUES('苹果手机','苹果公司', 5,'苹果无敌',0),
    13. ('华为手机','华为公司', 100,'华为无敌',1),
    14. ('小米手机','小米公司',50 ,'小米无敌',1);
    15. SELECT * FROM tb_brand;

    查询全部信息

    步骤: 非红色为不变的步骤

    1: 获取connection

    2:定义 sql

    3:获取 PrepareStatement对象

    4:设置参数

    5:执行sql

    6 处理结果  List  并封装到ArrayList集合

    7:释放资源

    创建brank信息类

    1. //在实体类中,基本数据类型建议使用对应的包装类型,防止默认值影响功能
    2. public class Brand {
    3. private Integer id ;
    4. private String brandName ; //'商品名称'
    5. private String companyName ;//' 企业名称'
    6. private Integer ordered ; //'排序字段',
    7. private String description; //'描述信息'
    8. private Integer status ;//-- 状态信息, 0禁用,1启用
    9. /**
    10. * @return the id
    11. */
    12. public Integer getId() {
    13. return id;
    14. }
    15. /**
    16. * @param id the id to set
    17. */
    18. public void setId(Integer id) {
    19. this.id = id;
    20. }
    21. /**
    22. * @return the brandName
    23. */
    24. public String getBrandName() {
    25. return brandName;
    26. }
    27. /**
    28. * @param brandName the brandName to set
    29. */
    30. public void setBrandName(String brandName) {
    31. this.brandName = brandName;
    32. }
    33. /**
    34. * @return the companyName
    35. */
    36. public String getCompanyName() {
    37. return companyName;
    38. }
    39. /**
    40. * @param companyName the companyName to set
    41. */
    42. public void setCompanyName(String companyName) {
    43. this.companyName = companyName;
    44. }
    45. /**
    46. * @return the ordered
    47. */
    48. public Integer getOrdered() {
    49. return ordered;
    50. }
    51. /**
    52. * @param ordered the ordered to set
    53. */
    54. public void setOrdered(Integer ordered) {
    55. this.ordered = ordered;
    56. }
    57. /**
    58. * @return the description
    59. */
    60. public String getDescription() {
    61. return description;
    62. }
    63. /**
    64. * @param description the description to set
    65. */
    66. public void setDescription(String description) {
    67. this.description = description;
    68. }
    69. /**
    70. * @return the status
    71. */
    72. public Integer getStatus() {
    73. return status;
    74. }
    75. /**
    76. * @param status the status to set
    77. */
    78. public void setStatus(Integer status) {
    79. this.status = status;
    80. }
    81. @Override
    82. public String toString() {
    83. return "Brand [id=" + id + ", brandName=" + brandName + ", companyName=" + companyName + ", ordered=" + ordered
    84. + ", description=" + description + ", status=" + status + "]";
    85. }
    86. public Brand(Integer id, String brandName, String companyName, Integer ordered, String description,
    87. Integer status) {
    88. super();
    89. this.id = id;
    90. this.brandName = brandName;
    91. this.companyName = companyName;
    92. this.ordered = ordered;
    93. this.description = description;
    94. this.status = status;
    95. }
    96. public Brand() {
    97. super();
    98. }
    99. }

    开始查询

    1. import java.sql.Connection;
    2. import java.sql.DriverManager;
    3. import java.sql.PreparedStatement;
    4. import java.sql.ResultSet;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. public class BrandTest {
    8. public static void main(String[] args) throws Exception {
    9. //获取连接
    10. String url="jdbc:mysql:///info";
    11. String userName="root";
    12. String passWord="666";
    13. Connection con=DriverManager.getConnection(url, userName, passWord);
    14. String sql1="select * from tb_brand";
    15. PreparedStatement ps=con.prepareStatement(sql1);
    16. ResultSet rs=ps.executeQuery();
    17. Brand brand=null;
    18. List< Brand> list=new ArrayList<>();
    19. while(rs.next()) {
    20. int id=rs.getInt("id");
    21. String brandName =rs.getString("brand_name");
    22. String companyName= rs.getString("company_name");
    23. int ordered= rs.getInt("ordered");
    24. String description= rs.getString("description");
    25. int status= rs.getInt("status");
    26. list.add(new Brand(id, brandName, companyName, ordered, description, status));
    27. }
    28. System.out.println(list);
    29. rs.close();
    30. ps.close();
    31. con.close();
    32. }
    33. }

    添加

    1编写SQL语句

    2是否需要参数?需要:除id以外的所有数据

    3:返回结果如何封装? Boolean

    1. import java.sql.Connection;
    2. import java.sql.DriverManager;
    3. import java.sql.PreparedStatement;
    4. public class Test3 {
    5. public static void main(String[] args) throws Exception {
    6. String url="jdbc:mysql:///info";
    7. String userName="root";
    8. String password="666";
    9. Connection con=DriverManager.getConnection(url, userName, password);
    10. //模拟网页传来的信息
    11. String brandName="香飘飘";
    12. String companyName="香飘飘公司";
    13. int ordered=5;
    14. String description="绕地球一圈";
    15. int status=0;
    16. String sql="INSERT into tb_brand (brand_name,company_name,ordered,description,status) VALUES(?,?,?,?,?) ";
    17. PreparedStatement ps=con.prepareStatement(sql);
    18. //设置参数
    19. ps.setString(1, brandName);
    20. ps.setString(2, companyName);
    21. ps.setInt(3, ordered);
    22. ps.setString(4, description);
    23. ps.setInt(5, status);
    24. int affect=ps.executeUpdate();
    25. System.out.println(affect>0);
    26. ps.close();
    27. con.close();
    28. }
    29. }

    根据ID更新数据

    1 编写sql语句

    2 是否需要参数?需要:Brand对象的所有数据

    3: 返回结果如何封装Boolean

    根据ID删除数据

    1 编写sql语句

    2是否需要参数 需要id

    3 返回结果如何封装 boolean

    1. import java.sql.Connection;
    2. import java.sql.DriverManager;
    3. import java.sql.PreparedStatement;
    4. public class deleteTest {
    5. public static void main(String[] args) throws Exception {
    6. // TODO Auto-generated method stub
    7. String url="jdbc:mysql:///info";
    8. String userName="root";
    9. String password="666";
    10. Connection con=DriverManager.getConnection(url, userName, password);
    11. //模拟网页传来的信息
    12. int id=1;
    13. String sql="DELETE FROM tb_brand WHERE id=?;";
    14. PreparedStatement ps=con.prepareStatement(sql);
    15. //设置参数
    16. ps.setInt(1, id);
    17. int affect=ps.executeUpdate();
    18. System.out.println(affect>0);
    19. ps.close();
    20. con.close();
    21. }
    22. }

  • 相关阅读:
    (笔记整理未完成)【图论】强连通分量——Tarjan算法
    MyBatis的基本操作
    这世上又多了一只爬虫(spiderflow)
    Flink 窗口处理函数 WindowFunction
    ubuntu系统使用bing wallpaper壁纸
    文心一言 VS 讯飞星火 VS chatgpt (104)-- 算法导论10.1 2题
    变量转为json格式ivx 自定义图表 echarts自定义图表
    了解这6个原因 才知道为什么需要FTP替代方案了
    React-5 React Hooks知识
    【系统设计】指标监控和告警系统
  • 原文地址:https://blog.csdn.net/m0_64365315/article/details/126152343