Lesson: JDBC Introduction
The JDBC API is a Java API that can access any kind of tabular data, especially data stored in a relational database.
JDBC helps you to write Java applications that manage these three programming activities:
- Connect to a data source, like a database
- Send queries and update statements to the database
- Retrieve and process the results received from the database in answer to your query
The following simple code fragment gives a simple example of these three steps:
public void connectToAndQueryDatabase(String username, String password) < Connection con = DriverManager.getConnection( "jdbc:myDriver:myDatabase", username, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); while (rs.next()) < int x = rs.getInt("a"); String s = rs.getString("b"); float f = rs.getFloat("c"); >>
This short code fragment instantiates a DriverManager object to connect to a database driver and log into the database, instantiates a Statement object that carries your SQL language query to the database; instantiates a ResultSet object that retrieves the results of your query, and executes a simple while loop, which retrieves and displays those results. It’s that simple.
JDBC Product Components
JDBC includes four components:
- The JDBC API — The JDBC™ API provides programmatic access to relational data from the Java™ programming language. Using the JDBC API, applications can execute SQL statements, retrieve results, and propagate changes back to an underlying data source. The JDBC API can also interact with multiple data sources in a distributed, heterogeneous environment. The JDBC API is part of the Java platform, which includes the Java™ Standard Edition (Java™ SE ) and the Java™ Enterprise Edition (Java™ EE). The JDBC 4.0 API is divided into two packages: java.sql and javax.sql. Both packages are included in the Java SE and Java EE platforms.
- JDBC Driver Manager — The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. DriverManager has traditionally been the backbone of the JDBC architecture. It is quite small and simple. The Standard Extension packages javax.naming and javax.sql let you use a DataSource object registered with a Java Naming and Directory Interface™ (JNDI) naming service to establish a connection with a data source. You can use either connecting mechanism, but using a DataSource object is recommended whenever possible.
- JDBC Test Suite — The JDBC driver test suite helps you to determine that JDBC drivers will run your program. These tests are not comprehensive or exhaustive, but they do exercise many of the important features in the JDBC API.
- JDBC-ODBC Bridge — The Java Software bridge provides JDBC access via ODBC drivers. Note that you need to load ODBC binary code onto each client machine that uses this driver. As a result, the ODBC driver is most appropriate on a corporate network where client installations are not a major problem, or for application server code written in Java in a three-tier architecture.
This Trail uses the first two of these four JDBC components to connect to a database and then build a java program that uses SQL commands to communicate with a test relational database. The last two components are used in specialized environments to test web applications, or to communicate with ODBC-aware DBMSs.
Java JDBC API
The Java Database Connectivity (JDBC) API provides universal data access from the Java programming language. Using the JDBC API, you can access virtually any data source, from relational databases to spreadsheets and flat files. JDBC technology also provides a common base on which tools and alternate interfaces can be built.
The JDBC API is comprised of two packages:
You automatically get both packages when you download the Java Platform Standard Edition (Java SE) 8.
To use the JDBC API with a particular database management system, you need a JDBC technology-based driver to mediate between JDBC technology and the database. Depending on various factors, a driver might be written purely in the Java programming language or in a mixture of the Java programming language and Java Native Interface (JNI) native methods. To obtain a JDBC driver for a particular database management system, see JDBC Data Access API.
Enhancements in Java SE 8
Component: core-libs
Sub-Component: java.sql:bridge
Synopsis: The JDBC-ODBC Bridge has been removed.
RFE: 7176225
Component: core-libs
Sub-Component: java.sql
Synopsis: JDBC 4.2 introduces the following features:
- Addition of REF_CURSOR support.
- Addition of java.sql.DriverAction Interface
- >Addition of security check on deregisterDriver Method in DriverManager Class
- Addition of the java.sql.SQLType Interface
- Addition of the java.sql.JDBCType Enum
- Add Support for large update counts
- Changes to the existing interfaces
- Rowset 1.2: Lists the enhancements for JDBC RowSet.
Enhancements in Java SE 7
Component: core-libs
Sub-Component: java.sql
Synopsis: JDBC 4.1 introduces the following features:
- The ability to use a try -with-resources statement to automatically close resources of type Connection , ResultSet , and Statement
- RowSet 1.1: The introduction of the RowSetFactory interface and the RowSetProvider class, which enable you to create all types of row sets supported by your JDBC driver.
Component: docs
Sub-Component: release_notes
Synopsis: The JDBC-ODBC Bridge will be removed in JDK 8.
RFE: 8001747