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

来源:互联网 发布:蓝洞游戏公司 知乎 编辑:程序博客网 时间:2024/06/07 06:30


参照上一节的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);                }            }        }    }}
0 0