Code That Made Me Cry, #CTMMC™

95% of all code makes me cry. The other 5% are white space.

How to Call Stored Procedures (Java, #7 / 50)

// Using implementation-dependent JPA 2.0
StoredProcedureCall storedProcedureCall = new StoredProcedureCall();
storedProcedureCall.setProcedureName("mypackage.myprocedure");
storedProcedureCall.addNamedArgument("i_input_1");
storedProcedureCall.addNamedOutputArgument("o_output_1");
DataReadQuery query = new DataReadQuery();
query.setCall(storedProcedureCall);
query.addArgument("i_input_1");
List<Object> argumentValues = new ArrayList<Object>();
argumentValues.add("valueOf_i_input_1");
Session session = jpaEntityManager.getActiveSession();
List<?> results = 
    (List<?>) session.executeQuery(query, argumentValues);
DatabaseRecord record = (DatabaseRecord) results.get(0);
String result = String.valueOf(record.get("o_output_1"));

// Using implementation-independent JPA 2.1
StoredProcedureQuery query = getEntityManager()
    .createStoredProcedureQuery("mypackage.myprocedure");
query.registerStoredProcedureParameter(
    "i_input_1", String.class, ParameterMode.IN);
query.registerStoredProcedureParameter(
    "o_output_1", String.class, ParameterMode.OUT);
query.setParameter("i_input_1", "valueOf_i_input_1");
boolean queryResult = query.execute();
String result = String.valueOf(
    query.getOutputParameterValue("o_output_1"));        

Calling stored procedures can be such a pain using JDBC or JEE's standard approaches as pointed out by Malcolm Boekhoff in his Stack Overflow answer:

Source: http://stackoverflow.com/a/19107545/521799

comments powered by Disqus

Submit your own Code That Made You Cry

Send us your own example of code that made you cry! Please include:

Send the above to ctmmc@datageekery.com. All accepted submissions will be licensed as CC BY-SA 3.0. Submit only code of a compatible license.

Note that we may choose not to publish some submissions if we feel they're not appropriate. We hope you understand.

Don't let your SQL code make you cry!

Use jOOQ, instead of JDBC.

jOOQ generates Java code from your database and lets you build typesafe SQL queries through its fluent API.

Don't let your SQL code make you cry! Get back in control of your SQL with jOOQ.