I want to know about the differences between JDBC and JDBI in java. In particular, which one is generally better and why?
Jdbi is an open source Java library (Apache license) that uses lambda expressions and reflection to provide a friendlier, higher level interface than JDBC to access the database.
Jdbi is not an ORM.
The short answer on the fundamental difference between JDBC and Hibernate is that Hibernate performs an object-relational mapping framework, while JDBC is simply a database connectivity API. The long answer requires a history lesson on database access with Java.
JDBC is database-dependent, which means that different scripts must be written for different databases. On the other side, JPA is database-agnostic, meaning that the same code can be used in a variety of databases with few (or no) modifications.
(I am the primary author of jDBI)
jDBI is a convenience library built on top of JDBC. JDBC works very well but generally seems to optimize for the database vendors (driver writers) over the users. jDBI attempts to expose the same functionality, but in an API optimized for users.
It is much lower level than things like Hibernate or JPA. The closest similar library is probably MyBatis (forked successor to iBATIS).
jDBI supports two style APIs, an older fluent style, which looks like:
List<Something> r = h.createQuery("select * from something where name = :name and id = :id") .bind(0, "eric") .bind("id", 1) .map(Something.class) .list();
A newer SQL Object API does much more reflective type stuff and really does start to abstract a bunch of JDBC stuff:
interface TheBasics { @SqlUpdate("insert into something (id, name) values (:id, :name)") int insert(@BindBean Something something); @SqlQuery("select id, name from something where id = :id") Something findById(@Bind("id") long id); } @Test public void useTheBasics() throws Exception { TheBasics dao = dbi.onDemand(TheBasics.class); dao.insert(new Something(7, "Martin")); Something martin = dao.findById(7); }
The library has good reference docs (javadoc) and some reasonable tutorial style documentation at http://jdbi.org/. It has been around since 2004, and is used by a relatively small number of folks (some few dozen people I know of personally, and maybe a dozen companies) but it works very well for them. Most of the folks who work on it are A+ folks, and are primarily concerned with building a tool that works well for them -- that it is open source is largely a side effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With