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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM