JDBC&综合练习

简介: JDBC&综合练习

1.需求


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

• 查询:查询所有数据

• 添加:添加品牌

• 修改:根据 id修改

• 删除:根据 id删除


2.环境搭建


•准备环境:1.创建项目 2.导入druid 和mysql驱动包 3.在src下面创建连接数据库的配置文件

Ø数据库表 tb_brand

Ø实体类 Brand

Ø测试用例

3.查询所有数据


1.获取Connection

2.定义SQL:select *** from tb_brand;

3.获取 PreparedStatement对象

4.设置参数:不需要

5.执行SQL

6.处理结果:List

7.释放资源

 @Test
    public void query() throws Exception{
        Properties p = new Properties();
        p.load(new FileInputStream("src\\druid.properties"));
        DataSource ds = DruidDataSourceFactory.createDataSource(p);
        //获取Connection
        Connection conn = ds.getConnection();
        //定义SQL:select * from tb_brand;
        String sql = "select * from tb_brand";
        //获取 PreparedStatement对象
        PreparedStatement pst = conn.prepareStatement(sql);
        //设置参数:不需要
        //执行SQL
        ResultSet rs = pst.executeQuery();
        //处理结果:List<Brand>
        ArrayList<Brand> list = new ArrayList<>();
        while (rs.next()){
            int id = rs.getInt("id");
            String brandName = rs.getString("brand_name");
            String companyName = 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(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);
            list.add(brand);
        }
        System.out.println("list = " + list);
        //释放资源
        rs.close();
        pst.close();
        conn.close();
    }


4.添加


@Test
    public void add() throws Exception{
        Properties p = new Properties();
        p.load(new FileInputStream("src\\druid.properties"));
        DataSource ds = DruidDataSourceFactory.createDataSource(p);
        //获取Connection
        Connection conn = ds.getConnection();
        String sql = "insert into tb_brand values(null,?,?,?,?,?)";
        //获取 PreparedStatement对象
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,"苹果");
        pst.setString(2,"apple");
        pst.setInt(3,1);
        pst.setString(4,"贵");
        pst.setInt(5,0);
        int count = pst.executeUpdate();
        System.out.println("count = " + count);
        //释放资源
        pst.close();
        conn.close();
    }


5.修改


 @Test
    public void update() throws Exception{
        Properties p = new Properties();
        p.load(new FileInputStream("src\\druid.properties"));
        DataSource ds = DruidDataSourceFactory.createDataSource(p);
        //获取Connection
        Connection conn = ds.getConnection();
        String sql = "update tb_brand set brand_name  = ?,company_name= ?,ordered= ?,description = ?,status= ? where id = ?";
        //获取 PreparedStatement对象
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1,"OPPO");
        pst.setString(2,"OPPO");
        pst.setInt(3,1);
        pst.setString(4,"贵");
        pst.setInt(5,0);
        pst.setInt(6,3);
        int count = pst.executeUpdate();
        System.out.println("count = " + count);
        //释放资源
        pst.close();
        conn.close();
    }


6.删除


 @Test
    public void delete() throws Exception{
        Properties p = new Properties();
        p.load(new FileInputStream("src\\druid.properties"));
        DataSource ds = DruidDataSourceFactory.createDataSource(p);
        //获取Connection
        Connection conn = ds.getConnection();
        String sql = "delete from tb_brand where id = ?";
        //获取 PreparedStatement对象
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setInt(1,4);
        int count = pst.executeUpdate();
        System.out.println("count = " + count);
        //释放资源
        pst.close();
        conn.close();
    }
相关文章
|
SQL 开发框架 安全
分享154个ASP源码,总有一款适合您
分享154个ASP源码,总有一款适合您
413 2
|
Apache C++ Linux
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17202 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1293 3
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2364 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3840 22