Since Java 6, the recommended way to access an SQL-based database in Java is via the JDBC(Java DataBase Connectivity) API.
This API comes in two packagages: java.sql
and javax.sql
.
JDBC defines database interactions in terms of Connections
and Drivers
.
A Driver
interacts with the database, and provides a simplified interface for opening and managing connections. Most database server varieties (PostgreSQL, MySQl, etc.) have their own Drivers
, which handle setup, teardown, and translation specific to that server. Drivers
are usually not accessed directly; rather, the interface provided by the DriverManager
object is used instead.
The DriverManager
object is essentially the core of JDBC. It provides a (mostly) database-agnostic interface to create Connections
. For older versions of the JDBC API, database-specific Drivers
had to be loaded before DeviceManager
could create a connection to that database type.
A Connection
is, as the name implies, a representation of an open connection to the database. Connections
are database-agnostic, and are created and provided by the DriverManager
. They provide a number of 'shortcut' methods for common query types, as well as a raw SQL interface.