|Java JDBC Tutorial|
|JDBC Driver Types|
|JDBC Open Database Connection|
|JDBC Query the Database|
|JDBC Update the Database|
|JDBC Batch Updates|
The JDBC API consists of the following core parts:
There are four basic JDBC use cases around which most JDBC work evolves:
I will explain both the core component and common use cases in the following sections.
A JDBC driver is a collection of Java classes that enables you to connect to a certain database. For instance, MySQL will have its own JDBC driver. A JDBC driver implements a lot of the JDBC interfaces. When your code uses a given JDBC driver, it actually just uses the standard JDBC interfaces. The concrete JDBC driver used is hidden behind the JDBC interfaces. Thus you can plugin a new JDBC driver without your code noticing it.
Of course, the JDBC drivers may vary a little in the features they support.
Once a JDBC driver is loaded and initialized, you need to connect to the database. You do so
by obtaining a
Connection to the database via the JDBC API and the loaded driver. All communication
with the database happens via a connection. An application can have more than one connection
open to a database at a time. This is actually very common.
Statement is what you use to execute queries and updates against the database. There are
a few different types of statements you can use. Each statement corresponds to a single
query or update.
When you perform a query against the database you get back a
ResultSet. You can
then traverse this
ResultSet to read the result of the query.
One of the most common use cases is to read data from a database. Reading data from a database is called querying the database.
Another common use case is to query the database meta data. The database meta data contains information about the database itself. For instance, information about the tables defined, the columns in each table, the data types etc.
Another very common JDBC use case is to update the database. Updating the database means writing data to it. In other words, adding new records or modifying (updating) existing records.
Transactions is anoter common use case. A transaction groups multiple updates and possibly queries into a single action. Either all of the actions are executed, or none of them are.
Here is an example of how the core components interact in during the execution of a database query (click image to view larger version):
|Java JDBC: Interaction of the core JDBC components during the execution of a database query.|