Introduction to JDBI
JDBI is a Java library that can be used to interact with a relational database. JDBI provides a more friendly interface than JDBC, which makes it easier to write code that is correct and safe.
Getting started
The easiest way to create a JDBI instance is by passing a DataSource:
1
2
3
final MysqlDataSource ds = new MysqlDataSource();
datasource.setURL("jdbc:mysql://0.0.0.0:3306/test?user=root&password=my-secret-pw");
final Jdbi jdbi = Jdbi.create(ds);
Once we have a Jdbi
object we can use it to get handles
. A handle
represents an active database connection. Handles need to be closed once they are not needed, or the database might be overwhelmed by open connections. A simple example of using a handle to execute an insert operation: