Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This section looks at a very simple stored procedure that has no parameters. Even though most stored procedures do something more complex than this example, it serves to illustrate some basic points about them. As previously stated, the syntax for defining a stored procedure is different for each DBMS. For example, some usebegin . . . end
or other keywords to indicate the beginning and ending of the procedure definition. In some DBMSs, the following SQL statement creates a stored procedure:create procedure SHOW_SUPPLIERS as select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME from SUPPLIERS, COFFEES where SUPPLIERS.SUP_ID = COFFEES.SUP_ID order by SUP_NAMEThe following code puts the SQL statement into a string and assigns it to the variable
createProcedure
, which we will use later:String createProcedure = "create procedure SHOW_SUPPLIERS " + "as " + "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from SUPPLIERS, COFFEES " + "where SUPPLIERS.SUP_ID = COFFEES.SUP_ID " + "order by SUP_NAME";The following code fragment uses the
Connection
objectcon
to create aStatement
object, which is used to send the SQL statement creating the stored procedure to the database:Statement stmt = con.createStatement(); stmt.executeUpdate(createProcedure);The procedure
SHOW_SUPPLIERS
will be compiled and stored in the database as a database object that can be called, similar to the way you would call a method.Calling a Stored Procedure from JDBC
JDBC allows you to call a database stored procedure from an application written in the Java programming language. The first step is to create a
CallableStatement
object. As withStatement
andPreparedStatement
objects, this is done with an openConnection
object. ACallableStatement
object contains a call to a stored procedure; it does not contain the stored procedure itself. The first line of code below creates a call to the stored procedureSHOW_SUPPLIERS
using the connectioncon
. The part that is enclosed in curly braces is the escape syntax for stored procedures. When the driver encounters"{call SHOW_SUPPLIERS}"
, it will translate this escape syntax into the native SQL used by the database to call the stored procedure namedSHOW_SUPPLIERS
.CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}"); ResultSet rs = cs.executeQuery();The
ResultSet
rs
will be similar to the following:SUP_NAME COF_NAME ---------------- ----------------------- Acme, Inc. Colombian Acme, Inc. Colombian_Decaf Superior Coffee French_Roast Superior Coffee French_Roast_Decaf The High Ground EspressoNote that the method used to execute
cs
isexecuteQuery
becausecs
calls a stored procedure that contains one query and thus produces one result set. If the procedure had contained one update or one DDL statement, the methodexecuteUpdate
would have been the one to use. It is sometimes the case, however, that a stored procedure contains more than one SQL statement, in which case it will produce more than one result set, more than one update count, or some combination of result sets and update counts. In this case, where there are multiple results, the methodexecute
should be used to execute theCallableStatement
.The class
CallableStatement
is a subclass ofPreparedStatement
, so aCallableStatement
object can take input parameters just as aPreparedStatement
object can. In addition, aCallableStatement
object can take output parameters or parameters that are for both input and output. INOUT parameters and the methodexecute
are used rarely. For complete information, refer to JDBC Database Access with Java.
Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback FormCopyright 1995-2001 Sun Microsystems, Inc. All rights reserved.