java 如何在调用存储过程时从前端设置 INOUT 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5230541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:05:46  来源:igfitidea点击:

How to set INOUT parameters from frontend while calling a stored procedure

javastored-proceduresjdbc

提问by Amit Kumar Gupta

I developed a SP, say abc(a,b,c), where

我开发了一个 SP,比如说 abc(a,b,c),其中

  • a is IN parameter
  • b is OUT parameter
  • c is INOUT parameter.
  • a 是 IN 参数
  • b 是 OUT 参数
  • c 是 INOUT 参数。

If i call this sp directly from the DB as abc(<val>,?,?),I get err

如果abc(<val>,?,?),我在出错时直接从数据库调用这个 sp

The number of variables in the EXECUTE statement, the number of variables in the OPEN statement, or the number of arguments in an OPEN statement for a parameterized cursor is not equal to the number of values required.

The number of variables in the EXECUTE statement, the number of variables in the OPEN statement, or the number of arguments in an OPEN statement for a parameterized cursor is not equal to the number of values required.

But if i run it as abc(<val>,?,<val>), it runs successfully.

但是,如果我将它作为 运行abc(<val>,?,<val>),它会成功运行。

I want to call this SP through Java program. For this, i am setting IN & INOUT parameters. And registering OUT & INOUT parameters. But it is giving me the same error as above

我想通过Java程序调用这个SP。为此,我正在设置 IN 和 INOUT 参数。并注册 OUT & INOUT 参数。但它给了我与上面相同的错误

回答by Pablo Santa Cruz

You need to use java.sql.CallableStatementto process out parameters.

您需要使用java.sql.CallableStatement来处理参数。

So, following your example, your call would be:

因此,按照您的示例,您的电话将是:

String sql = "{ call abc(?, ?, ?) }";
CallableStatement cs = conn.prepareCall(sql);
cs.setInt(1, 20); // setting "a" in parameter to 1
cs.registerOutParameter(2, Types.VARCHAR); // setting "b" as out parameter
cs.setString(3, "Some String"); // setting "c" as in parameter
cs.registerOutParameter(3, Types.VARCHAR); // setting "c" as out parameter
// then execute
cs.executeUpdate();
// and retrieve out parameters
String bout = cs.getString(2);
String cout = cs.getString(3);