首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JDBC使用批处理删除和插入

问JDBC使用批处理删除和插入
EN

Stack Overflow用户
提问于 2016-06-23 22:50:32
回答 1查看 18.5K关注 0票数 2

我想知道是否可以使用批处理同时执行参数化的DELETE和INSERT语句。我知道如何插入多行,但是,我想首先执行一条DELETE语句(这需要不同的参数)。下面是我如何插入多条语句的方法:

代码语言:javascript
复制
        String query = "INSERT INTO " + TABLE + "(FOO, BAR) VALUES (?,?);";

        PreparedStatement sql = connection.prepareStatement(query);

        for(...){
            sql.setString(1, fooValue);
            sql.setInt(2, barValue);
            sql.addBatch();
        }       

        sql.executeBatch();

        sql.close();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-23 22:58:08

对于删除部分:

使用addBatch,然后使用executeBatch:

代码语言:javascript
复制
Statement st = con.createStatement();
st.addBatch("DELETE FROM tbl1");
st.addBatch("DELETE FROM tbl2");
st.addBatch("DELETE FROM tbl3");
int[] results = st.executeBatch();

然后,结果将包含一个数组,其中包含从每个表中删除的行数。

对于插入:

下面是一个示例,向您展示如何通过JDBC PreparedStatement在批处理中插入少量记录。

代码语言:javascript
复制
dbConnection.setAutoCommit(false);//commit trasaction manually

String insertTableSQL = "INSERT INTO DBUSER"
            + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
            + "(?,?,?,?)";              
PreparedStatement = dbConnection.prepareStatement(insertTableSQL);

preparedStatement.setInt(1, 101);
preparedStatement.setString(2, "mkyong101");
preparedStatement.setString(3, "system");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
preparedStatement.addBatch();

preparedStatement.setInt(1, 102);
preparedStatement.setString(2, "mkyong102");
preparedStatement.setString(3, "system");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
preparedStatement.addBatch();
preparedStatement.executeBatch();

dbConnection.commit();

资源链接:

JDBC PreparedStatement example – Batch Update

更新:

示例/完整程序JDBC- Batch PreparedStatement -在java中使用PreparedStatement的executeUpdate方法执行DELETE查询

代码语言:javascript
复制
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class PreparedStatementDeleteExample {
    public static void main(String... arg) {
           Connection con = null;
           PreparedStatement prepStmt = null;
           try {
                  // registering Oracle driver class
                  Class.forName("oracle.jdbc.driver.OracleDriver");

                  // getting connection
                  con = DriverManager.getConnection(
                               "jdbc:oracle:thin:@localhost:1521:orcl",
                               "ankit", "Oracle123");
                  System.out.println("Connection established successfully!");             

                  con.setAutoCommit(false); //Now, transactions won't be committed automatically.

                  prepStmt = con.prepareStatement("DELETE from EMPLOYEE where ID=? ");

                  //1) add set of parameters in PreparedStatement's object - BATCH of commands
                  prepStmt.setInt(1, 7); //substitute first occurrence of ? with 7
                  prepStmt.addBatch();

                  //2) add set of parameters in PreparedStatement's object - BATCH of commands                  
                  prepStmt.setInt(1, 8); //substitute first occurrence of ? with 8
                  prepStmt.addBatch();


                  //Execute PreparedStatement batch
                  prepStmt.executeBatch();
                  System.out.println("PreparedStatement Batch executed, DELETE done");

                  con.commit(); //commit all the transactions

           } catch (ClassNotFoundException e) {
                  e.printStackTrace();
           } catch (SQLException e) {
                  e.printStackTrace();
           }
           finally{
                  try {
                        if(prepStmt!=null) prepStmt.close(); //close PreparedStatement
                        if(con!=null) con.close(); // close connection
                  } catch (SQLException e) {
                        e.printStackTrace();
                  }
           }
    }
}

输出:

代码语言:javascript
复制
Connection established successfully!
PreparedStatement Batch executed, DELETE done

在本教程中,我们学习了如何在java JDBC中使用PreparedStatement的addBatch()和executeBatch()方法执行DELETE查询(DML命令)。

资源链接:

  1. JDBC Batch Processing (Batch insert, update and delete)
  2. JDBC- Batch PreparedStatement example- Execute DELETE query(DML command) using PreparedStatement's addBatch() and executeBatch() methods in java
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37995067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档