从零开始学JDBC--1.4 模仿上节DDL过程写出DML过程

本文通过具体示例展示了如何使用Java进行数据库的插入、更新和删除操作,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


参照上一节的DDL过程,照猫画虎写出来这个DML过程,其中包含增加新的一行,删除一行,修改一行
(代码太长啦!!放在最后面)

仔细观察代码,发现其实变化的地方只不过是那么几行,集中在sql语句那一行,所以这样写代码,累死程序员了!!


下一节内容,将对此作出功能抽取以简化代码的结构,防止重复工作的编写,简化程序员的工作

public class Demo2 {

    private String url = "jdbc:mysql://localhost:3306/day17";
    private String user = "root";
    private String password = "123";

    @Test
    public void testInsert() {
        Connection conn = null;
        Statement stmt = null;
        try {
            // 1.驱动程序的注册
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获取连接
            conn = DriverManager.getConnection(url,user,password);

            // 3.创建statment
            stmt = conn.createStatement();

            // 4.准备sql
            String sql = "INSERT INTO student (NAME,gender)VALUES('齐八','女')";

            // 5.执行sql语句,得到返回结果
            int count = stmt.executeUpdate(sql);

            // 6、获取返回结果
            System.out.println("本次执行共影响了:" + count + "行数据");

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {// 7.关闭连接资源(注意顺序:后打开的先关闭)
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }

        }

    }

    /**
     * 更新操作
     */
    @Test
    public void testUpdate() {
        Connection conn = null;
        Statement stmt = null;

        String name = "齐八";
        String gender = "男";
        try {
            // 1.驱动程序的注册
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获取连接
            conn = DriverManager.getConnection(url,user,password);

            // 3.创建statment
            stmt = conn.createStatement();

            // 4.准备sql
            String sql = "UPDATE student SET gender='"+gender+"' WHERE id='"+gender+"'";

            // 5.执行sql语句,得到返回结果
            int count = stmt.executeUpdate(sql);

            // 6、获取返回结果
            System.out.println("本次执行共影响了:" + count + "行数据");

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {// 7.关闭连接资源(注意顺序:后打开的先关闭)
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }

        }

    }
    /**
     * 删除操作
     */
    @Test
    public void testDelete() {
        Connection conn = null;
        Statement stmt = null;

        String name = "齐八";
        try {
            // 1.驱动程序的注册
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获取连接
            conn = DriverManager.getConnection(url,user,password);

            // 3.创建statment
            stmt = conn.createStatement();

            // 4.准备sql
            String sql = "delete from student where NAME='"+name+"'";

            // 5.执行sql语句,得到返回结果
            int count = stmt.executeUpdate(sql);

            // 6、获取返回结果
            System.out.println("本次执行共影响了:" + count + "行数据");

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {// 7.关闭连接资源(注意顺序:后打开的先关闭)
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }

        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值