The DBMS segment of the software business is no more unified than the operating system or Web services segments. Many different vendors have entered and left the market over the years, leaving a trail of different file formats, APIs, dialects of SQL, hardware constraints, and compatibility problems.
Three significant developments in the computing world served to narrow the problem of multiple incompatible data formats somewhat, but not completely eliminate it. The first development was the publishing of a paper by E. F. Codd, a mathematician, who describes the foundations of what he called the "relational database." The relational database approach proved to be a simplifying concept that gained almost universal acceptance in the user community. The major impact of this was on the user's learning curve. Relational Database Management Systems (RDBMS) resemble each other quite a bit, even when produced by different vendors.
The second development was the creation of theStructured Query Language(SQL). This language narrowed the syntax of the statements used to separate dialects of the same language instead of wildly different languages as existed before.
The third development was the universal acceptance of TCP/IP and a means of communicating between computers. TCP/IP impacted the DBMS world indirectly, but powerfully, because it provided a universal protocol that could be used to move data from one computer to another. This provided a mechanism for a program on one computer to request data on a different computer.
The existence of these three mechanisms provided the foundation for the development of JDBC drivers for different database management systems that can run on different computers. JDBC assumes that the database you are contacting is relational and it uses SQL for its query language. In addition, its drivers are normally written to communicate with the database server using TCP/IP.
JDBC is based on the Open Database Connection (ODBC) approach introduced by Microsoft in the early 1990s. Both JDBC and ODBC are based on the existence of drivers that mask the differences in the specific dialects of SQL. In addition, the driver provides the computer-to-computer connectivity needed in distributed systems. The end result is that the database is as easy to deal with (from the program's point of view), as it would be if it were located on the same machine. Figure 19.2 shows this graphically.
Figure 19.2. The JDBC driver hides many of the details of accessing the database from the programmer.
Your Java program is running on your computer. It contains JDBC code, which communicates with a Java class that serves as the JDBC Driver. This driver contains code that knows how to communicate with a DBMS that is often located on another computer. Using this driver, your program can execute a full range of database functionality, including creating tables, modifying the schema of a table, storing data, and retrieving information from a database.
You only need one JDBC driver to access your database. There are four different types of JDBC drivers to choose from; some offering better performance than the others:
Type 1 This driver connects to the DBMS by using a datasource that has been defined in a MS Windows ODBC manager. In the early days of JDBC, there were not many drivers available, so it was common to piggyback on existing ODBC drivers to access the data. Now that more advanced drivers are widely available, this driver is mostly used in examples and simple systems. It suffers from additional overhead introduced by having the extra ODBC layer in the solution.
Type 2 These drivers make method calls in other languages to networking software installed on the client machine. The performance of this driver is better, but the distribution of your application is more complex because you must ensure that the network software is installed on every client machine.
Type 3 These drivers are written in 100% Java on the client side. On the server side, there is a component that the client communicates with. This removes the requirement that every client have an extra piece of software and moves that requirement to the server.
Type 4 This is the easiest type of driver to install and use. It is written in Java and contains networking code right inside its classes. No additional installation is necessary on either the client or server.
After you have followed the instructions for installing any of the driver types, they all look the same to your program. The JDBC code uses the same SQL dialect regardless of the driver or driver type used. You need only enter the correct connection URL for the database that you want to access, and the driver does the heavy lifting for you.
It is a common practice for programmers to develop using whatever JDBC driver is readily available, and then change the driver to one whose performance is better suited to a production system.
Normally, you can obtain a JDBC driver from the vendor of your DBMS. In addition, third-party software companies often sell drivers that outperform the vendor's driver.
In this chapter, the Type 1 driver will be used in all examples. This driver ships with the Windows version of the JDK.