SQL 如何使用 JDBC 调用带有命名参数的 Sybase 存储过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20718199/
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 to call Sybase stored procedure with named params using JDBC
提问by jabal
I have a stored procedure in Sybase which I can invoke from my favourite SQL client like this:
我在 Sybase 中有一个存储过程,我可以像这样从我最喜欢的 SQL 客户端调用它:
exec getFooBar @Foo='FOO', @Bar='BAR'
It returns a table of results, so its like a query. It has actually many parameters, but I only want to call it with Foo and sometimes with Foo and Bar specified.
它返回一个结果表,所以它就像一个查询。它实际上有很多参数,但我只想用 Foo 调用它,有时还指定 Foo 和 Bar。
Sybase is ASE 15.0, I am using jConnect 6.0.5.
Sybase 是 ASE 15.0,我用的是 jConnect 6.0.5。
I can invoke this using a PreparedCall
if I specify only the first parameter:
PreparedCall
如果我只指定第一个参数,我可以使用 a 调用它:
CallableStatement cs = conn.prepareCall("{call getFooBar(?) }");
cs.setString(1,"FOO");
However I can't use the @Foo
notation to pass my params:
但是我不能使用@Foo
符号来传递我的参数:
conn.prepareCall("{call getFooBar @Foo='FOO' }");
conn.prepareCall("call getFooBar @Foo='FOO' ");
conn.prepareCall("{call getFooBar @Foo=? }"); // + setString(1,'FOO')
In all these cases I get exception from the DB telling me that the Foo parameter is expected:
在所有这些情况下,我从数据库中得到异常,告诉我需要 Foo 参数:
com.sybase.jdbc3.jdbc.SybSQLException: Procedure getFooBar expects
parameter @Foo, which was not supplied
Ideally I'd like to do this with Spring JdbcTemplate
or SimpleJdbcCall
, but with those I could not even get to the point where I could with plain old JDBC.
理想情况下,我想用 SpringJdbcTemplate
或SimpleJdbcCall
.
So to summarize, I'd like to avoid ending up with:
总而言之,我想避免以:
{ call getFooBar(?,null,null,null,null,null,?,null,null) }
Is that possible using JDBC or better Spring?
使用 JDBC 或更好的 Spring 有可能吗?
回答by Subbu
Not the most efficient solution, but, have you tried using the plain Statement itself with EXEC.
eg.String mySql = "EXEC getFooBar @Foo='FOO', @Bar='BAR'";
ResultSet rs = conn.getConnection().createStatement().executeQuery(mySql);
不是最有效的解决方案,但是,您是否尝试过将普通 Statement 本身与EXEC 一起使用。
例如。String mySql = "EXEC getFooBar @Foo='FOO', @Bar='BAR'";
ResultSet rs = conn.getConnection().createStatement().executeQuery(mySql);