Calling a stored procedure

Stored procedure implementation in most SQL database servers is vendor-specific. JDBC offers a generic way for calling those and Groovy's Sql class helps to simplify that task.

This recipe will demonstrate how to utilize the Sql class to make stored procedure calls.

Getting ready

Let's use the same cookbook database, created and populated, like in the Querying an SQL database recipe:

import static DBUtil.*
import groovy.sql.Sql

def server = startServer()
createSchema()
populate()

Let's also assume we have defined a stored procedure with the following structure:

CREATE PROCEDURE INGREDIENT_USAGE( OUT INGREDIENTS_RATE INTEGER, IN INGREDIENT_NAME VARCHAR(100)) READS SQL DATA BEGIN ATOMIC SELECT COUNT(*) INTO INGREDIENTS_RATE ...

Get Groovy 2 Cookbook 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.