Loading

JDBC | 第二章: JDBC之批量更新,添加,和删除操作

在JDBC中Statement批量操作主要用到了addBatch批量添加要执行的sql到当前Statement对象 executeBatch提交执行所有sql语句

批量添加

 public int[] insBatch() {
        Connection connection = null;
        Statement statement = null;
        String sql = "";
        try {
            //获取数据连接
            connection = basicUse.getConnection();
            //获取发送sql指令执行sql对象
            statement = connection.createStatement();
            for (int i = 0; i < 10; i++) {
                StringBuffer sbf = new StringBuffer("insert into student (name, age, addr, hobby) ");
                sbf.append(" values ('kenx',24,'上海','篮球')");
                sql = sbf.toString();
                System.out.println("执行sql" + sql);
                //将指定SQL添加到Statement对象的当前命令列表中
                statement.addBatch(sql);
            }
            //执行成功返回更新计数的数组
            int[] success = statement.executeBatch();  //批量执行所有sql返回一个更新计数的数组
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            //执行完数据库操作后记得关闭数据库连接资源
            try {

                statement.close();
                connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }

批量更新

 public int[] updBatch() {
        Connection connection = null;
        Statement statement = null;
        String sql = "";
        try {
            //获取数据连接
            connection = basicUse.getConnection();
            //获取发送sql指令执行sql对象
            statement = connection.createStatement();
            for (int i = 1; i < 10; i++) {
                StringBuffer sbf = new StringBuffer("update student set  hobby='足球'");
                sbf.append(" where id=" + i);
                sql = sbf.toString();
                System.out.println("执行sql" + sql);
                //将指定SQL添加到Statement对象的当前命令列表中
                statement.addBatch(sql);
            }
            //执行成功返回更新计数的数组
            int[] success = statement.executeBatch();  //批量执行所有sql返回一个更新计数的数组
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            //执行完数据库操作后记得关闭数据库连接资源
            try {

                statement.close();
                connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

批量删除

 public int[] deldBatch() {
        Connection connection = null;
        Statement statement = null;
        String sql = "";
        try {
            //获取数据连接
            connection = basicUse.getConnection();
            //获取发送sql指令执行sql对象
            statement = connection.createStatement();
            for (int i = 1; i < 10; i++) {
                StringBuffer sbf = new StringBuffer("delete from student ");
                sbf.append(" where id=" + i);
                sql = sbf.toString();
                System.out.println("执行sql" + sql);
                //将指定SQL添加到Statement对象的当前命令列表中
                statement.addBatch(sql);
            }
            //执行成功返回更新计数的数组
            int[] success = statement.executeBatch();  //批量执行所有sql返回一个更新计数的数组
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            //执行完数据库操作后记得关闭数据库连接资源
            try {

                statement.close();
                connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

完整项目案例
点击这里 github

posted @ 2020-08-20 17:12  程序员三时  阅读(1409)  评论(0编辑  收藏  举报