The Database control provides access to a relational database. The methods that you add to the Database control execute SQL commands against the database. This topic discusses the mechanics of adding a method to a Database control.
You can add a method directly to a Database control in Design view by right-clicking on the Database control instance and selecting Add Method, then typing a name for the method.
When you add a method, it initially has no parameters and no associated SQL statement. The following section explains how to edit the parameter list and the associated SQL statement.
A method on a Database control always has an associated SQL statement, which executes against the database when the method is called. The method’s @jc:sql annotation describes the method’s SQL statement.
You can specify the SQL statement in one of the following ways:
Note: In WebLogic Workshop 7.0, Database controls used the @jws:sql annotation. The deprecated @jws:sql annotation is still supported. To learn more about the @jc:sql annotation, see @jc:sql Annotation.
The method’s SQL statement may include substitution parameters. These parameters are replaced at runtime with the values that were passed to the method. The names of the substitution parameters in the SQL statement must match those in the method signature, so that WebLogic Workshop knows which parameter to replace with which value. Within the SQL statement, substitution parameters are enclosed in curly braces.
The following example Database control method illustrates using parameter substitution in Database control methods:
/** * @jc:sql statement="UPDATE customer SET address = {customerAddress} WHERE custid={customerID}" */ public int changeAddress(int customerID, String customerAddress);
In the example above, the SQL statement includes the substitutions {customerAddress} and {customerID}. These map to the customerID and customerAddress parameters of the findCustomer method. When the method is invoked, the values of any referenced parameters are substituted in the SQL statement before it is executed. Note that parameter substitution is case sensitive, so parameters mentioned in substitutions must exactly match the spelling and case of the parameters to the method.
The method signature declares a method that a user of this control may invoke. You should design this method so that its arguments and return value are convenient and useful to developers of applications that will use this control.
The rules of parameter substitution in Database control method SQL statements are described in Parameter Substitution in @jc:sql Statements.
The return type of the database operation is determined by the return type of the Java method. WebLogic Workshop attempts to format the results in whatever type you have specified for the method to return.
A method of a Database control can return a single value, a single row, or multiple rows. To learn more about the values returned by Database control methods, see the following three topics: