Thin layer on top of JDBC providing higher-level abstractions and convenience operations.
- execute transactions easily
- extract your SQL queries from the code and move it to resources
- read entities from database using custom row mappers
- set query parameters safely using prepared statement setters
- avoid NULL-related problems while reading/writing to JDBC objects
- obtain auto-generated keys easily
You can include this library in your Maven project simply using the Jitpack service.
This has two steps. Step one, include this repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Step two, add this dependency (you can find the latest version in pom.xml
file):
<dependency>
<groupId>com.github.voho</groupId>
<artifactId>jdbcis</artifactId>
<version>{SPECIFY_VERSION_HERE}</version>
</dependency>
The latest version available is shown here:
First, let us prepare an extension point that will integrate your connection manager (e.g. a pool) with PureJDBC.
All you have to do is to provide an implementation of the ConnectionManager
interface.
This interface has two methods:
acquireConnection
- specify a way how PureJDBC can obtain a new connection, e.g. acquire from a connection poolreleaseConnection
- specify what to do with a connection that is no longer needed by PureJDBC, e.g. return back to a connection pool or simply do nothing
Example: integration with HikariCP:
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
dataSource.setUsername("bart");
dataSource.setPassword("51mp50n");
ConnectionManager connectionManager = new DataSourceConnectionManager(dataSource);
JdbcOperations pureJdbc = new JdbcOperations(cm);
PreparedStatementSetter setter = statement -> statement.setLongOrNull(18);
String sql = "SELECT * FROM person WHERE age > ?";
List<Person> people = pureJdbc.queryForList(sql, setter, mapper);
PreparedStatementSetter setter = statement -> statement.setLongOrNull(42);
String sql = "SELECT * FROM person WHERE id = ?";
Optional<Person> person = pureJdbc.queryForSingle(sql, setter, mapper);
PreparedStatementSetter setter = statement -> {
statement.setStringOrNull(1, "John Doe");
statement.setLongOrNull(2, 32);
};
String sql = "INSERT INTO person (name, age) VALUES (?, ?)";
Long newPrimaryKey = pureJdbc.insertAndGetSingleLongKey(sql, setter);
PreparedStatementSetter setter = statement -> {
statement.setStringOrNull(1, "John Doe");
statement.setLongOrNull(2, 32);
statement.setLongOrNull(3, 204);
};
String sql = "UPDATE person SET name = ?, age = ? WHERE id = ?";
pureJdbc.update(sql, setter);
PreparedStatementSetter setter = statement -> {
statement.setLongOrNull(1, 10024);
};
String sql = "DELETE FROM person WHERE id = ?";
pureJdbc.update(sql, setter);
A PreparedStatementSetter
instance can be used to set up various parameters on a prepared statement.
Note that the prepared statement is in fact a decorated object, providing more features than the original.
For getting the original, you can use the getInnerPreparedStatement()
method.
PreparedStatementSetter setter = (decoratedPreparedStatement) -> {
decoratedPreparedStatement.setStringOrNull(1, "value");
decoratedPreparedStatement.setLongOrNull(2, 42L);
};
A RowMapper
instance can be used to extract result set into a domain model object instance.
Note that the result set is in fact a decorated object, providing more features than the original.
For getting the original, you can use the getInnerResultSet()
method.
RowMapper<Person> rowMapper = (decoratedResultSet) -> {
Person person = new Person();
person.setName(decoratedResultSet.getNonNullString("name"));
person.setAge(decoratedResultSet.getNonNullInteger("age"));
return person;
};
Thank you very much for using the library.
If you have any feedback or feature wishes, please use the Issues
section.
I will be also very happy to learn about the library usages.