Java Spring Jdbc中如何从存储过程中读取返回值(过程中没有out参数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19582322/
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
How read returned value from stored procedure in Spring Jdbc (without out parameters in procedure)
提问by Malahov
How can I read returned value from stored procedure in Spring Jdbc (without out parameters) ?
如何从 Spring Jdbc 中的存储过程读取返回值(没有输出参数)?
I am use Sybase ASE database.
我使用的是 Sybase ASE 数据库。
Procedure Example:
程序示例:
CREATE PROCEDURE dbo.procedureA (
@a int
)
as
begin
IF EXISTS (SELECT 1 FROM dbo.T WHERE a = @a)
return 1
ELSE
return -1
end
回答by Gerard Ribas
The best approach for handle this is to use SimpleJdbcCallthat it's in the Spring-Jdbc project.
处理此问题的最佳方法是使用它在 Spring-Jdbc 项目中的SimpleJdbcCall。
With SimpleJdbcCall you can declare in and out parameters for this purpose.
使用 SimpleJdbcCall 您可以为此目的声明输入和输出参数。
You can do it something like this:
你可以这样做:
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource);
simpleJdbcCall.withCatalogName("dbo");
simpleJdbcCall.withProcedureName("procedureA ");
simpleJdbcCall.setAccessCallParameterMetaData(false);
simpleJdbcCall.declareParameters(new new SqlOutParameter("a",Types.NUMERIC));
simpleJdbcCall.execute();
More info here
更多信息在这里
Hope it helps.
希望能帮助到你。
回答by Siddiqi
This might help, although I haven't tried it myself yet;
这可能会有所帮助,尽管我自己还没有尝试过;
public MyStoredProc(JdbcTemplate jdbcTemplate) {
super(jdbcTemplate, "mystoredproc");
setFunction(true);
declareParameter(new SqlOutParameter("Result", Types.NUMERIC));
declareParameter(new SqlInOutParameter("otherparam1", Types.INTEGER));
declareParameter(new SqlParameter("otherparam2", Types.INTEGER));
allowsUnusedParameters();
compile();
}
回答by Jeff
Reference: http://www.databaseskill.com/1270151/Key point is: SimpleJdbcCall.withReturnValue() directs the SimpleJdbcCall object to put the return value in the result map with key "RETURN_VALUE". So you can access it like (Integer)simpleJdbcCall.execute().get("RETURN_VALUE")
参考:http://www.databaseskill.com/1270151/ 重点是:SimpleJdbcCall.withReturnValue() 指示 SimpleJdbcCall 对象将返回值放入结果映射中,键为“RETURN_VALUE”。所以你可以像 (Integer)simpleJdbcCall.execute().get("RETURN_VALUE") 一样访问它
For Sybase, it is not necessary to declare SqlOutParameter "RETURN_VALUE"
对于 Sybase,不需要声明 SqlOutParameter "RETURN_VALUE"