简体   繁体   English

使用JDBC编写带有绑定变量的匿名PL / SQL块

[英]Writing an anonymous PL/SQL block with bind variables using JDBC

I have an Anonymous PL/SQL Block with bind variables that I want to run via JDBC. 我有一个带有绑定变量的匿名PL / SQL块,我想通过JDBC运行。

PL/SQL block example: PL / SQL块示例:

variable v_value number
declare
v_return varchar2(30);
begin
:v_value:=300;
select ename into v_return from emp where empno=:v_value;
end;

The corresponding Java code would make use of the escape syntax with "?" 相应的Java代码将使用带有“?”的转义语法 to set the variables. 设置变量。 So this block would look like this(correct me if I'm wrong): 所以这个块看起来像这样(如果我错了,请纠正我):

String block = "declare v_return varchar2(30);" +
               "begin" + 
               "? := 300;" +
               "select ename into v_return from emp where empno = ?;" +
               "end;"

Now, assuming that my variable is an INPUT parameter, I'll have to set the parameter like this: 现在,假设我的变量是一个INPUT参数,我将必须像这样设置参数:

// omitting the CallableStatement and conn declarations
cs = conn.prepareCall(block);

cs.setInt(parameterIndex, parameterValue);

The PROBLEM is that in my block I have two "?" 问题是在我的区块中我有两个“?” used to replace the bound parameter :v_value. 用于替换绑定参数:v_value。 This means that when using the escape syntax only the 1'st "?" 这意味着当使用转义语法时只有1'st “?” will be set. 将被设定。 The 2'nd "?" 2 '和“?” will be left "hanging". 将被“悬挂”。

In such cases, when the same bind variable(s) is used multiple times in a PL/SQL block, how should I proceed with translating this in JDBC escape syntax? 在这种情况下,当在PL / SQL块中多次使用相同的绑定变量时,我应该如何继续在JDBC转义语法中转换它?

EDIT: 编辑:

I found this question on SO that is related to my problem. 我发现这个问题与我的问题有关。 What I understand from it is that I'll have to REWRITE all Anonymous PL/SQL Blocks that make use of multiple bind variable instances in the same block. 我从中理解的是,我必须重写所有在同一块中使用多个绑定变量实例的匿名PL / SQL块。 Is there ANY workaround for this? 这有什么解决方法吗? Or this is it... game over... it's the way JDBC works and I'll have to make due. 或者就是这样......游戏结束......这就是JDBC的工作方式,我必须做出应有的决定。



Looking forward for an answer... searched for this for 2 hours with no results. 期待一个答案...搜索这个2小时没有结果。

Take a look at this doc . 看看这个文档

Basicaly, you can bind the same variable as Input and Output like this: 基本上,你可以像输入和输出一样绑定相同的变量:

CallableStatement call = conn.prepareCall(
    "{CALL doubleMyInt(?)}");
// for inout parameters, it is good practice to
// register the outparameter before setting the input value
call.registerOutParameter(1, Types.INTEGER);
call.setInt(1,10);

I hope it helps. 我希望它有所帮助。

Try this query: 试试这个查询:

String block = "declare " +
               "   v_return varchar2(30);" +
               "begin" + 
               "   select ename into v_return from emp where empno = ?;" +
               "   ? := v_return; " +
               "end;";
CallableStatement cs = connection.prepareCall( block );
cs.setInt( 1, v_value );
cs.registerOutParameter( 2, java.SQL.VARCHAR );
cs.executeUpdate();
String result = cs.getString( 2 );

See below links for more details and examples: 有关详细信息和示例,请参阅以下链接:
http://docs.oracle.com/cd/E11882_01/java.112/e16548/getsta.htm#JJDBC28075 http://docs.oracle.com/cd/E11882_01/java.112/e16548/getsta.htm#JJDBC28075
http://docs.oracle.com/cd/E11882_01/java.112/e16548/oraint.htm#JJDBC28168 http://docs.oracle.com/cd/E11882_01/java.112/e16548/oraint.htm#JJDBC28168

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

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