Creating a Java Bean that issues an SQL statement

You can use the Create a Java Bean that executes an SQL statement wizard to create a Java Bean that issues a specific SQL statement.

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

To create a Java Bean that issues an SQL statement:

  1. Switch to the Data perspective.

  2. In the Data Definition view, in the Statements folder, right-click the SQL statement that you want to use, 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, type the name of the folder where you want to create the Java Bean or click Browse to select the folder.

    2. In the Package field, type 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. If the SQL statement returns a result set, specify whether you want to include a parameter in the execute() method to limit the size of the result set. By default, the execute() method retrieves all rows in the result set.

  4. 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.

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

    • Use DriverManager 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.

  5. Specify how the Java Bean will provide user authentication. To run the SQL statement, supply a user ID and password that are valid for the database. You can specify that the Java Bean 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

  6. 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 calls a stored procedure

Related reference
JavaDoc for DB beans