16-2. Accessing a PL/SQL Stored Function from JDBC

Problem

You want to utilize a PL/SQL function from a Java application that uses the JDBC API to connect to an Oracle Database and returns a value to the Java application.

Solution

Use the JDBC API and a CallableStatement to invoke the PL/SQL function by passing a Java String containing the function call to the CallableStatement. The following example demonstrates a Java method that accepts a parameter of type double and then makes a JDBC call to the PL/SQL function calc_quarter_hour using the parameter. It is assumed that this Java method is to be added into the class that was created in Recipe 16-1.

public void calcQuarterHour(double hours)             throws SQLException {         float returnValue; ...

Get Oracle and PL/SQL Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.