Data or information is at the heart of most business applications, and JDBC deals with data stored and manipulated in relational database systems.
This book takes an example based approach to describing the features and functionalities available in JDBC. Whether you are a new or an experienced database/JDBC developer, you should find the examples and accompanying text a valuable and accessible knowledge base for creating your own database solutions.
In this book, I will use some basic Java/JDBC utility classes (such as the DatabaseUtil class), which are available for download from the book’s website. The DatabaseUtil class provides methods for closing JDBC objects (such as Connection, ResultSet, Statement and PreparedStatement).
So What is JDBC ?
JDBC is a set of programming APIs that allows easy connection to a wide range of databases (esp relational databases) through Java programs.
In today’s programming world, JDBC is the standard for communication between a Java application and a relational database. JDBC is simple and powerful because it is a database-independent way of manipulating data from any relational database.
In a nutshell, JDBC is a database-independent API for accessing a relational database. You pass SQL to Java methods in the JDBC classes (the packages java.sql and javax.sql ) and get back JDBC objects (such as ResultSet) that represent the results of your query. JDBC is designed in a simple way, so most database programmers need to learn only a few methods to do most of what database programmers need to do to accomplish database programming tasks.
In this section, I presented the basic outline of the JDBC architecture. JDBC’s DriverManager class provides the basic service for managing a set of JDBC drivers. I will talk about these classes and interfaces again.
What is JDBC’s High-Level Architecture ?
Your Java programs interact with only the JDBC API. The sole purpose of JDBC is to read data from databases and to write data back to the databases. With JDBC, though, you can do more than reading/writing records.
You can even read the metadata about tables, views, and other useful objects in databases. For example, using JDBC’s DatabaseMetadata, you can find out the name and number of columns for a given table and their associated data types. This information is useful in developing GUI applications.
According to Sun, the JDBC API contains two major sets of interfaces:
- the first is the JDBC API for application writers.
- the second is the lower-level JDBC driver API for driver writers.
This book focuses on the JDBC API for application writers.
The JDBC API does most of the things through the DriverManager class (java.sql.DriverManager).
What is DriverManager ?
It is a connection factory class. In fact, DriverManager is the only class that can create database connections. (Each database connection is represented by an instance of a java.sql.Connection) The DriverManager uses drivers to create connections. Each vendor (such as Oracle, MySQL, and Sybase) provides a set of drivers.
Refer to the JDBC detailed architecture:
- Java code calls a JDBC library (using the java.sql and javax.sql packages)
- JDBC loads a driver, for example, an Oracle driver is loaded using the following code snippet:
Class.forName(“oracle.jdbc.driver.OracleDriver”)
- Calling Class.forName() automatically creates an instance of the driver and registers the driver with the DriverManager class.
- The driver talks to a particular database such as Oracle or MySQL
WHO PROVIDES THESE JDBC DRIVERS ?
Usually, a database vendor (such as MySQL, Oracle, Sybase, and so on) writes a JDBC driver ( a specific software for a specific database), which is a set of classes/interfaces that implements these interfaces for a particular database system.
Following the JDBC architecture, a Java database application uses the DriverManager class to get the java.sql.Connection object, which represents a database connection. Then using the Connection object, you can create Statement/PreparedStatement/ CallableStatement, which can execute SQL queries and stored procedures and return results as ResultSet objects.
ResultSet is a table of data representing a database result set, which is usually generated by executing a statement that queries the database.
The JDBC API is comprised of two Java packages: java.sql and javax.sql. The following are core JDBC classes, interfaces and exceptions in the java.sql package:
DriverManager: This class loads JDBC drivers in memory. You can also use it to create java.sql.Connection objects to data sources (such as Oracle, MySQL and so on).
Connection: This interface represents a connection with a data source. You can use the Connection object for creating Statement, PreparedStatement, and CallableStatement objects.
Statement: This interface represents a static SQL statement. You can use it to retrieve ResultSet objects.
PreparedStatement: This interface extends Statement and represents a precompiled SQL statement. You can use it to retrieve ResultSet objects.
CallableStatement: This interface represents a database stored procedure.