java-mysql 使用druid连接池-增-删-改

quange 2022-5-9 99 5/9

记录 代码

增加数据

// 添加数据
    @Test
    public boolean testAdd() throws Exception {
        // 加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));

        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        // 获取数据库连接对象
        Connection conn = dataSource.getConnection();

        String sql = "insert into quange.tb_brand (brand_name, company_name, ordered, description, status) values" +
                " (?,?,?,?,?)";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, "老坛涮菜");
        preparedStatement.setString(2, "老谭涮菜有限公司");
        preparedStatement.setInt(3, 20);
        preparedStatement.setString(4, "脚气老谭");
        preparedStatement.setInt(5, 1);

        // 执行mysql
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("添加数据成功");

            // 关闭链接
            preparedStatement.close();
            conn.close();

            return true;
        } else {
            System.out.println("添加失败!");
            return false;
        }
    }

修改数据

    // 修改数据
    @Test
    public void testUpdate() throws Exception {
        // 加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));

        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        // 获取数据库连接对象
        Connection conn = dataSource.getConnection();

        String sql = "update quange.tb_brand set brand_name=?,company_name=?,ordered=?,description=?,status=? where id =?";

        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, "老谭");
        preparedStatement.setString(2, "老谭酸菜牛肉面");
        preparedStatement.setInt(3, 30);
        preparedStatement.setString(4, "牛肉面");
        preparedStatement.setInt(5, 1);
        preparedStatement.setInt(6, 4);

        int i = preparedStatement.executeUpdate();
        System.out.println(i > 0);

        // 关闭链接
        preparedStatement.close();
        conn.close();
    }

删除数据

    // 删除数据
    @Test
    public void testDel() throws Exception {
        // 加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));

        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        // 获取数据库连接对象
        Connection conn = dataSource.getConnection();

        String sql = "delete from tb_brand where id=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setInt(1,4);

        int i = preparedStatement.executeUpdate();

        System.out.println(i > 0);

        // 关闭链接
        preparedStatement.close();
        conn.close();
    }
- THE END -
Tag:
最后修改:2022年5月9日
0

版权声明:
一、本站致力于为软件爱好者提供国内外软件开发技术和软件共享,着力为用户提供优资资源。
二、本站提供的所有下载文件均为网络共享资源,请于下载后的24小时内删除。如需体验更多乐趣,还请支持正版。
三、我站提供用户下载的所有内容均转自互联网。如有内容侵犯您的版权或其他利益的,请编辑邮件并加以说明发送到站长邮箱。站长会进行审查之后,情况属实的会在三个工作日内为您删除。

共有 0 条评论