fjdbc is a lightweight library to write JDBC code in a more convenient and functional style.
fjdbc is built around a small number of abstractions:
ConnectionProvider
is responsible for gettingjava.sql.Connection
instances.DbOperation
represents either a singlejava.sql.Statement
, or a sequence of Statements to be executed in a single transaction.ResultSetExtractor
is a functional interface to extract objects from ajava.sql.ResultSet
.PreparedStatementBinder
is a functional interface to bind the parameters of ajava.sql.PreparedStatement
.Query
represents a SQL SELECT statement.Fjdbc
is the library façade.SQLException
instances are wrapped in an uncheckedRuntimeSQLException
.
Requires Java >= 8.
<dependency>
<groupId>com.github.ewanld</groupId>
<artifactId>fjdbc</artifactId>
<version>0.1.0</version>
</dependency>
mvn package
final Connection connection = DriverManager.getConnection("jdbc/url/to/database");
final ConnectionProvider cnxProvider = new SingleConnectionProvider(connection);
final Fjdbc fjdbc = new Fjdbc(cnxProvider);
final String sql = "select name from user";
final SingleRowExtractor<String> extractor = (rs) -> rs.getString("name");
final List<String> names = fjdbc.query(sql, extractor).toList();
final String sql = "select name from user where role = ?";
final SingleRowExtractor<String> extractor = rs -> rs.getString("name");
final List<String> names = fjdbc
.query(sql, extractor)
.setBinder((ps, seq) -> ps.setString(seq.next(), "grunt"))
.toList();
final String sql = "update user set name='jack' where name='henri'";
final int nRows = fjdbc.statement(sql).executeAndCommit();
final String sql = "update user set name=? where name=?";
final int nRows = fjdbc
.statement(sql)
.setBinder((ps, seq) -> {
ps.setString(seq.next(), "jack");
ps.setString(seq.next(), "henri");
})
.executeAndCommit();
final StatementOperation updateName = fjdbc
.statement("update user set name=? where name=?")
.setBinder((ps, seq) -> {
ps.setString(seq.next(), "jack");
ps.setString(seq.next(), "henri");
});
final StatementOperation deleteManagers = fjdbc
.statement("delete from user where role=?")
.setBinder((ps, seq) -> {
ps.setString(seq.next(), "manager");
});
final int nRows = fjdbc.composite(updateName, deleteManagers).executeAndCommit();