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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.