Java Database Connectivity (JDBC) is a set of classes that can be used to develop client/server applications that work with databases developed by Microsoft, Sybase, Oracle, Informix, and other sources. With JDBC, you can use the same methods and classes in Java programs to read and write records and perform other kinds of database access. A class called a driver acts as a bridge to the database source—there are drivers for each of the popular databases. Client/server software connects a user of information with a provider of that information, and it’s one of the most commonplace forms of programming. You use it every time you surf the Web: A web browser client requests pages, image files, and other documents using a uniform resource locator, or URL. Web servers provide the requested information, if it can be found, for the client.
One of the biggest obstacles faced by database programmers is the wide variety of database formats in use, each with its own proprietary method of accessing data. To simplify using relational database programs, a standard language called SQL (Structured Query Language) has been introduced. This language supplants the need to learn different database-querying languages for each database format. Java DB, the database included in Java 6, supports SQL. In database programming, a request for records in a database is called a query. Using SQL, you can send complex queries to a database and get the records you’re looking for in any order you specify.
Consider the example of a database programmer at a student loan company who has been asked to prepare a report on the most delinquent loan recipients. The programmer could use SQL to query a database for all records in which the last payment was more than 180 days ago and the amount due is more than $0.00. SQL also can be used to control the order in which records are returned, so the programmer can get the records in the order of Social Security number, recipient name, amount owed, or another field in the loan database.
All this is possible with SQL—the programmer doesn’t need any of the proprietary languages associated with popular database formats. SQL is strongly supported by many database formats, so, in theory, you should be able to use the same SQL commands for each database tool that supports the language. However, you will still need to learn the idiosyncrasies of a specific database format when accessing it through SQL.
SQL is the industry-standard approach to accessing relational databases. JDBC supports SQL, enabling developers to use a wide range of database formats without knowing the specifics of the underlying database. JDBC also supports the use of database queries specific to a database format. The JDBC class library’s approach to accessing databases with SQL is comparable to existing database-development techniques, so interacting with an SQL database by using JDBC isn’t much different than using traditional database tools. Java programmers who already have some database experience can hit the ground running with JDBC. The JDBC library includes classes for each of the tasks commonly associated with database usage:
· Making a connection to a database
· Creating a statement using SQL
· Executing that SQL query in the database
· Viewing the resulting records
These JDBC classes are all part of the java.sql package.
Database Drivers
Java programs that use JDBC classes can follow the familiar programming model of issuing SQL statements and processing the resulting data. The format of the database and the platform it was prepared on don’t matter. This platform- and database independence is made possible by a driver manager. The classes of the JDBC class library are largely dependent on driver managers, which keep track of the drivers required to access database records. You’ll need a different driver for each database format that’s used in a program, and sometimes you might need several drivers for versions of the same format. Java DB includes its own driver. JDBC also includes a driver that bridges JDBC and another database-connectivity standard, ODBC.