image.png
    使用crtl加R快捷键即可一起改变
    image.png
    alt + 鼠标左键,即可一起编辑 ctrl + alt + L 将所有的; 号移动到变量这里来
    image.png
    // 包转类的默认值为null
    // 定义数据库的变量值时:要用包装类,不能用基本数据类型,因为
    // 因为如果没给值的话,基本数据Integer类型为0,在数据库中会出现参数错误

    image.png
    image.png
    image.png

    1. package com.itheima.example;
    2. import com.alibaba.druid.pool.DruidDataSourceFactory;
    3. import com.itheima.pojo.Brand;
    4. import org.junit.Test;
    5. import javax.sql.DataSource;
    6. import java.io.FileInputStream;
    7. import java.io.FileNotFoundException;
    8. import java.sql.Connection;
    9. import java.sql.PreparedStatement;
    10. import java.sql.ResultSet;
    11. import java.util.ArrayList;
    12. import java.util.List;
    13. import java.util.Properties;
    14. public class BrandTest {
    15. /**
    16. * 品牌数据的增删改查操作
    17. * 1. SQL:select * from tb_brand
    18. * 2. 参数:不需要
    19. * 3. 结果:List<Brand>
    20. */
    21. @Test
    22. public void testSelectAll() throws Exception {
    23. // 1. 获取Connection
    24. // 3. 加载配置文件
    25. // 3. 加载配置文件 创建配置文件对象
    26. Properties prop = new Properties();
    27. // 将配置文件用文件的输入流,加载到内存中
    28. prop.load(new FileInputStream("C:\\Users\\aufs\\code\\IdeaProjects\\jdbc\\jdbc-demo\\src\\druid.properties"));
    29. // 4. 获取连接池对象
    30. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);// 将创建的配置文件对象传入进去
    31. // 5. 获取数据库连接 Connection
    32. Connection connection = dataSource.getConnection();
    33. // 2. 定义SQL语句
    34. String sql = "select * from tb_brand;";
    35. // 3. 获取pstmt对象
    36. PreparedStatement pstmt = connection.prepareStatement(sql);
    37. // 4. 设置参数
    38. // 执行sql
    39. ResultSet rs = pstmt.executeQuery();
    40. // 6. 处理结果List<Brand> 封装Brand对象,装载List集合
    41. // 把brand对象放放外面去
    42. Brand brand = null; // 在这里创建一个局部变量,然后之后再循环里面
    43. // 创建对象就可以直接赋值(复用)给这个变量(可以节约栈空间)
    44. List<Brand> brands = new ArrayList<>();
    45. while (rs.next()){
    46. // 获取数据
    47. int id = rs.getInt("id");
    48. String brandName = rs.getString("brand_name");
    49. String companyName = rs.getString("company_name");
    50. int ordered = rs.getInt("ordered");
    51. String description = rs.getString("description");
    52. int status = rs.getInt("status");
    53. // 封装Brand对象
    54. // 把brand对象放放外面去,相当于节约栈空间,这里复用其局部变量
    55. brand = new Brand(); // 创建对象直接赋值给局部变量brand(Brand类型)
    56. brand.setId(id);
    57. brand.setBrandName(brandName);
    58. brand.setCompanyName(companyName);
    59. brand.setOrdered(ordered);
    60. brand.setDescription(description);
    61. brand.setStatus(status);
    62. // 装载集合
    63. brands.add(brand); // 将brand对象放入集合中去
    64. }
    65. // 打印查看集合效果
    66. System.out.println(brands);
    67. // 7. 释放资源
    68. rs.close();
    69. pstmt.close();
    70. connection.close();
    71. }
    72. /**
    73. * 添加
    74. * 1. SQL:insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);
    75. * 2. 参数:需要,除了id之外的所有参数信息
    76. * 3. 结果:boolean
    77. */
    78. @Test
    79. public void testAdd() throws Exception {
    80. // 接收页面提交的参数
    81. String brandName = "香飘飘";
    82. String companyName = "香飘飘";
    83. int ordered = 1;
    84. String description = "绕地球一圈";
    85. int status = 1;
    86. //1. 获取Connection
    87. //3. 加载配置文件
    88. Properties prop = new Properties();
    89. prop.load(new FileInputStream("C:\\Users\\aufs\\code\\IdeaProjects\\jdbc\\jdbc-demo\\src\\druid.properties"));
    90. //4. 获取连接池对象
    91. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    92. //5. 获取数据库连接 Connection
    93. Connection conn = dataSource.getConnection();
    94. //2. 定义SQL
    95. String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
    96. //3. 获取pstmt对象
    97. PreparedStatement pstmt = conn.prepareStatement(sql);
    98. //4. 设置参数
    99. pstmt.setString(1,brandName);
    100. pstmt.setString(2,companyName);
    101. pstmt.setInt(3,ordered);
    102. pstmt.setString(4,description);
    103. pstmt.setInt(5,status);
    104. //5. 执行SQL
    105. int count = pstmt.executeUpdate(); // 影响的行数
    106. //6. 处理结果
    107. System.out.println(count > 0);
    108. //7. 释放资源
    109. pstmt.close();
    110. conn.close();
    111. }
    112. /**
    113. * 修改
    114. * 1. SQL:
    115. update tb_brand
    116. set brand_name = ?,
    117. company_name= ?,
    118. ordered = ?,
    119. description = ?,
    120. status = ?
    121. where id = ?
    122. * 2. 参数:需要,所有数据
    123. * 3. 结果:boolean
    124. */
    125. @Test
    126. public void testUpdate() throws Exception {
    127. // 接收页面提交的参数
    128. String brandName = "香飘飘";
    129. String companyName = "香飘飘";
    130. int ordered = 1000; // 排序
    131. String description = "绕地球三圈";
    132. int status = 1;
    133. int id = 4;
    134. //1. 获取Connection
    135. //3. 加载配置文件
    136. Properties prop = new Properties();
    137. prop.load(new FileInputStream("C:\\Users\\aufs\\code\\IdeaProjects\\jdbc\\jdbc-demo\\src\\druid.properties"));
    138. //4. 获取连接池对象
    139. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    140. //5. 获取数据库连接 Connection
    141. Connection conn = dataSource.getConnection();
    142. //2. 定义SQL
    143. String sql = "update tb_brand\n" +
    144. " set brand_name = ?,\n" +
    145. " company_name= ?,\n" +
    146. " ordered = ?,\n" +
    147. " description = ?,\n" +
    148. " status = ?\n" +
    149. " where id = ?";
    150. //3. 获取pstmt对象
    151. PreparedStatement pstmt = conn.prepareStatement(sql);
    152. //4. 设置参数
    153. pstmt.setString(1,brandName);
    154. pstmt.setString(2,companyName);
    155. pstmt.setInt(3,ordered);
    156. pstmt.setString(4,description);
    157. pstmt.setInt(5,status);
    158. pstmt.setInt(6,id);
    159. //5. 执行SQL
    160. int count = pstmt.executeUpdate(); // 影响的行数
    161. //6. 处理结果
    162. System.out.println(count > 0);
    163. //7. 释放资源
    164. pstmt.close();
    165. conn.close();
    166. }
    167. /**
    168. * 删除
    169. * 1. SQL:
    170. delete from tb_brand where id = ?
    171. * 2. 参数:需要,id
    172. * 3. 结果:boolean
    173. */
    174. @Test
    175. public void testDeleteById() throws Exception {
    176. // 接收页面提交的参数
    177. int id = 4;
    178. //1. 获取Connection
    179. //3. 加载配置文件
    180. Properties prop = new Properties();
    181. prop.load(new FileInputStream("C:\\Users\\aufs\\code\\IdeaProjects\\jdbc\\jdbc-demo\\src\\druid.properties"));
    182. //4. 获取连接池对象
    183. DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    184. //5. 获取数据库连接 Connection
    185. Connection conn = dataSource.getConnection();
    186. //2. 定义SQL
    187. String sql = "delete from tb_brand where id = ?";
    188. //3. 获取pstmt对象
    189. PreparedStatement pstmt = conn.prepareStatement(sql);
    190. //4. 设置参数
    191. pstmt.setInt(1,id);
    192. //5. 执行SQL
    193. int count = pstmt.executeUpdate(); // 影响的行数
    194. //6. 处理结果
    195. System.out.println(count > 0);
    196. //7. 释放资源
    197. pstmt.close();
    198. conn.close();
    199. }
    200. }