简体   繁体   中英

Fetching the registeroutparameter of mysql procedure in java

I have a procedure in mysql with the signature

"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. And after commit I am setting the value of the OUT Parameter.

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.

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.

So please help me in getting the correct value from Java code too.

Where I am wrong in fetching the output parameter from JAVA.

Here is the java Code :

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?

In-between

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

include

callableStatement.execute();

And then getString( 9 ) should be returning something desired.

The route cause for the issue is in mysql procedure signature. If there is OUT parameters in the procedure, those should be define before IN parameters .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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