Java 从 JDBC MSSQL 获取返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1947754/
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-08-13 01:42:40  来源:igfitidea点击:

Getting the Return Value from JDBC MSSQL

javasql-serverstored-proceduresjdbcreturn-value

提问by Ryan Elkins

I'm connecting to SQL Server (2005) through Java using the Microsoft SQL Server JDBC Driver 2.0.

我正在使用 Microsoft SQL Server JDBC Driver 2.0 通过 Java 连接到 SQL Server (2005)。

How do I get the return value from a stored procedure? I'm doing something like:

如何从存储过程中获取返回值?我正在做类似的事情:

Connection connection = dataSource.getConnection()
CallableStatement proc = connection.prepareCall("{ call dbo.mySproc() }");
proc.execute();

Should I be using execute()? executeQuery()? executeUpdate()? None of these seem to return a return value by default but I'm not really sure how to get to it.

我应该使用execute()吗?执行查询()?执行更新()?默认情况下,这些似乎都没有返回返回值,但我不确定如何获得它。

EDIT 1: To be clear, I know how to call stored procedures. This question is specifically about how to get the RETURN VALUE (as opposed to a Result Set). The Return Value is an integer that is usually generated when you execute a query with no Result Set or if you specifically state something like RETURN 0in your SQL.

编辑 1:要清楚,我知道如何调用存储过程。这个问题特别是关于如何获得返回值(而不是结果集)。返回值是一个整数,通常在您执行没有结果集的查询时生成,或者如果您RETURN 0在 SQL 中明确声明了类似的内容。

EDIT 2: executeUpdate() returns an int but this int is not the same as the Return Value. Also, an OUT parameter is not the same as a return value.

编辑 2:executeUpdate() 返回一个 int 但这个 int 与返回值不同。此外,OUT 参数与返回值不同。

采纳答案by Ryan Elkins

Bozho's 2nd revised answer was close but not quite there. It did lead me to the answer though.

Bozho 的第二个修订答案很接近,但还没有完全到位。不过,它确实让我找到了答案。

Taking the code example I started with we end up with:

以我开始的代码示例为例,我们最终得到:

CallableStatement proc = connection.prepareCall("{ ? = call dbo.mySproc() }");
proc.registerOutParameter(1, Types.INTEGER);
proc.execute();
int returnValue = proc.getInt(1);

The key pieces here are the "? =" in front of the "call" in the prepareCallfunction which sets up a place for the return value and the registerOutputParameter. It has to be registered as an Integer, as the return value is always an int (at least in SQL Server, maybe it's different in other DBs). You therefore have to get it using getInt. I tested this method and it does work.

这里的关键部分是prepareCall函数中“调用”前面的“?=”,它为返回值和registerOutputParameter. 它必须注册为整数,因为返回值始终是整数(至少在 SQL Server 中,可能在其他数据库中有所不同)。因此,您必须使用getInt. 我测试了这个方法,它确实有效。

回答by Bozho

c.prepareCall("? = ..");
cs.execute();
String returnedValue = cs.getString(1);

(or the method of the appropriate type. You can use getObjectalternatively)

(或适当类型的方法。您可以getObject交替使用)

From an old getting started tutorial

旧的入门教程

the getXXX methods in CallableStatement retrieve values from the OUT parameters and/or return value of a stored procedure.
CallableStatement 中的 getXXX 方法从存储过程的 OUT 参数和/或返回值中检索值。

(Btw, the links that were provided by Umesh had this sort of information.)

(顺便说一句,Umesh 提供的链接包含此类信息。)