关于java:JDBC使用JDBCUtils工具类执行增删改操作

8次阅读

共计 1188 个字符,预计需要花费 3 分钟才能阅读完成。


public class JDBCUtilsUpdateTest {

    /**
     * 应用自定义编写的 JDBCUtils 执行插入语句
     * */
    @Test
    public void TestInsert() throws SQLException {

        // 获取连贯
        Connection con = JDBCUtils.getConnection();
        // 获取语句执行平台
        Statement sqlExecute = JDBCUtils.createStatement(con);
        // 执行 sql 语句
        String sql = "insert into test_02 values (1,'wdm','0704','0704'),(2,'xx','1111','1111'),(3,'maomao','1515','1505');";
        sqlExecute.executeUpdate(sql);
        // 敞开流对象
        JDBCUtils.close(con,sqlExecute);

    }

    /**
     * 应用自定义编写的 JDBCUtils 执行创立表的语句
     * */
    @Test
    public void TestCreate() throws SQLException {

        // 1. 获取连贯
        Connection con = JDBCUtils.getConnection();
        // 2. 获取语句执行平台 即 Statement 对象
        Statement sqlExecute = JDBCUtils.createStatement(con);
        // 3. 执行 sql 语句
        String sql2 = "create table test_02(id int, name varchar(20), password varchar(20), birthday varchar(20));";
        int i = sqlExecute.executeUpdate(sql2);
        System.out.println("创立表胜利!");
        // 4. 敞开流对象
        JDBCUtils.close(con,sqlExecute);
    }

    /**
     * 应用自定义编写的 JDBCUtils 执行删除数据的操作
     */
    @Test
    public void TestDelete() throws SQLException {

        // 1。获取连贯
        Connection con = JDBCUtils.getConnection();
        // 2。获取语句执行平台,即 Statement 对象
        Statement sqlExecute = JDBCUtils.createStatement(con);
        // 3。执行 sql 语句
        String sql3 = "delete from test_01 where id = 4;";
        int i = sqlExecute.executeUpdate(sql3);
        System.out.println("删除胜利!受影响的行数是:" + i);
        // 4。敞开对象
        JDBCUtils.close(con,sqlExecute);
    }
}
正文完
 0