gpt4 book ai didi

java - JDBC 使用批处理删除和插入

转载 作者:行者123 更新时间:2023-11-29 01:27:08 25 4
gpt4 key购买 nike

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

        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();

最佳答案

对于删除部分:

使用 addBatch 然后执行 Batch:

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 在批处理中插入几条记录。

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

更新:

Example/ Full Programs JDBC- Batch PreparedStatement - Execute DELETE query using PreparedStatement's executeUpdate method in java

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();
}
}
}
}

输出:

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(DMLcommand) using PreparedStatement's addBatch() and executeBatch()methods in java

关于java - JDBC 使用批处理删除和插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37995067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com