gpt4 book ai didi

java - 无法对 PLSQL 语句执行 fetch : next

转载 作者:行者123 更新时间:2023-12-03 02:05:51 25 4
gpt4 key购买 nike

存储程序

CREATE OR REPLACE
PROCEDURE ADDITION
(
A IN NUMBER ,
B IN NUMBER ,
C OUT NUMBER )
AS
BEGIN
C := A+B;
dbms_output.put_line(c);
END ADDITION;

JDBC程序

 cstmt = conn.prepareCall("BEGIN addition(?, ?,?); END;");
cstmt.setInt(1, 10);
cstmt.setInt(2, 85);
cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
ResultSet rs = cstmt.executeQuery();
while(rs.next()){
int c =rs.getInt("c");
System.out.println(c);
}

执行上述程序时出现错误。

错误:

java.sql.SQLException: Cannot perform fetch on a PLSQL statement: next
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:192)
at stored_procedure.main(stored_procedure.java:32)

最佳答案

executeQuery() 设计用于处理返回游标的 sql 语句(最常见的是 SELECT 查询)。您的调用不会返回游标,因此不适合与此方法一起使用。您应该只使用 execute(),然后从输出参数中获取值:

cstmt = conn.prepareCall("BEGIN addition(?, ?,?); END;");
cstmt.setInt(1, 10);
cstmt.setInt(2, 85);
cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
cstmt.execute();
int c = cstmt.getInt(3);
System.out.println(c);

关于java - 无法对 PLSQL 语句执行 fetch : next,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19443213/

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