DZone
Database Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Using Stored Procedures With JPA, JDBC… Meh, Just Use jOOQ

Using Stored Procedures With JPA, JDBC… Meh, Just Use jOOQ

Do you like verbosity and complexity? No? Neither do we. This is why we give you jOOQ.

Lukas Eder user avatar by
Lukas Eder
·
Jun. 10, 16 · Database Zone · Tutorial
Like (9)
Save
Tweet
11.11K Views

Join the DZone community and get the full member experience.

Join For Free

The current edition of the Java magazine has an article about Big Data Best Practices for JDBC and JPA by Josh Juneau. 

The article shows how to use a stored procedure with JDBC. (Notice how resources aren’t closed, unfortunately. This is commonly forgotten, even in Java Magazine articles)

// Using JDBC to call upon a database stored
// procedure
CallableStatement cs = null;
try {
    cs = conn.prepareCall("{call DUMMY_PROC(?,?)}");
    cs.setString(1, "This is a test");
    cs.registerOutParameter(2, Types.VARCHAR);
    cs.executeQuery();

    // Do something with result
    String returnStr = cs.getString(2);
} catch (SQLException ex){
    ex.printStackTrace();
}


And with JPA:

// Utilize JPA to call a database stored procedure
// Add @NamedStoredProcedureQuery to entity class
@NamedStoredProcedureQuery(
 name="createEmp", procedureName="CREATE_EMP",
 parameters = {
     @StoredProcedureParameter(
     mode= ParameterMode.IN,
     type=String.class,
     name="first"),
     @StoredProcedureParamter(
     mode = ParameterMode.IN,
     type=String.class,
     name="last")
 })

// Calling upon stored procedure
StoredProcedureQuery qry =
 em.createStoredProcedureQuery("createEmp");
qry.setParameter("first", "JOSH");
qry.setParameter("last","JUNEAU");
qry.execute();


Specifically, the latter was also recently discussed in blog posts by Vlad Mihalcea and Thorben Janssen.

Do You Like Verbosity and Complexity?

No? Neither do we. This is why we give you a third option instead: Just use jOOQ. Here’s the equivalent jOOQ code:

// JDBC example:
String returnStr = Routines.dummyProc(
 config, "This is a test");

// JPA example
Routines.createEmp(config, "JOSH", "JUNEAU");


Yes! That’s it. Don’t waste time manually configuring your bind variables with JDBC API calls, or JPA annotations. No one likes writing annotations for stored procedures. With jOOQ and jOOQ’s code generator, procedure calls are:

  • A one-liner
  • A no-brainer
  • A way to bring back the fun to stored procedures

Learn more about using Oracle stored procedures with nested collections and object types here:
Painless Access from Java to PL/SQL Procedures with jOOQ

Big data Java (programming language) Annotation Magazine PL/SQL POST (HTTP) API Blog Object (computer science)

Published at DZone with permission of Lukas Eder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • Choosing Between GraphQL Vs REST
  • Top Soft Skills to Identify a Great Software Engineer

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo