Question

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.

Était-ce utile?

La solution

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...!

Autres conseils

You can try:

CallableStatement cstmt = con.prepareCall("{call verifierQteDemandee(?)}");
cstmt.registerOutParameter(1,Type.Boolean);
cstmt.setInt("numBonIn", 42);
cstmt.execute();
bool outputValue = cstmt.getBoolean(1);

As a sidenote, I would not expect to obtain a resultset from an sql function! I would expect a scalar value as output from my sql function

Please refer to Function vs. Stored Procedure in SQL Server for a complete comparison between function and stored procedure

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top