Call stored MySQL function

Aimad Majdou picture Aimad Majdou · Jun 7, 2013 · Viewed 10.4k times · Source

I've created a stored function verifierQteDemandee in my database which has an integer parameter numBonIn, which returns a boolean value.

I want to execute this function in my java program, I googled about it and all I can find is to execute a stored procedure, however I assumed that executing a stored function is the same as executing a stored procedure, and this is the code I tried :

CallableStatement cStmt = con.prepareCall("{call verifierQteDemandee(?)}");
            cStmt.setInt("numBonIn", 42);
            boolean hadResults = cStmt.execute();
            if (hadResults) {
                ResultSet rs = cStmt.getResultSet();
            }
            Boolean outputValue = cStmt.getBoolean(outputValue);;

The con variable is ans instance of Connection.

As you can notice in my code I don"t know how I get the returned value from that stored function in this line : int outputValue = cStmt.getInt("");.

Please if someone knows how to get the returned value the procedure, I'll be thankful.

Answer

Simon picture Simon · Jun 7, 2013

Try this :

CallableStatement cStmt = con.prepareCall("{? = call verifierQteDemandee(?)}");
cStmt.registerOutParameter(1,java.sql.Types.BOOLEAN);
cStmt.setInt(2, 42);
cStmt.execute();
Boolean outputValue = cStmt.getBoolean(1);

Ça devrais fonctionner...!