Chapter 1. Introduction to JDBC

Oracle JDBC is where the write-once-run-anywhere database meets Java, the write-once-run-anywhere programming language. JDBC acts as the bridge between Oracle and Java. But what is JDBC? JDBC is a Java API for executing dynamic SQL statements. Oracle JDBC is for executing dynamic SQL statements in a standard way and for leveraging Oracle’s extended functionality. Consequently, when you use Oracle as your persistent storage, you have to make a decision early on as to whether to program for portability or for additional performance and functionality.

Before we start our discussion on how you can use JDBC, I think it’s appropriate to cover some required background information. In this chapter, we’ll start by looking at the architecture of the JDBC API. Then we’ll continue by defining client/server and four different types of Oracle clients. Finally, we’ll finish with my soapbox speech about how it’s important to use the set capabilities of SQL.

The JDBC API

In this section, I will try to give you the big picture of the JDBC API. Given this overview, you’ll have a contextual foundation on which to lay your knowledge as you build it chapter by chapter while reading this book.

The JDBC API is based mainly on a set of interfaces, not classes. It’s up to the manufacturer of the driver to implement the interfaces with their own set of classes. Figure 1-1 is a class diagram that shows the basic JDBC classes and interfaces; these make up the core API. Notice that the only concrete class is DriverManager. The rest of the core API is a set of interfaces.

The interfaces of the core JDBC API

Figure 1-1. The interfaces of the core JDBC API

I’ll take a second to explain some of the relationships in the diagram. DriverManager is used to load a JDBC Driver. A Driver is a software vendor’s implementation of the JDBC API. After a driver is loaded, DriverManager is used to get a Connection. In turn, a Connection is used to create a Statement, or to create and prepare a PreparedStatement or CallableStatement. Statement and PreparedStatement objects are used to execute SQL statements. CallableStatement objects are used to execute stored procedures. A Connection can also be used to get a DatabaseMetaData object describing a database’s functionality.

The results of executing a SQL statement using a Statement or PreparedStatement are returned as a ResultSet. A ResultSet can be used to get the actual returned data or a ResultSetMetaData object that can be queried to identify the types of data returned in the ResultSet.

The six interfaces at the bottom of Figure 1-1 are used with object-relational technology. A Struct is a weakly typed object that represents a database object as a record. A Ref is a reference to an object in a database. It can be used to get to a database object. An Array is a weakly typed object that represents a database collection object as an array. The SQLData interface is implemented by custom classes you write to represent database objects as Java objects in your application. SQLInput and SQLOutput are used by the Driver in the creation of your custom classes during retrieval and storage.

In Oracle’s implementation of JDBC, most of the JDBC interfaces are implemented by classes whose names are prefixed with the word Oracle. Figure 1-2 shows these classes and is laid out so that the classes correspond positionally with those shown in Figure 1-1.

Oracle’s implementation of the JDBC API interfaces

Figure 1-2. Oracle’s implementation of the JDBC API interfaces

As you can see from Figure 1-2, the only interface not implemented by an Oracle class is SQLData. That’s because you implement the SQLData interface yourself with custom classes that you create to mirror database objects. Now that you’ve got the big picture for the JDBC API, let’s lay a foundation for understanding what I mean when I used the term client with respect to JDBC.

Get Java Programming with Oracle JDBC now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.