In the following sections, you enhance the sample application to display a dynamically-generated list of job IDs and job titles in the Job field when they are inserting a new employee record.
To do this, you create a database function, GET_JOBS, that uses a REF CURSOR to retrieve a result set of jobs from the Jobs table. A new Java method, getJobs, calls this database function to retrieve the result set.
The following steps create a new package in the database to hold a REF CURSOR declaration.
Example 6-10 Creating a Package in the Database
CREATE OR REPLACE
PACKAGE JOBSPKG AS
TYPE ref_cursor IS REF CURSOR;
END;
The code for the package is shown in Example 6-10:
These steps create a database function GET_JOBS that uses a REF CURSOR to retrieve a result set of jobs from the Jobs table.
Example 6-11 Creating a Stored Function
CREATE OR REPLACE FUNCTION GET_JOBS RETURN JobsPkg.ref_cursor AS jobs_cursor JobsPkg.ref_cursor; BEGIN OPEN jobs_cursor FOR SELECT job_id, job_title FROM jobs; RETURN jobs_cursor; END;
The code for the function is shown in Example 6-11.
These steps create a Java method, getJobs, in the DataHandler class that calls the GET_JOBS function to retrieve the result set.
The code for the getJobs method is as follows:
public ResultSet getJobs() throws SQLException {
try {
getDBConnection();
String jobquery = "begin ? := get_jobs; end;";
CallableStatement callStmt = conn.prepareCall(jobquery);
callStmt.registerOutParameter(1, OracleTypes.CURSOR);
callStmt.execute();
rset = (ResultSet)callStmt.getObject(1);
} catch ( SQLException ex ) {
logException( ex );
}
return rset;
}
To create the list displaying the list of job IDs and job titles in the Insert page, you hard-coded the job IDs and job titles. In the following steps, you replace this with a dynamically-generated list provided by the REF CURSOR created in the previous section.
Now run the application, click to insert a new employee and use the list to display a list of available jobs. Figure 6-4 shows the dynamic jobs list in the browser.
Figure 6-4 Dynamically Generated List in Browser
