Druid连接池练习

练习描述

完成商品品牌数据的增删改查操作

查询: 查询所有数据

添加: 添加品牌

修改: 根据id修改

删除: 根据id删除

技巧:

alt + 鼠标左键,整列编辑

alt + Insert可以批量导入构造函数、get方法、set方法

操作步骤

1, 获取Connection

2, 定义SQL

3, 获取PreparedStatement对象

4, 设置参数

5, 执行SQL

6, 处理结果:List<Brand>

7, 释放资源

源代码

package local.content.example;


import java.io.FileInputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import local.content.pojo.Brand;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.sql.Connection;
/**
 * 测试用例
 * 品牌数据的增、删、改、查操作
 */
public class BrandTest {
    public static void main(String[] args){
        BrandTest test1 = new BrandTest();

        // 测试查询所有品牌信息方法
        try {
            test1.testSelectDB();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 测试添加品牌信息方法
        try {
            test1.testAddBrand();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 测试删除品牌信息方法
        try {
            test1.testDeleteBrand(3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 测试修改品牌信息方法
        try {
            test1.testUpdateBrand();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 修改品牌信息方法
    @Test
    public void testUpdateBrand() throws Exception {
            //1, 获取Druid Connection
        Properties prop = new Properties();
        prop.load(new FileInputStream("jdbc01/src/main/resources/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        //2, 编写SQL语句
        String sql = "update brand set brandName = ?, companyName = ?, order_id = ? where id = ?";
        //3, 执行SQL语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "iphone");
        preparedStatement.setString(2, "苹果公司");
        preparedStatement.setInt(3, 10);
        preparedStatement.setString(4, "1");
        System.out.println(sql);
        int result = preparedStatement.executeUpdate();
        if(result > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        //4, 关闭连接
        preparedStatement.close();
        connection.close();
    }
    // 删除品牌信息方法
    @Test
    public void testDeleteBrand(int id) throws Exception {
        //1, 获取Druid Connection
        Properties prop = new Properties();
        prop.load(new FileInputStream("jdbc01/src/main/resources/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();
        //2, 编写SQL语句
        String sql = "delete from brand where id = ?";
        //3, 获取PreparedStatement
        PreparedStatement ps = conn.prepareStatement(sql);
        //4, 设置参数
        ps.setInt(1, 3);
        //5, 执行SQL
        int count = ps.executeUpdate();
        //6, 处理结果
        if (count > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
            //7, 释放资源
            ps.close();
            conn.close();
        }
    }


    // 添加品牌信息方法
    @Test
    public void testAddBrand() throws Exception {
        // 定义要新增的参数内容
        String brandName = "抖音";
        String company = "字节跳动";
        int ordered = 1000;

        //1, 获取Druid Connection
        Properties prop = new Properties();
        prop.load(new FileInputStream("jdbc01/src/main/resources/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection connection = dataSource.getConnection();
        //2, 编写SQL语句
        String sql = "insert into brand (brandName, companyName, order_id) values (?, ?, ?)";
        //3, 获取PreparedStatement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //4, 给占位符赋值
        preparedStatement.setString(1, brandName);
        preparedStatement.setString(2, company);
        preparedStatement.setInt(3, ordered);
        //5, 执行SQL语句
        int i = preparedStatement.executeUpdate();
        //6, 处理结果
        if (i > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
            //7, 释放资源
            preparedStatement.close();
            connection.close();
        }
    }


    // 查询品牌信息
   @Test
    public void testSelectDB() throws Exception {
        //1, 获取Druid Connection
        Properties prop = new Properties();
        prop.load(new FileInputStream("jdbc01/src/main/resources/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();
        //2, 定义SQL
        String sql = "select * from brand where id >= ?";

        //3, 获取PreparedStatement对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //4, 设置参数
        pstmt.setInt(1,1);

        //5, 执行SQL
        ResultSet rs = pstmt.executeQuery();
        //6, 处理结果:List<Brand>
        List<Brand> brands = new ArrayList<>();
        Brand brand = null;
        while(rs.next()){
            int id = rs.getInt("id");
            String brandName = rs.getString("brandName");
            String companyName = rs.getString("companyName");
            int ordered = rs.getInt("order_id");
            brand = new Brand(id,brandName,companyName,ordered);
            brands.add(brand);
        }
        System.out.println(brands);
        //7, 释放资源
        rs.close();
        pstmt.close();
        conn.close();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值