zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    XPOSED优秀模块列表 Xposed GEL 设置
    XPOSED优秀模块列表 Eggster
    XPOSED优秀模块列表 全部成为F
    XPOSED优秀模块列表 XBridge
    XPOSED优秀模块列表 xBoon
    XPOSED优秀模块列表 定位注入器
    showModalDialog()、showModelessDialog()方法使用详解
    手工更改数据库字符集
    blog开张了
    SQLSERVER 2008 复制之旅
  • 原文地址:https://www.cnblogs.com/kenx/p/13536097.html
Copyright © 2011-2022 走看看