4.2 JDBC APPLICATION FUNDAMENTALS
As we discussed in Section 3.1 in Chapter 3, to run a Java database application to perform data actions against the selected database, the JDBC API needs to perform the following operations:
1) Establish a connection between your Java application and related databases.
2) Build and execute Oracle statements.
3) Process the results.
In fact, to successfully develop and run a Java database application, the previous three steps need to be further divided into the following seven steps:
1) Import necessary Java packages, such as java.awt, java.util, javax.swing, java.sql and javax.sql.
2) Load and register the JDBC driver.
3) Establish a connection to the database server.
4) Create an Oracle statement.
5) Execute the built statement.
6) Retrieve the execution results.
7) Close the statement and connection objects.
For all these steps, step 1 is a prerequisite since all JDBC-related components and interfaces are defined in the java.sql and javax.sql packages. All GUI-related components are defined in the java.awt and javax.swing packages, and all other application-related components are defined in the java.util package. In order to use any component defined in those packages, you must first import those packages into your program to provide namespaces and locations for those
components. Otherwise, a compiling error may be encountered since the compiler cannot find and identify those components when you use them without providing the related packages.
In this and the following sections, we will provide a deeper and more detailed discussion about data actions in Java database applications based on these seven fundamental steps.
4.2.1 Loading and Registering Drivers
As we studied in Chapter 3, to establish a valid database connection, first you need to load and reg-ister a JDBC driver. Then you can call the connect() method to establish a database connection to your desired database.
We provided a brief discussion about the JDBC Driver and DriverManager components in Chapter 3. In fact, the core of the JDBC API is the JDBC Driver that can be accessed and called from the DriverManager class method. However, the Driver class is under the control of the DriverManager class, and the DriverManager is just that: a manager for the Driver class. When using this Driver class, you cannot call and run any method defined in the Driver class; instead, you need to call them via the DriverManager class methods.
The DriverManager class is a set of utility functions that work with the Driver methods together and manage multiple JDBC drivers by keeping them as a list of drivers loaded. Although loading a driver and registering a driver are two steps, only one method call is neces-sary to perform these two operations. The operational sequence of loading and registering a JDBC driver is:
1) Call class methods in the DriverManager class to load the driver into the Java interpreter.
2) Register the driver using the registerDriver() method.
When loaded, the driver will execute the DriverManager.registerDriver() method to register itself. These two operations will never be performed until a method in the DriverManager is executed, which means that even if both operations have been coded in an application, the driver cannot be loaded and registered until a method such as connect() is first executed.
To load and register a JDBC driver, two popular methods can be used:
1) Use the Class.forName() method: Class.forName(“oracle.jdbc.OracleDriver”);
2) Create a new instance of the Driver class: Driver oraDriver = new oracle.jdbc.OracleDriver;
Relatively speaking, the first method is more professional, since the driver is both loaded and registered when a valid method in the DriverManager class is executed. The second method can-not guarantee that the driver has been registered by using the DriverManager.
A piece of sample code used to load and register an Oracle 18c XE JDBC driver using the first
method is shown in Figure 4.3.
In Figure 4.3, the first codeline is used to import the JDBC API package java.sql.*.
Then a try-catch block is used to load and register an Oracle 18c XE JDBC Driver. The Class.forName() method is utilized to make sure that our JDBC Driver is not only loaded but also registered when it is connected by running the getConnection() method later. The argu-ment of this method, oracle.jdbc.OracleDriver, is the name of this Oracle 18c XE JDBC Driver class, and it is created by NetBeans when it is added to a Java database application project.
The catch block is used to track any possible error in loading and registering. The related exception information will be displayed if any error occurs.
You can use the second method instead to perform the same driver loading and registering opera-tion if you like.

FIGURE 4.3 Sample code for driver loading and registering.