JDBC练习——完成商品品牌数据的增删改查操作 一、准备环境 1、数据库表tb_brand
2、实体类Brand
public class Brand {
private Integer id;
private String brandName;
private String companyName;
private Integer ordered;
private String description;
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
3、测试用例 期望把代码放到一个包内
package com.itheima.example;
public class BrandTest {
}
二、增删改查功能实现
- 查询:查询所有数据
- 添加:添加品牌
- 修改:根据id修改
- 删除:根据id删除
(一)查询所有数据
1、步骤 (1)获取Connection (2)定义SQL:select * from tb_brand (3)获取PreparedStatement对象 (4)设置参数:(不需要) (5)执行SQL (6)处理结果:List< Brand > (7)释放资源
@Test
public void testSelectAll() throws Exception{
String url = "jdbc:mysql://localhost:3306/db1?serverTimezone=GMT";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url,username,password);
String sql = "select * from tb_brand";
PreparedStatement psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
List<Brand> list = new ArrayList<>();
while (rs.next()){
Brand brand = new Brand();
Integer id = rs.getInt("id");
String brandName = rs.getString("brand_name");
String companyName = rs.getString("company_name");
Integer ordered = rs.getInt("ordered");
String description = rs.getString("description");
Integer status = rs.getInt("status");
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
list.add(brand);
}
System.out.println(list);
psmt.close();
rs.close();
conn.close();
}
(二)添加品牌
1、编写SQL语句 insert into tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?); 2、是否需要参数?需要,除了id之外的所有数据 3、返回结果如何封装?Boolean
@Test
public void testAdd() throws Exception{
String url = "jdbc:mysql://localhost:3306/db1?serverTimezone=GMT";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url,username,password);
String sql = "insert into tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?);";
PreparedStatement pstmt = conn.prepareStatement(sql);
String input_brand_name = "龟苓膏";
String input_company_name = "苼禾堂";
int input_ordered = 9;
String input_description = "过瘾不过火";
int input_status = 0;
pstmt.setString(1, input_brand_name);
pstmt.setString(2,input_company_name);
pstmt.setInt(3,input_ordered);
pstmt.setString(4,input_description);
pstmt.setInt(5,input_status);
int count = pstmt.executeUpdate();
if(count>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
pstmt.close();
conn.close();
}
(三)根据id修改
1、编写SQL语句 2、是否需要参数?需要:Brand对象所有数据 3、返回结果如何封装?boolean
@Test
public void testUpdate() throws Exception{
String url = "jdbc:mysql://localhost:3306/db1?serverTimezone=GMT";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url,username,password);
String sql = "update tb_brand set brand_name = ? , company_name = ? , " +
"ordered = ? , description = ? , status = ? " +
"where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
int input_id = 4;
String input_brand_name = "龟苓爽";
String input_company_name = "苼禾堂";
int input_ordered = 500;
String input_description = "过瘾不过火";
int input_status = 0;
pstmt.setString(1, input_brand_name);
pstmt.setString(2,input_company_name);
pstmt.setInt(3,input_ordered);
pstmt.setString(4,input_description);
pstmt.setInt(5,input_status);
pstmt.setInt(6,input_id);
int count = pstmt.executeUpdate();
if(count>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
pstmt.close();
conn.close();
}
(四)根据id删除
1、编写SQL语句 delete from tb_brand where id = ? 2、是否需要参数?需要id 3、返回结果如何封装?boolean
@Test
public void testDelete() throws Exception{
String url = "jdbc:mysql://localhost:3306/db1?serverTimezone=GMT";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url,username,password);
String sql = "delete from tb_brand where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
int input_id = 4;
pstmt.setInt(1,input_id);
int count = pstmt.executeUpdate();
if(count>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
pstmt.close();
conn.close();
}
|