简体   繁体   English

调用存储过程时,如何通过名称而不是在JDBC / JPA中的位置设置参数?

[英]How to set parameter by name instead of its position in JDBC/JPA when calling stored procedure?

I'm calling stored procedure from java. 我正在从Java调用存储过程。 Now I need to set the stored procedure's parameter by its name instead of its position inidex, is it doable? 现在,我需要通过存储过程的名称而不是其位置inidex设置参数,这可行吗?

I don't want to use string concatenation though, it's not safe and ugly to deal with. 我不想使用字符串连接,但是处理起来既不安全又丑陋。

Notice: I need to work with OUT/INOUT parameters, too. 注意:我也需要使用OUT / INOUT参数。

We can name the parameters using the format 我们可以使用以下格式命名参数

cstmt.setString("arg", name);

where cstmt is an CallableStatement 其中cstmt是CallableStatement

where arg is the name of the argument in the corresponding stored procedure. 其中arg是相应存储过程中参数的名称。

We do not need to name parameters in the same order as the arguments in the stored procedure as we will use paramname in this case. 我们不需要使用与存储过程中的参数相同的顺序来命名参数,因为在这种情况下,我们将使用paramname。

Also you can go through the Tutorial regarding the CallableStatement in the below link - 您也可以通过以下链接查看有关CallableStatement的教程-

http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.jdbc_pg.doc/jdbc89.htm http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.jdbc_pg.doc/jdbc89.htm

You can read the link - the registerOutParameter statement is there in the link - 您可以阅读链接-链接中有registerOutParameter语句-

// Register out parameter which should return the product is created.
  cstmt.registerOutParameter("prod_id", Types.FLOAT);

It is feasible, however not natively within the language. 这是可行的,但是不是语言本身的。 You will need to write your own code to provide this functionality or rely upon a third parties code (recommended approach). 您将需要编写自己的代码来提供此功能或依靠第三方代码(推荐的方法)。 I have used the simple code from this article in the past with success. 我过去曾成功使用本文中的简单代码。 It doesn't handle all cases but it is easily extendable. 它不能处理所有情况,但很容易扩展。 The SpringFramework also provides some libraries to provide this functionality if your looking for a more robust solution. 如果您正在寻找更强大的解决方案,SpringFramework还提供了一些库来提供此功能。

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

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