gpt4 book ai didi

java - Java可以批量调用SQL存储过程吗?

转载 作者:行者123 更新时间:2023-12-04 23:55:29 25 4
gpt4 key购买 nike

Java: Insert multiple rows into MySQL with PreparedStatement涵盖将多个 INSERT 批处理为一个操作。我想知道是否有可能对存储过程的调用做同样的事情,更具体地说是对 MySQL?如果是这样,将使用什么语句类?

最佳答案

如果你有这样的存储过程:

JDBC CallableStatement Stored procedure IN parameter example.
CREATE OR REPLACE PROCEDURE insertEMPLOYEE(
e_id IN EMPLOYEE.EMPLOYEE_ID%TYPE,
e_name IN EMPLOYEE.NAME%TYPE,
e_salary IN EMPLOYEE.SALARY%TYPE)
IS
BEGIN

INSERT INTO EMPLOYEE ("EMPLOYEE_ID", "NAME", "SALARY")
VALUES (e_id, e_name, e_salary);

COMMIT;

END;

您只需使用 executeBatch() 即可按照您的意愿进行操作。示例:

Connection conn = null;
CallableStatement callableStatement = null;
String proc = "{call insertEMPLOYEE(?,?,?)}";
try{
//get connection
conn = JDBCUtil.getConnection();

//create callableStatement
callableStatement = conn.prepareCall(proc);

callableStatement.setInt(1, 7);
callableStatement.setString(2, "Harish Yadav");
callableStatement.setInt(3, 50000);
callableStatement.addBatch();

callableStatement.setInt(1, 8);
callableStatement.setString(2, "Abhishek Rathor");
callableStatement.setInt(3, 50000);
callableStatement.addBatch();

//execute query
callableStatement.executeBatch();

//close connection
callableStatement.close();
conn.close();

System.out.println("Records inserted successfully.");
}catch(Exception e){
e.printStackTrace();
}

关于java - Java可以批量调用SQL存储过程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961866/

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