完成商品品牌数据的增删改查
查询:查询所有数据
添加:添加品牌
修改:根据id修改
删除:根据id删除
1、数据表tb_brand的创建
CREATE table tb_brand
(
//id主键
id int primary key auto_increment,
//品牌名称
brand_name varchar(20),
//企业名称
company_name varchar(20),
//排序字段
ordered int,
//描述信息
description varchar(100),
//状态:0:禁用 1:启用
status int
);
INSERT into tb_brand(brand_name,company_name,ordered,description,status)
value('三只松鼠','三只索尼概述股份有限公司',5 ,'好吃不上火',0 ),
('华为','华为技术有限公司',100 ,'华为致力于,。。。。',0 ),
('小米','小米科技有限公司',50 ,'areuok',1 );
select * from tb_brand
2、实体类的创建
int尽量使用封装数据类型integer,防止默认值0对业务产生影响
alter+鼠标左键
可以实现多列同时编辑
package com.avb.pojo; public class Brand { //id主键 private Integer id ; //品牌名称 private String brand_name ; //企业名称 private String company_name ; //排序字段 private Integer ordered; //描述信息 private String description ; //状态:0:禁用 1:启用 private Integer status ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBrand_name() { return brand_name; } public void setBrand_name(String brand_name) { this.brand_name = brand_name; } public String getCompany_name() { return company_name; } public void setCompany_name(String company_name) { this.company_name = company_name; } 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 + ", brand_name='" + brand_name + '\'' + ", company_name='" + company_name + '\'' + ", ordered=" + ordered + ", description='" + description + '\'' + ", status=" + status + '}'; } }
3、实现查询所有
package com.avb.example; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.avb.pojo.Brand; import org.junit.jupiter.api.Test; import javax.sql.DataSource; import java.io.FileInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Properties; /*brand类的增删改查操作*/ public class brand_test { @Test public void testSelectALl() throws Exception{ //1、导入jar包 //2、配置文件 //3、加载配置文件 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); //4、获取数据库连接Connection DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //5、获取数据库连接Connection Connection conn= dataSource.getConnection(); //System.out.println(conn); //6、构建sql语句 String sql="select * from tb_brand"; //创建stmt对象 PreparedStatement pstmt = conn.prepareStatement(sql) ; //设置参数 //执行sql ResultSet rs = pstmt.executeQuery(); //处理结果 List<Brand> 封装Brand对象,装载List集合 Brand brand = null; List<Brand> brands = new ArrayList<>(); while (rs.next()){ //获取数据 alter+enter可以自动补全 int id = rs.getInt("id"); String brandName = rs.getString("brand_name"); String companyName = rs.getString("company_name"); int ordered = rs.getInt("ordered"); String description = rs.getString("description"); int status = rs.getInt("status"); /*System.out.println(id); System.out.println(brandName); System.out.println(companyName); System.out.println(ordered); System.out.println(status); System.out.println(description); */ //封装brand对象 brand = new Brand(); brand.setId(id); brand.setBrand_name(brandName); brand.setCompany_name(companyName); brand.setOrdered(ordered); brand.setDescription(description); brand.setStatus(status); //装载集合 brands.add(brand); } System.out.println(brands); //释放资源 pstmt.close(); conn.close(); rs.close(); //System.out.println(System.getProperty("user.dir")); } }
4、添加数据
@Test public void testAdd() throws Exception{ //1、导入jar包 //2、配置文件 //3、加载配置文件 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); //4、获取数据库连接Connection DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //5、获取数据库连接Connection Connection conn= dataSource.getConnection(); //System.out.println(conn); //定义插入数据 String brandName = "香飘飘奶茶"; String companyName = "香飘飘"; int ordered = 8; String description = "可绕地球三圈"; int status = 1; //6、构建sql语句 String sql="insert into tb_brand(brand_name,company_name,ordered,description,status) value(?,?,?,?,?)"; //创建pstmt对象 PreparedStatement pstmt = conn.prepareStatement(sql) ; //设置参数 pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setInt(3,ordered); pstmt.setString(4,description); pstmt.setInt(5,status); //执行sql int count = pstmt.executeUpdate(); //处理结果 List<Brand> 封装Brand对象,装载List集合 /*Brand brand = null; List<Brand> brands = new ArrayList<>(); while (rs.next()){ //获取数据 alter+enter可以自动补全 int id = rs.getInt("id"); String brandName = rs.getString("brand_name"); String companyName = rs.getString("company_name"); int ordered = rs.getInt("ordered"); String description = rs.getString("description"); int status = rs.getInt("status");*/ /*System.out.println(id); System.out.println(brandName); System.out.println(companyName); System.out.println(ordered); System.out.println(status); System.out.println(description); */ //封装brand对象 /* brand = new Brand(); brand.setId(id); brand.setBrand_name(brandName); brand.setCompany_name(companyName); brand.setOrdered(ordered); brand.setDescription(description); brand.setStatus(status); //装载集合 brands.add(brand); } System.out.println(brands);*/ //判断结果 System.out.println(count>0); //释放资源 pstmt.close(); conn.close(); //rs.close(); //System.out.println(System.getProperty("user.dir")); }
5、修改数据
@Test public void testupdate() throws Exception{ //1、导入jar包 //2、配置文件 //3、加载配置文件 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); //4、获取数据库连接Connection DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //5、获取数据库连接Connection Connection conn= dataSource.getConnection(); //System.out.println(conn); //定义插入数据 String brandName = "香飘飘奶茶"; String companyName = "香飘飘"; int ordered = 2; String description = "可绕地球六圈"; int status = 0; int id = 4; //6、构建sql语句 String sql="update tb_brand set brand_name = ?,company_name = ?,ordered = ?,description=?,status=? where id=?"; //创建pstmt对象 PreparedStatement pstmt = conn.prepareStatement(sql) ; //设置参数 pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setInt(3,ordered); pstmt.setString(4,description); pstmt.setInt(5,status); pstmt.setInt(6,id); //执行sql int count = pstmt.executeUpdate(); //处理结果 List<Brand> 封装Brand对象,装载List集合 /*Brand brand = null; List<Brand> brands = new ArrayList<>(); while (rs.next()){ //获取数据 alter+enter可以自动补全 int id = rs.getInt("id"); String brandName = rs.getString("brand_name"); String companyName = rs.getString("company_name"); int ordered = rs.getInt("ordered"); String description = rs.getString("description"); int status = rs.getInt("status");*/ /*System.out.println(id); System.out.println(brandName); System.out.println(companyName); System.out.println(ordered); System.out.println(status); System.out.println(description); */ //封装brand对象 /* brand = new Brand(); brand.setId(id); brand.setBrand_name(brandName); brand.setCompany_name(companyName); brand.setOrdered(ordered); brand.setDescription(description); brand.setStatus(status); //装载集合 brands.add(brand); } System.out.println(brands);*/ //判断结果 System.out.println(count>0); //释放资源 pstmt.close(); conn.close(); //rs.close(); //System.out.println(System.getProperty("user.dir")); } }
6、删除数据
@Test public void testdeletebyid() throws Exception{ //1、导入jar包 //2、配置文件 //3、加载配置文件 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); //4、获取数据库连接Connection DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //5、获取数据库连接Connection Connection conn= dataSource.getConnection(); //System.out.println(conn); //定义插入数据 int id = 4; //6、构建sql语句 String sql="delete from tb_brand where id = ?"; //创建pstmt对象 PreparedStatement pstmt = conn.prepareStatement(sql) ; //设置参数 pstmt.setInt(1,id); //执行sql int count = pstmt.executeUpdate(); //判断结果 System.out.println(count>0); //释放资源 pstmt.close(); conn.close(); //rs.close(); //System.out.println(System.getProperty("user.dir")); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏