Java CallableStatement registerOutParameter,它有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22313935/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:52:48  来源:igfitidea点击:

Java CallableStatement registerOutParameter, what does it do?

javamysqlcallable-statement

提问by neilnm

I'm following a guide regarding callable statements.

我正在遵循有关可调用语句的指南。

In this guide, it says that I need to register the out parameter with the follow statement:

在本指南中,它说我需要使用以下语句注册 out 参数:

callsts.registerOutParameter(2, java.sql.Types.VARCHAR);

QUESTION: Why do I need to do this/what does it do ?

问题:为什么我需要这样做/它有什么作用?

Complete Java code:

完整的Java代码:

String getcall = "{call gettitle (?,?)}";
CallableStatement callsts = connect.prepareCall(getcall);
int nID = 15;
callsts.setInt(1, nID);
callsts.registerOutParameter(2, java.sql.Types.VARCHAR);
callsts.execute();
String calltitle = callsts.getString(2);
System.out.println("Callable Stamement Title: "+calltitle);       

mySQL procedure:

mySQL 程序:

DELIMITER $$ 
DROP PROCEDURE IF EXISTS `nothingtowear`.`gettitle` $$
CREATE PROCEDURE  `nothingtowear`.`gettitle` (
IN nothingtowear_ID INT,
OUT nothingtowear_TITLE VARCHAR( 255 )) 
BEGIN 
SELECT Title INTO nothingtowear_TITLE
FROM articles
WHERE ID = nothingtowear_ID;
END $$
DELIMITER;

采纳答案by anttix

Out parameters are parameters passed to a SQL stored procedure that the procedure writes to. In MySQL it can also be done via a SET command

输出参数是传递给 SQL 存储过程的参数,该过程将写入该过程。在 MySQL 中,它也可以通过 SET 命令完成

SET out_param1 = value

See mysql stored-procedure: out parameterfor another example

另一个例子参见mysql存储过程:out参数

registerOutParameterwill tell JDBC driver that it this is an otuput parameter and needs to be treated as such. E.g. it needs to use special syntax to call the procedure and retrieve output values after a statement is called

registerOutParameter将告诉 JDBC 驱动程序这是一个 otuput 参数,需要这样处理。例如,它需要使用特殊语法来调用过程并在调用语句后检索输出值

回答by Premraj

registerOutParameteris used to create a variable i.e. sql typeson database server, so which is used to store value, and can get access using index in java calling stored procedures and functions context.

registerOutParameter用于在数据库服务器上创建一个变量即sql类型,因此它用于存储值,并且可以在java调用存储过程和函数上下文中使用索引进行访问。

for example:

例如:

callableStatement.registerOutParameter(1, java.sql.Types.VARCHAR);
......
// get cursor and cast it to ResultSet
String var = callableStatement.getString(1);

In Oracle:

在甲骨文中:

callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
// get cursor and cast it to ResultSet
ResultSet rs = (ResultSet) callableStatement.getObject(1);