简体   繁体   English

在Java中获取mysql程序的registeroutparameter

[英]Fetching the registeroutparameter of mysql procedure in java

I have a procedure in mysql with the signature 我在mysql中有签名的程序

"coursePK bigint(20), groupName varchar(1000), userPK bigint(20), newClassPK bigint(20), assignmentFlag varchar(1), rosterFlag varchar(1), gradebookFlag varchar(1), isHavingMasterClass varchar(1), OUT newBookPK bigint(20)"

The procedure has Start transaction and Commit statements at the end. 该过程在末尾具有Start transactionCommit语句。 And after commit I am setting the value of the OUT Parameter. 提交后,我将设置OUT参数的值。

Now, my java code is using Callable statement for calling the procedure and when I am fetching the out parameter using callableStatement.getString(index) or callableStatement.getInt(index) , I am always getting null and 0 respectively. 现在,我的Java代码使用Callable语句来调用该过程,并且当我使用callableStatement.getString(index)callableStatement.getInt(index)来获取out参数时,我总是分别获得null和0。

But When I am executing the procedure from mysql query browser and adding the line SELECT newBookPK at end, I am getting the correct output parameter. 但是,当我从mysql查询浏览器执行过程并在末尾添加SELECT newBookPK ,我得到了正确的输出参数。

So please help me in getting the correct value from Java code too. 因此,也请帮助我从Java代码中获得正确的价值。

Where I am wrong in fetching the output parameter from JAVA. 我在从JAVA获取输出参数时错了。

Here is the java Code : 这是java代码:

callableStatement = conn.prepareCall("{call copy_custombook_details(?,?,?,?,?,?,?,?,?)}"); //coursePK bigint(20), groupName varchar(1000), userPK bigint(20), newClassPK bigint(20), assignmentFlag varchar(1), rosterFlag varchar(1), gradebookFlag varchar(1), isHavingMasterClass varchar(1), OUT newBookPK bigint(20)
count=1;
callableStatement.setString(count++, coursePK);
callableStatement.setString(count++, groupName);
callableStatement.setString(count++, userPK);
callableStatement.setString(count++, newClassPK);
callableStatement.setString(count++, "N");
callableStatement.setString(count++, "N");
callableStatement.setString(count++, "N");
callableStatement.setString(count++, isHavingMasterClass);
callableStatement.registerOutParameter(count++, Types.VARCHAR);
callableStatement.execute();
newBookPK = callableStatement.getString(9);

When I am executing the procedure from mysql query browser ...
And, in your java code, where are you calling to execute the statement? 而且,在您的Java代码中,您在哪里调用以执行该语句?

In-between 中间

callableStatement.registerOutParameter(count++, Types.VARCHAR);
newBookPK = callableStatement.getString(9);

include 包括

callableStatement.execute();

And then getString( 9 ) should be returning something desired. 然后, getString( 9 )应该返回所需的东西。

The route cause for the issue is in mysql procedure signature. 问题的路由原因在mysql过程签名中。 If there is OUT parameters in the procedure, those should be define before IN parameters . 如果程序中有OUT参数, 则应在IN参数之前定义

In your case, procedure signature need to change as 在您的情况下,过程签名需要更改为

OUT newBookPK bigint(20), coursePK bigint(20), groupName varchar(1000), userPK bigint(20), newClassPK bigint(20), assignmentFlag varchar(1), rosterFlag varchar(1), gradebookFlag varchar(1), isHavingMasterClass varchar(1)

and java code need to change according to that order. 和Java代码需要根据该顺序进行更改。

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

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