JDBC——案例

风陵南·2022-09-21 02:08·67 次阅读

JDBC——案例

 

 创建一个商品表

复制代码
drop table if exists tb_brand;

-- 创建tb_brand表
create table tb_brand(
        id int primary key auto_increment,     -- 主链
        brand_name varchar(20),                -- 品牌名称
        company_name varchar(20),              -- 公司名称
        orderd int,                            -- 排序字段
        description varchar(100),              -- 描述信息
        status INT                             -- 状态:  0-禁用  1-启用
);

-- 添加数据
insert into tb_brand (brand_name,company_name, orderd, description, status) 
values
('三只松鼠','三只松鼠股份有限公司', 5 ,'好吃不上火', 0),
('华为','华为技术有限公司',100,'华为致力于把数字世界带入每个人、每个家庭', 1),
('小米','小米科技有限公司',50,'are you ok', 1);

select * from tb_brand;
复制代码

 

 

复制代码
 /**查询表所有元素
     * 1、SQL:select * from tb_brand
     * 2、参数:不需要
     * 3、结果:List<Brand>
     */
    @Test
    public void testSelectAll() throws Exception {
        // 1、获取Connection连接对象
            // 配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid0920.properties"));
            // 获取数据库连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
            // 获取Connection连接对象
        Connection connection = dataSource.getConnection();

        // 2、定义sql语句
        String sql = "select * from tb_brand";

        // 3、获取pstmt对象
        PreparedStatement pstmt = connection.prepareStatement(sql);

        // 4、设置参数(暂无)

        // 5、执行SQL
        ResultSet rs = pstmt.executeQuery();

        // 6、处理结果 List<Brand>
        ArrayList<Brand> brands = new ArrayList<>();
        while(rs.next()){
            Brand brand = new Brand(rs.getInt("id"),rs.getString("brand_name"),
                                    rs.getString("company_name"),rs.getInt("orderd"),
                                    rs.getString("description"),rs.getInt("status"));
            brands.add(brand);
        }

        // 7、释放资源
        rs.close();
        pstmt.close();
        connection.close();

        System.out.println(brands);
    }
复制代码

复制代码
/**
     * 添加表元素
     * 1、SQL:insert into tb_brand (brand_name,company_name,orderd,description,
     *                              status) values (?,?,?,?,?)
     * 2、参数:不需要
     * 3、结果:True or False
     */
    @Test
    public void testInsert() throws Exception {
        // 1、获取Connection连接对象
        // 配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid0920.properties"));
        // 获取数据库连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        // 获取Connection连接对象
        Connection connection = dataSource.getConnection();

        // 2、定义sql语句
        String sql = "insert into tb_brand (brand_name,company_name,orderd,description,status) values (?,?,?,?,?)";

        // 3、获取pstmt对象
        PreparedStatement pstmt = connection.prepareStatement(sql);

        // 4、设置参数-- 5个参数
        Brand brand = new Brand();
        brand.setBrandName("重邮火锅");
        brand.setCompanyName("重庆邮电有限公司");
        brand.setDescription("重庆的火锅真的好吃!!");
        brand.setOrderd(105);
        brand.setStatus(1);
        pstmt.setString(1,brand.getBrandName());
        pstmt.setString(2,brand.getCompanyName());
        pstmt.setInt(3,brand.getOrderd());
        pstmt.setString(4,brand.getDescription());
        pstmt.setInt(5,brand.getStatus());

        // 5、执行SQL
        int count = pstmt.executeUpdate();

        // 6、处理结果 True or False
        System.out.println(count > 0);
       

        // 7、释放资源

        pstmt.close();
        connection.close();
    }
复制代码

 

 

复制代码
/**
     * 通过id修改表元素
     * 1、SQL:update tb_Brand set brand_name = ? , company_name = ? , orderd = ? , description = ? , status = ? where id = ?
     * 2、参数:不需要
     * 3、结果:True or False
     */
    @Test
    public void testUpdateById() throws Exception {
        // 1、获取Connection连接对象
        // 配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid0920.properties"));
        // 获取数据库连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        // 获取Connection连接对象
        Connection connection = dataSource.getConnection();

        // 2、定义sql语句
        String sql = "update tb_Brand set brand_name = ? , company_name = ? , orderd = ? , description = ? , status = ? where id = ?";

        // 3、获取pstmt对象
        PreparedStatement pstmt = connection.prepareStatement(sql);

        // 4、设置参数-- 5个参数
        Brand brand = new Brand();
        brand.setBrandName("重邮锅巴");
        brand.setCompanyName("重庆邮电有限公司");
        brand.setDescription("重庆的锅吧真的好吃!!");
        brand.setOrderd(105);
        brand.setStatus(1);
        pstmt.setString(1,brand.getBrandName());
        pstmt.setString(2,brand.getCompanyName());
        pstmt.setInt(3,brand.getOrderd());
        pstmt.setString(4,brand.getDescription());
        pstmt.setInt(5,brand.getStatus());
        pstmt.setInt(6,4);

        // 5、执行SQL
        int count = pstmt.executeUpdate();

        // 6、处理结果 True or False
        System.out.println(count > 0);
       

        // 7、释放资源

        pstmt.close();
        connection.close();
    }
复制代码

 

 

复制代码
/**
     * 通过id删除元素
     * 1、SQL:delete from tb_brand where id = ?
     * 2、参数:不需要
     * 3、结果:True or False
     */
    @Test
    public void testDeleteById() throws Exception {
        // 1、获取Connection连接对象
        // 配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid0920.properties"));
        // 获取数据库连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        // 获取Connection连接对象
        Connection connection = dataSource.getConnection();

        // 2、定义sql语句
        String sql = "delete from tb_brand where id = ?";

        // 3、获取pstmt对象
        PreparedStatement pstmt = connection.prepareStatement(sql);

        // 4、设置参数-- 1个参数--id
        pstmt.setInt(1,4);

        // 5、执行SQL
        int count = pstmt.executeUpdate();

        // 6、处理结果 True or False
        System.out.println(count > 0);
       
        // 7、释放资源

        pstmt.close();
        connection.close();
    }
复制代码

 

 

 

 

 

posted @   风陵南  阅读(67)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示