Creating a Java Bean that calls a stored procedure

You can use the Create a Java Bean that calls a stored procedure wizard to create a Java Bean that calls a specific stored procedure.

Prerequisite: To use this wizard, be online, with access to your database server.

To use the wizard:

  1. Switch to the Data perspective.

  2. In either the Data Definition view or the Database Explorer view, in the Stored Procedures folder, right-click the stored procedure that you want to work with, and select Generate Java Bean from the pop-up menu.

  3. On the Java Class Specification page of the wizard, complete the following steps.

    1. In the Source Folder field, enter the name of folder where you want to create the Java Bean or click Browse to select the folder.

    2. In the Package field, enter the name of Java package in which you want to create your Java Bean or click Browse to select the package. Leave this field empty to create the new class in the default package.

    3. In the Name field, type a name for the new class. To conform to Java conventions, the class name must start with a capital letter.

    4. Specify whether the stored procedure returns a result set. If the procedure does return a result set, you can specify whether to generate one of the following items:

      • A helper class that provides better access to each column of the result set.

      • A parameter in the execute() method to limit the size of the result set. If you do not select this option, the execute() method retrieves all rows into the result set.

  4. Describe the result set. If you choose to generate a helper class, describe the result set that is returned by the stored procedure. You specify the columns that are in the result set on this page.

  5. Click Next.

  6. On the Specify Runtime Database Connection Information page, specify whether to use a data source connection or a driver manager connection. A data source is defined in an application server that implements Java Database Connectivity (JDBC), and is generally the preferred way for Web applications to connect to a database because it provides pooled connections. When the application initializes, the server requests a pool of database connections. Each time a Web application requires a database connection, the server provides one from the pool. When the Web application is done with the connection, it releases the connection back to the pool. Because connecting to the database is one of the slowest operations an application can perform, this approach is usually the most efficient. In contrast, for a driver manager connection, each time a Web application requires a database connection, it requests a connection from the database server.

    • Data source connection: Enter the Java Naming and Directory Interface (JNDI) name of the data source, as defined in the server configuration.

    • Driver manager connection: Type the fully-qualified class name of the driver in the Driver Name field, and the associated JNDI address in the URL field. For example, for DB2 enter COM.ibm.db2.jdbc.app.DB2Driver for the Driver Name and jdbc:db2:SAMPLE for the URL.

  7. Specify how the method will provide user authentication. To run the stored procedure, supply a user ID and password that are valid for the database. You can specify that the method will provide a valid user ID and password within its execute() method. This means that the Java Bean will always connect using the same user ID and password. If you do not include the user ID and password inside the execute() method, the application must provide the user ID and password as input parameters to the execute() method.

    • To include user authentication within the method, click Inside the execute() method. Type a user ID and password in the appropriate fields to access the database. The initial values are the ones originally used to load the existing database model. The password will be masked when you type it in the field.

    • To require the user ID and password to be provided as parameters to the execute()method, click By the execute() method's caller

  8. Click Next.

  9. On the final page of the wizard, review the specifications for the new Java Bean, then click Finish to complete the wizard.

The wizard creates the Java Bean, along with all the necessary deployment descriptors to build, run, and deploy it with your application.

 

Related concepts

DB beans for database access

 

Related tasks

Creating a Java Bean that issues an SQL statement

Related reference
JavaDoc for DB beans