Original URL : http://www.coling.uni-freiburg.de/java/doku/jdbc-etc/jdbc/jdbc7.html
Calls to Stored Procedures and Functions are a bit more involved than SQL queries and updates. These calls are performed using the CallableStatement interface, which is an extension of the PreparedStatement interface. Parameters passed to and from the stored procedure are identified using ? placeholder symbols. These placeholders must be registered as IN our OUT data elements, using data types from the Types class.
// Stored procedure call
String command = "{call PROCEDURENAME(?,?,?,?)}"; // 4 placeholders
// Stored function call
String command = "{? = call FUNCTIONNAME(?,?,?,?)}"; // 4 placeholders + 1 return value
CallableStatement cstmt = con.prepareCall (command);
(Throws a SQLException error.)
// Assign IN parameters (use positional placement) cstmt.setInt(1, 50); // first placeholder has an integer value of 50 cstmt.setString(2, "Smith"); // second placeholder has a string value of "Smith" cstmt.registerOutParameter(3, Types.NUMERIC); // NUMERIC is preferred over INTEGER with some databases cstmt.registerOutParameter(4, Types.VARCHAR); // fourth placeholder is an OUT field of type VARCHAR.
(Throws a SQLException error.)
cstmt.execute();
(Throws a SQLException error.)
/** Use proper getXXXX() method based on parameter data type. * Placeholder 3 will receive a NUMERIC value as a BigDecimal object with 0 decimal places. * Placeholder 4 will receive a VARCHAR value as a String object. */ BigDecimal num = cstmt.getBigDecimal(3,0); String str = cstmt.getString(4);
(Throws a SQLException error.)
// Close the call object after update is complete cstmt.close();
(Throws a SQLException error.)
Next Topic: Accessing Metadata
| Enable Frames | Disable Frames |
| Home Page | JDBC | Java CGI | Java RMI | LiveConnect |
| Web Design | Personal Information | Favorite Links | What's New | Feedback |
visitors since January 1, 1997.
This site is brought to you courtesy of GeoCities. Get your own Free Home Page.
Last modified 19/09/2000