JDBC

Released for the first time in 1997, Java Database Connectivity (JDBC) has since been defining how applications communicate with databases (primary relational), providing a unified API for data access on a Java platform. The latest API revision, 4.3, was released in 2017 and was included in Java SE 9.

JDBC allows multiple database client drivers to exist and be used by the same application. JDBC Driver Manager is responsible for correct registration, loading, and usage of required driver implementation. When the driver is loaded, the client may create a connection with appropriate access credentials. JDBC connections make it possible to initialize and execute statements such as SQL's SELECT, CREATE, INSERT, UPDATE, and DELETE. Statements that update the state of the database as an execution result return a number of affected rows, and query statements return java.sql.ResultSet, which is an iterator over the rows of the result. ResultSet was designed long ago and has a strange API. For example, an index that enumerates columns in a row starts from 1, not from 0.

The ResultSet interface is designed for backward iteration, and even random access, but that level of compatibility requires the driver to load all the rows before allowing any processing. For the sake of simplicity, we assume that ResultSet resembles a simple iterator over the rows of the result. This assumption allows an underlying implementation to operate over a chunked result set and load batches from the database on demand. Any underlying asynchronous implementation has to be wrapped into synchronous blocking calls at the level of JDBC.

In the area of performance, JDBC allows batching for non-select queries. This should allow communicating with the database in fewer network requests. However, because JDBC is designed to be synchronous and blocking, that does not help when processing big datasets.

Even though JDBC was designed as a business-logic level API, it operates with tables, rows, and columns, instead of entities and aggregates as domain-driven design recommends. Consequently, nowadays, JDBC is considered to be too low-level for direct use. For such purposes, the Spring ecosystem has the Spring Data JDBC and Spring Data JPA modules. There are also a lot of well-established libraries that wrap JDBC and provide a more pleasant API to use. One example of such a library is Jdbi. It proposes not only a fluent API but also has excellent integration with the Spring ecosystem.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.32.67