DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Using spwrap to Simplify Calling Stored Procedures From Java

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.

Mohammed Hewedy user avatar by
Mohammed Hewedy
·
Mar. 08, 17 · Tutorial
Like (2)
Save
Tweet
Share
8.46K Views

Join the DZone community and get the full member experience.

Join For Free

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 {

    @StoredProc("SHOW_SUPPLIERS")
    List<SupplierCoffee> showSuppliers();

    @Scalar(VARCHAR)
    @StoredProc("GET_SUPPLIER_OF_COFFEE")
    String getSupplierOfCoffee(@Param(VARCHAR) String coffeeName);

    @Scalar(NUMERIC)
    @StoredProc("RAISE_PRICE")
    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;

    @Override
    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;
    }

    @Override
    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))
        .build();

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

List<SupplierCoffee> supplierCoffees = columbianDAO.showSuppliers();
supplierCoffees.forEach(System.out::println);

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. :)

Java (programming language)

Published at DZone with permission of Mohammed Hewedy. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Discovery With Eureka
  • Express Hibernate Queries as Type-Safe Java Streams
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • API Design Patterns Review

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: