Package guru.mikelue.jdut.jdbc

This package provides coding library of JDBC by usage of lambda expression.
When we are coding database operations with JDBC, there are two things important, and, annoying:
  1. The handling of SQLException
  2. The release of resource
This package is intent on "surrounding" your JDBC code by lambda expressions.

Quick example

Following example shows the basic usage of JDBC coding.

 // dataSource - The initialized object of javax.sql.DataSource

 JdbcVoidFunction<Connection> jdbcFunc = JdbcTemplateFactory.buildRunnable(
     () -> dataSource.getConnection(), // The supplier provides connection object
     connection -> {
         JdbcExecuteFactory.buildRunnable(
             () -> connection.createStatement(), // The supplier provides statement object
             JdbcTemplateFactory.buildRunnable(
                 statement -> {
                     statement.executeUpdate("INSERT INTO tab_car VALUES('CC-01', 20)");
                 };
             )
         ).runJdbc();
     }
 );

 try {
     jdbcFunc.runJdbc();
 } catch (SQLException e) {
     throws new RuntimeException(e);
 }
 

JdbcFunction and JdbcVoidFunction

JdbcFunction is the core function to implement your JDBC operations, which returns value.
JdbcVoidFunction is the convenient function without return value.

Surrounding

You may implement SurroundOperator to surround functions.
Using either JdbcFunction.surroundedBy() or JdbcExecuteFactory.surround(), you can surround your code by lambda expression.

 JdbcFunction<Connection, Integer> funcGetCount = conn -> { 20 };
 funcGetCount.surroundedBy(
     func -> conn -> {
         logger.info("Before calling");
         func.applyJdbc(conn);
         logger.info("After calling");
     }
 );
 

Transaction

With benefit of JdbcFunction.SurroundOperator, there are some build-in functions to provide surrounding of Connection which is inside transaction-ready code.
Reference Transactional.

 JdbcVoidFunction<Connection> jdbcFunc = JdbcTemplateFactory.buildRunnable(
     () -> dataSource.getConnection(), // The supplier provides connection object
     connection -> {
         // Using connection....
     },
     surroundingList -> {
         surroundingList.add(Transactional::new);
     };
 )
 
See Also:
JdbcFunction, JdbcTemplateFactory