5,JDBC练习
5.1 需求
完成商品品牌数据的增删改查操作
- 查询:查询所有数据
- 添加:添加品牌
- 修改:根据id修改
- 删除:根据id删除
5.2 案例实现
5.2.1 环境准备
- 数据库表 tb_brand
--删除tb_brand表droptableifexiststb_brand; --创建tb_brand表createtabletb_brand ( --id主键idintprimarykeyauto_increment, --品牌名称brand_namevarchar(20), --企业名称company_namevarchar(20), --排序字段orderedint, --描述信息descriptionvarchar(100), --状态:0:禁用1:启用statusint); --添加数据insertintotb_brand (brand_name, company_name, ordered, description, status) values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0), ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1), ('小米', '小米科技有限公司', 50, 'are you ok', 1);
- 在pojo包下实体类 Brand
/*** 品牌* alt + 鼠标左键:整列编辑* 在实体类中,基本数据类型建议使用其对应的包装类型*/publicclassBrand { // id 主键privateIntegerid; // 品牌名称privateStringbrandName; // 企业名称privateStringcompanyName; // 排序字段privateIntegerordered; // 描述信息privateStringdescription; // 状态:0:禁用 1:启用privateIntegerstatus; publicIntegergetId() { returnid; } publicvoidsetId(Integerid) { this.id=id; } publicStringgetBrandName() { returnbrandName; } publicvoidsetBrandName(StringbrandName) { this.brandName=brandName; } publicStringgetCompanyName() { returncompanyName; } publicvoidsetCompanyName(StringcompanyName) { this.companyName=companyName; } publicIntegergetOrdered() { returnordered; } publicvoidsetOrdered(Integerordered) { this.ordered=ordered; } publicStringgetDescription() { returndescription; } publicvoidsetDescription(Stringdescription) { this.description=description; } publicIntegergetStatus() { returnstatus; } publicvoidsetStatus(Integerstatus) { this.status=status; } publicStringtoString() { return"Brand{"+"id="+id+", brandName='"+brandName+'\''+", companyName='"+companyName+'\''+", ordered="+ordered+", description='"+description+'\''+", status="+status+'}'; } }
5.2.2 查询所有
/*** 查询所有* 1. SQL:select * from tb_brand;* 2. 参数:不需要* 3. 结果:List<Brand>*/publicvoidtestSelectAll() throwsException { //1. 获取Connection//3. 加载配置文件Propertiesprop=newProperties(); prop.load(newFileInputStream("jdbc-demo/src/druid.properties")); //4. 获取连接池对象DataSourcedataSource=DruidDataSourceFactory.createDataSource(prop); //5. 获取数据库连接 ConnectionConnectionconn=dataSource.getConnection(); //2. 定义SQLStringsql="select * from tb_brand;"; //3. 获取pstmt对象PreparedStatementpstmt=conn.prepareStatement(sql); //4. 设置参数//5. 执行SQLResultSetrs=pstmt.executeQuery(); //6. 处理结果 List<Brand> 封装Brand对象,装载List集合Brandbrand=null; List<Brand>brands=newArrayList<>(); while (rs.next()){ //获取数据intid=rs.getInt("id"); StringbrandName=rs.getString("brand_name"); StringcompanyName=rs.getString("company_name"); intordered=rs.getInt("ordered"); Stringdescription=rs.getString("description"); intstatus=rs.getInt("status"); //封装Brand对象brand=newBrand(); brand.setId(id); brand.setBrandName(brandName); brand.setCompanyName(companyName); brand.setOrdered(ordered); brand.setDescription(description); brand.setStatus(status); //装载集合brands.add(brand); } System.out.println(brands); //7. 释放资源rs.close(); pstmt.close(); conn.close(); }
5.2.3 添加数据
/*** 添加* 1. SQL:insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);* 2. 参数:需要,除了id之外的所有参数信息* 3. 结果:boolean*/publicvoidtestAdd() throwsException { // 接收页面提交的参数StringbrandName="香飘飘"; StringcompanyName="香飘飘"; intordered=1; Stringdescription="绕地球一圈"; intstatus=1; //1. 获取Connection//3. 加载配置文件Propertiesprop=newProperties(); prop.load(newFileInputStream("jdbc-demo/src/druid.properties")); //4. 获取连接池对象DataSourcedataSource=DruidDataSourceFactory.createDataSource(prop); //5. 获取数据库连接 ConnectionConnectionconn=dataSource.getConnection(); //2. 定义SQLStringsql="insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);"; //3. 获取pstmt对象PreparedStatementpstmt=conn.prepareStatement(sql); //4. 设置参数pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setInt(3,ordered); pstmt.setString(4,description); pstmt.setInt(5,status); //5. 执行SQLintcount=pstmt.executeUpdate(); // 影响的行数//6. 处理结果System.out.println(count>0); //7. 释放资源pstmt.close(); conn.close(); }
5.2.4 修改数据
/*** 修改* 1. SQL:update tb_brandset brand_name = ?,company_name= ?,ordered = ?,description = ?,status = ?where id = ?* 2. 参数:需要,所有数据* 3. 结果:boolean*/publicvoidtestUpdate() throwsException { // 接收页面提交的参数StringbrandName="香飘飘"; StringcompanyName="香飘飘"; intordered=1000; Stringdescription="绕地球三圈"; intstatus=1; intid=4; //1. 获取Connection//3. 加载配置文件Propertiesprop=newProperties(); prop.load(newFileInputStream("jdbc-demo/src/druid.properties")); //4. 获取连接池对象DataSourcedataSource=DruidDataSourceFactory.createDataSource(prop); //5. 获取数据库连接 ConnectionConnectionconn=dataSource.getConnection(); //2. 定义SQLStringsql=" update tb_brand\n"+" set brand_name = ?,\n"+" company_name= ?,\n"+" ordered = ?,\n"+" description = ?,\n"+" status = ?\n"+" where id = ?"; //3. 获取pstmt对象PreparedStatementpstmt=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. 执行SQLintcount=pstmt.executeUpdate(); // 影响的行数//6. 处理结果System.out.println(count>0); //7. 释放资源pstmt.close(); conn.close(); }
5.2.5 删除数
/*** 删除* 1. SQL:delete from tb_brand where id = ?* 2. 参数:需要,id* 3. 结果:boolean*/publicvoidtestDeleteById() throwsException { // 接收页面提交的参数intid=4; //1. 获取Connection//3. 加载配置文件Propertiesprop=newProperties(); prop.load(newFileInputStream("jdbc-demo/src/druid.properties")); //4. 获取连接池对象DataSourcedataSource=DruidDataSourceFactory.createDataSource(prop); //5. 获取数据库连接 ConnectionConnectionconn=dataSource.getConnection(); //2. 定义SQLStringsql=" delete from tb_brand where id = ?"; //3. 获取pstmt对象PreparedStatementpstmt=conn.prepareStatement(sql); //4. 设置参数pstmt.setInt(1,id); //5. 执行SQLintcount=pstmt.executeUpdate(); // 影响的行数//6. 处理结果System.out.println(count>0); //7. 释放资源pstmt.close(); conn.close(); }