简体   繁体   English

在 Java 中调用 Oracle 存储过程

[英]Calling an Oracle Stored Procedure in Java

Here's how I'm able to execute my stored procedure through SqlDeveloper这是我如何通过 SqlDeveloper 执行我的存储过程

var p refcursor;
exec DMG.Getstudentids(12342343,:p);
print p;

Output输出

P
-----------
STUDENT_ID
-----------
23432425
54353455

Now I'm trying execute the stored procedure the same way but in Java.现在我正在尝试以相同的方式执行存储过程,但是在 Java 中。 Here's my code and I'm missing something about the input/output parameters or their datatypes.这是我的代码,我缺少有关输入/输出参数或其数据类型的信息。

Connection connection = DriverManager.getConnection(url, user, pass);
CallableStatement cs = connection.prepareCall("{call DMG.Getstudentids(?,?)}");
cs.setFloat(1, 12342343);
cs.registerOutParameter(2, Types.OTHER);
cs.execute();
List<Integer> result = (List<Integer>) cs.getArray(2);

I get the following error我收到以下错误

java.sql.SQLException: Invalid column type: 1111

I think I'm missing something fundamental here.我想我在这里遗漏了一些基本的东西。 Anyone see where I'm failing?有人看到我失败的地方吗? Thanks.谢谢。

Try following:尝试以下操作:

Connection connection = DriverManager.getConnection(url, user, pass);
CallableStatement cs = connection.prepareCall("{call DMG.Getstudentids(?,?)}");
cs.setFloat(1, 12342343);
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.executeQuery();
ResultSet resultSet=cs.getObject(1);
List<Integer> result = new ArrayList<Integer>();
while(resultSet.next()){
 result.add(resultSet.getInt(STUDENT_ID));
}

Note : Since the procedure is returning refcursor , you need to register OracleTypes.CURSOR as output parameter. Note :由于procedure返回refcursor ,您需要将OracleTypes.CURSOR注册为输出参数。

Nother thing to note is you need to catch the whole dataset (refcursor) into Result Set , iterate it and put the extracted value into List .另一件需要注意的事情是您需要将整个数据集(refcursor)捕获到Result Set ,对其进行迭代并将提取的值放入List

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM