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:
Using either
Reference
When we are coding database operations with JDBC, there are two things important, and, annoying:
- The handling of
SQLException - The release of resource
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 implementSurroundOperator 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 ofJdbcFunction.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
-
Interface Summary Interface Description JdbcFunction<T,R> Likes theFunctioninterface with throwing ofSQLException.JdbcFunction.SurroundOperator<T,R> This operator is used to surround fedJdbcFunctionby the implementation of surrounding.JdbcRunnable Likes theRunnableinterface with throwing ofSQLException.JdbcSupplier<R> Likes theSupplierinterface with throwing ofSQLException.JdbcVoidFunction<T> Likes theConsumerinterface with throwing ofSQLException.SQLExceptionConvert<E extends RuntimeException> Converts theSQLExceptioninto another instance ofRuntimeException.SurroundingConfig<T,R> This interface defines the lambda expression of configuration ofListofJdbcFunction.SurroundOperator. -
Class Summary Class Description JdbcTemplateFactory Template factory for building a bunch a JDBC working, with surrounding ofDbRelease.autoClose(guru.mikelue.jdut.jdbc.JdbcFunction<T, R>).
SeeJDBC Functionfor usage of this factory.