Over a million developers have joined DZone.

Using spwrap to Simplify Calling Stored Procedures From Java

DZone's Guide to

Using spwrap to Simplify Calling Stored Procedures From Java

How to use the spwrap framework to call stored procedures in Java, using an old Java tutorial from Oracle as a testing ground.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

spwrap is a tiny framework to simplify the call to stored procedures from Java code.

In this post, I'll implement the Columbian coffee example from the Java Tutorial at Oracle. This example uses MySQL to store coffee and supplier data. See the install script at GitHub.

The script installs two Tables: suppliers and coffees, fills them with data, and creates three stored procedures: 

show_suppliers: To list all coffee names with supplier names (takes no parameters and returns as result set).

get_supplier_of_coffee: To get a supplier name of a coffee (takes one input parameter and returns one output parameter).

raise_price: To raise the price of coffee (takes three input parameters and returns one output parameter).

Note: The original raise_price stored procedure takes two input parameters and one in/out parameter, but spwrap doesn't support in/out parameters, so I split it into one input and one output parameter.

We will use spwrap to simplify the call to these three stored procedures.

First, we need to create a Java interface to represent these three stored procedures:

public interface ColumbianDAO {

    List<SupplierCoffee> showSuppliers();

    String getSupplierOfCoffee(@Param(VARCHAR) String coffeeName);

    BigDecimal raisePrice(@Param(VARCHAR)String coffeeName,
                          @Param(FLOAT)float maximumPercentage,
                          @Param(NUMERIC) BigDecimal newPrice);

The interface contains three methods to represent the three stored procedures, the annotation @StoredProc is used to mark method as a stored procedure.

The annotation @Scalar is used to represent the return type of stored procedure so that the output parameter is mapped correctly to the method return type.

For the stored procedures that return their results as a result set, you need to provide a result set mapper to map the result set object to your domain object (SupplierCoffee), here's the mapper's implementation:

public class SupplierCoffee implements ResultSetMapper<SupplierCoffee> {
    private String supplierName, coffeeName;

    public SupplierCoffee map(Result<?> result) {
        // convert the result into SupplierCoffee        
        SupplierCoffee supplierCoffee = new SupplierCoffee();
        supplierCoffee.supplierName = result.getString(1);
        supplierCoffee.coffeeName = result.getString(2);
        return supplierCoffee;

    public String toString() {
        return "SupplierCoffee{" +
                "supplierName='" + supplierName + '\'' +
                ", coffeeName='" + coffeeName + '\'' +

And now you can call the stored procedures using the following code:

// you can provide dadaSource as well
DAO dao = new DAO.Builder("jdbc:mysql://localhost:3306/columbian", "root", "")
        .config(new Config().useStatusFields(false))

ColumbianDAO columbianDAO = dao.create(ColumbianDAO.class);

List<SupplierCoffee> supplierCoffees = columbianDAO.showSuppliers();

String coffee = "Colombian";
String supplier = columbianDAO.getSupplierOfCoffee(coffee);
System.out.printf("Supplier of the coffee '%s' is '%s'\n", coffee, supplier);

BigDecimal newPrice = columbianDAO.raisePrice(coffee, 0.10f, BigDecimal.valueOf(19.99));
System.out.printf("new price of '%s' is '%s'\n", coffee, newPrice);

Download the complete source code of the complete example at GitHub.

If you want to know more about spwrap, please visit the project page at GitHub.  If you like the project, please put a star on the project at GitHub. :)

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

jdbc ,stored procedure ,java ,mysql ,database ,dao

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}