需求
完成商品品牌数据的增删改查操作
- 查询:查询所有数据
- 添加:添加品牌
- 修改:根据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);
- 在pojo包下实体类
Brand
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(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;
}
......
set 和 get 方法
......
toString() 方法
}
查询所有
- 代码
/**
* 查询所有
*/
@Test
public void testSelectAll() throws Exception {
// 加载配置文件 (数据库相关参数)
Properties prop = new Properties();
System.out.println(System.getProperty("user.dir"));
prop.load(new FileInputStream("src/druid.properties"));
// 获取数据库连接池对象 DataSource
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
// 获取数据库连接 Connection
Connection connection = dataSource.getConnection();
// 定义 sql
String sql = "select * from tb_brand;";
// 获取 PreparedStatement 对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
// 执行 sql
ResultSet resultSet = preparedStatement.executeQuery();
// 处理结果
List list = new ArrayList<Brand>();
while(resultSet.next()){
Integer id = resultSet.getInt("id");
String brandName = resultSet.getString("brand_name");
String companyName = resultSet.getString("company_name");
int ordered = resultSet.getInt("ordered");
String description = resultSet.getString("description");
int status = resultSet.getInt("status");
list.add(new Brand(id,brandName,companyName,ordered,description,status));
}
for(Object object : list){
System.out.println((Brand)object);
}
// 释放资源
resultSet.close();
preparedStatement.close();
connection.close();
}
- 运行结果
添加数据
- 代码
/**
* 增加数据
*/
@Test
public void testAdd() throws Exception {
// 接收页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘";
int ordered = 1;
String description = "绕地球一圈";
int status = 1;
// 加载配置文件 (数据库相关参数)
Properties prop = new Properties();
System.out.println(System.getProperty("user.dir"));
prop.load(new FileInputStream("src/druid.properties"));
// 获取数据库连接池对象 DataSource
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
// 获取数据库连接 Connection
Connection connection = dataSource.getConnection();
// 定义 sql
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
// 获取 PreparedStatement 对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//4. 设置参数
preparedStatement.setString(1,brandName);
preparedStatement.setString(2,companyName);
preparedStatement.setInt(3,ordered);
preparedStatement.setString(4,description);
preparedStatement.setInt(5,status);
// 执行 sql
int count = preparedStatement.executeUpdate(); // 返回影响行数
// 处理结果
System.out.println(count > 0);
// 释放资源
preparedStatement.close();
connection.close();
}
- 运行结果
修改数据
修改与添加类似!
删除数据
删除与添加类似!