调用存储过程时,如何通过名称设置参数,而不是在JDBC/JPA中设置参数? [英] How to set parameter by name instead of its position in JDBC/JPA when calling stored procedure?
问题描述
我正在从Java调用存储过程.现在,我需要通过存储名称而不是位置索引来设置存储过程的参数,这可行吗?
I'm calling stored procedure from java. Now I need to set the stored procedure's parameter by its name instead of its position inidex, is it doable?
尽管我不想使用字符串连接,但是处理起来并不安全且丑陋.
I don't want to use string concatenation though, it's not safe and ugly to deal with.
注意:我也需要使用OUT/INOUT参数.
Notice: I need to work with OUT/INOUT parameters, too.
推荐答案
我们可以使用
cstmt.setString("arg", name);
其中cstmt是CallableStatement
其中arg是相应存储过程中参数的名称.
where arg is the name of the argument in the corresponding stored procedure.
我们不需要使用与存储过程中的参数相同的顺序来命名参数,因为在这种情况下,我们将使用paramname.
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.
此外,您还可以在下面的链接中浏览有关CallableStatement的教程-
Also you can go through the Tutorial regarding the CallableStatement in the below link -
http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.jdbc_pg.doc/jdbc89.htm
您可以阅读链接-链接中存在registerOutParameter语句-
You can read the link - the registerOutParameter statement is there in the link -
// Register out parameter which should return the product is created.
cstmt.registerOutParameter("prod_id", Types.FLOAT);
这篇关于调用存储过程时,如何通过名称设置参数,而不是在JDBC/JPA中设置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!