完成商品品牌数据的增删改查操作
-
查询:查询所有数据
-
添加:添加品牌
-
修改:根据id修改
-
删除:根据id删除
目录
实体类Brand--用于查询时接收Brand类的数据对象并打印反馈
操作一:查询所有当前数据库中的所有Brand对象信息,并将反馈在控制台打印出来
操作二:添加数据,将想要存入的数据通过pstmt送入到数据库中
操作三:修改数据,将想要修改的数据通过pstmt送入到数据库中,通过id确定唯一对象,id为主键,具有唯一性
环境准备
数据库表tb_brand
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建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)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
('小米', '小米科技有限公司', 50, 'are you ok', 1);
-- 添加后查询一下是否录入的数据正确
select * from tb_brand;
初始化结果
实体类Brand--用于查询时接收Brand类的数据对象并打印反馈
package com.redred.d2_pojo;
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 Brand() {
}
public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {
this.id = id;
this.brandName = brandName;
this.companyName = companyName;
this.ordered = ordered;
this.description = description;
this.status = 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 +
'}';
}
}
操作一:查询所有当前数据库中的所有Brand对象信息,并将反馈在控制台打印出来
1. SQL:select * from tb_brand; 查询语句可以在数据库内写好,直接复制进来
2. 参数:不需要
3. 结果:List<Brand> 用于存储从数据库中获取的每个Brand对象
/**
* 查询所有
* 1. SQL:select * from tb_brand;
* 2. 参数:不需要
* 3. 结果:List<Brand>
*/
@Test
public void testSelectAll() throws Exception{
// 1. 获取Connection
// 3. 加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("D:\\code\\javawebpro\\d1_jdbc_app\\src\\druid.properties"));
// 4. 获取连接池对象
DataSource ds = DruidDataSourceFactory.createDataSource(prop);
// 5. 获取数据库连接 Connection
Connection conn = ds.getConnection();
// 2. 定义SQL
String sql = "select * from tb_brand";
// 3. 获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 6. 执行SQL
ResultSet rs = pstmt.executeQuery();
// 7. 处理结果 List<Brand> 封装Brand对象,装载List集合
Brand brand = null;
List<Brand> brands = new ArrayList<>();
while (rs.next()){
// 获取数据
int id = rs.getInt("id");
String brand_name = rs.getString("brand_name");
String company_name = rs.getString("company_name");
int ordered = rs.getInt("ordered");
String description = rs.getString("description");
int status = rs.getInt("status");
// 封装Brand对象
brand = new Brand();
brand.setId(id);
brand.setBrandName(brand_name);
brand.setCompanyName(company_name);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
// 装载集合
brands.add(brand);
}
System.out.println(brands);
rs.close();
pstmt.close();
conn.close();
}
上式输出结果很难看全,通过增强for遍历list集合输出结果如下:
操作二:添加数据,将想要存入的数据通过pstmt送入到数据库中
1. SQL:insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);
2. 参数:需要,除了id之外的所有参数信息
3. 结果:boolean
/**
* 添加
* 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 brandName = "最伟大的作品";
String companyName = "周杰伦";
Integer ordered = 1000;
String description = "华流才是最屌的";
Integer status = 1;
//1. 获取Connection
//3. 加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("D:\\code\\javawebpro\\d1_jdbc_app\\src\\druid.properties"));
//4. 获取连接池对象
DataSource ds = DruidDataSourceFactory.createDataSource(prop);
//5. 获取数据库连接 Connection
Connection conn = ds.getConnection();
//2. 定义SQL
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?, ?, ?, ?, ?)";
//3. 获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. 设置参数
pstmt.setString(1, brandName);
pstmt.setString(2, companyName);
pstmt.setInt(3, ordered);
pstmt.setString(4, description);
pstmt.setInt(5, status);
int i = pstmt.executeUpdate();
System.out.println(i > 0 ? "添加成功" : "添加失败");
pstmt.close();
conn.close();
}
添加数据结果展示:
操作三:修改数据,将想要修改的数据通过pstmt送入到数据库中,通过id确定唯一对象,id为主键,具有唯一性
1. String sql = "update tb_brand set brand_name = ?, company_name = ?, ordered = ?, description = ?, status = ? where id = ?";
2. 参数:需要,所有数据 ,通过id来确定需要修改的数据
3. 结果:boolean
/**
* 修改
* 1. SQL:
update tb_brand
set brand_name = ?,
company_name= ?,
ordered = ?,
description = ?,
status = ?
where id = ?
* 2. 参数:需要,所有数据
* 3. 结果:boolean
*/
@Test
public void testUpdate() throws Exception {
// 接收页面提交的参数
String brandName = "最伟大的作品";
String companyName = "周杰伦";
Integer ordered = 100000;
String description = "华流才是最屌的";
Integer status = 1;
int id = 4;
//1. 获取Connection
//3. 加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("D:\\code\\javawebpro\\d1_jdbc_app\\src\\druid.properties"));
//4. 获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. 获取数据库连接 Connection
Connection conn = dataSource.getConnection();
//2. 定义SQL
String sql = "update tb_brand set brand_name = ?, company_name = ?, ordered = ?, description = ?, status = ? where id = ?";
//3. 获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. 设置参数;
pstmt.setString(1, brandName);
pstmt.setString(2, companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4, description);
pstmt.setInt(5,status);
pstmt.setInt(6, id);
//5. 执行SQL
int i = pstmt.executeUpdate(); // 影响的行数
//6. 处理结果
System.out.println(i > 0 ? "修改成功" : "修改失败");
//7. 释放资源
pstmt.close();
conn.close();
}
修改数据结果展示:
操作四:删除数据,将想要删除的数据通过id确定
1. String sql = "detele from tb_brand where id = ?";
2. 参数:需要,id
3. 结果:boolean
/**
* 删除
* 1. SQL:
delete from tb_brand where id = ?
* 2. 参数:需要,id
* 3. 结果:boolean
*/
@Test
public void testDeleteById() throws Exception {
// 接收页面提交的参数
int id = 4;
//1. 获取Connection
//3. 加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("D:\\code\\javawebpro\\d1_jdbc_app\\src\\druid.properties"));
//4. 获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. 获取数据库连接 Connection
Connection conn = dataSource.getConnection();
//2. 定义SQL
String sql = "delete from tb_brand where id = ?";
//3. 获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. 设置参数
pstmt.setInt(1, id);
//5. 执行SQL
int i = pstmt.executeUpdate(); // 影响的行数
//6. 处理结果
System.out.println(i > 0 ? "删除成功" : "删除失败");
//7. 释放资源
pstmt.close();
conn.close();
}
删除结果就是初始化时的数据啦,杰伦已经出专辑啦,怎么能删除呢,哈哈哈,这个就不展示结果了。。。
还有就是有一个JDBC的jar包是必须的,连接池德鲁伊(druid)在本题可以不使用,普通的连接就可以,如果使用连接池就需要德鲁伊的jar包与配置信息了,后续发送链接给大家,需要的可以等一等,官网都会有的哦,配置信息先给大家展示