Connect to database and call stored procedure

This code example shows how to connect to a database (in this case MS SQL Server) and call a stored procedure.
To call the stored procedure you create an instance of the class CallableStatement by calling the method prepareCall() on the Connection object.
In the first example below we assume that the name of the stored procedure is “generateID” and it takes one string argument, and returns an Id which depends on the argument (in the example it returns employeeId).
The value of the argument is set by calling the method setString() on the CallableStatement object. Since we want a return value from the stored procedure we have to call the executeQuery() method, which returns a ResultSet.

In the second example we do about just the same as above except that this time we don’t provide an argument to the stored procedure and we don’t receive any return value.

Search for more Java info on this site here:
Custom Search

Please type any questions here.