JDBC is a set of programming APIs that allows easy connection to a wide range of databases(especially relational databases) through Java programs. In this book, we will be using JDBC 2.0 and 3.0 versions(JDBC 4.0 is just a specification and has not been implemented extensively yet.) In Java 2 Platform Standard Edition (J2SE) 5.0 (which supports JDBC 3.0), the JDBC API is defined by two packages:

java.sql provides the API for accessing and processing data stored in a data source(usually a relational database) using the Java programming language. This package provides the foundation and most commonly used objects(such as Connection, ResultSet, Statement, and PreparedStatement). Also, this package provides classes and interfaces to get both database and result set metadata from the database server. This package has a set of classes and interfaces (such as DatabaseMetaData and ResultSetMetaData) that deal with database metadata, which will be one of the focuses of this book.

javax.sql provides the API for server-side data source access. According to the Java Development Kit(JDK) documentation, “This package supplements the java.sql package and, as of the version 1.4 release, is included in the JDK. It remains an essential part of the Java 2 SDK, Enterprise Edition (J2EE).” This package provides services for J2EE(such as DataSource and RowSets). Also, the package has a set of classes and interfaces(such as RowSetMetaData) that deal with row set metadata. In this book we focus on the metadata components of this package.

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, DatabaseMetaData, and ResultSetMetaData) that represent the results of your query. JDBC is designed so simply that most database programmers need learn only a few methods to accomplish most of what they need to do.

Figure shows how a database application(such as a Java application/applet/servlet) uses JDBC to interact with one or more databases.

Java database application using JDBC

Java database application using JDBC

Figure presents the basic outline of the JDBC architecture. JDBC’s DriverManager class provides the basic service for managing a set of JDBC drivers. The DriverManager loads JDBC drivers in memory, and can also be used to create java.sql.Connection objects to data sources(such as Oracle and MySQL).

Note that you can have more than one driver and therefore more than one database.

Figure illustrates how a Java application uses JDBC to interact with one or more relational databases(such as Oracle and MySQL) without knowing about the underlying JDBC driver implementations. Figure illustrates the core JDBC classes and interfaces that interact with Java and JDBC applications. This figure also shows the basic relationships of the DatabaseMetaData and ResultSetMetaData interfaces with other JDBC objects.

Using JDBC database metadata

Using JDBC database metadata

The following are core JDBC classes, interfaces, and exceptions in the java.sql package:

  • DriverManager: This class loads JDBC drivers in memory. It is a “factory” class and can also be used to create java.sql.Connection objects to data sources(such as Oracle, MySQL, etc.).
  • Connection: This interface represents a connection with a data source. The Connection object is used for creating Statement, PreparedStatement, and CallableStatement objects.
  • DatabaseMetaData: This interface provides detailed information about the database as a whole. The Connection object is used for creating Database MetaData objects.
  • Statement: This interface represents a static SQL statement. It can be used to retrieve ResultSet objects.
  • PreparedStatement: This interface extends Statement and represents a precompiled SQL statement. It can be used to retrieve ResultSet objects.
  • CallableStatement: This interface represents a database stored procedure. It can execute stored procedures in a database server.
  • ResultSet: This interface represents a database result set generated by using SQL’s SELECT statement. Statement, PreparedStatement, CallableStatement, and other JDBC objects can create ResultSet objects.
  • ResultSetMetaData: This interface provides information about the types and properties of the columns in a ResultSet object.
  • SQLException: This class is an exception class that provides information on a database access error or other errors.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics