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

  • 相关阅读:
    Fiddler基本操作和介绍
    cherles真机测试
    charles中的弱网测试(throttling setting方法)
    charles常用功能(一)修改request 请求参数值 修改返回值response--breakpoints
    安装charles以及基础配置
    接口测试
    ADB中monkey测试
    ADB的环境配置及ADB基本命令
    云测平台对app兼容性测试操作流程
    linux安装mysql后报错启动不了Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
  • 原文地址:https://www.cnblogs.com/kenx/p/13536097.html
Copyright © 2011-2022 走看看