Book Image

JDBC 4.0 and Oracle JDeveloper for J2EE Development

Book Image

JDBC 4.0 and Oracle JDeveloper for J2EE Development

Overview of this book

Table of Contents (20 chapters)
JDBC 4.0 and Oracle JDeveloper for J2EE Development
Credits
About the Author
About the Reviewer
Preface

Chapter 1. JDBC 4.0

The Java Database Connectivity API is used to access a SQL database from a Java application. JDBC also supports tabular data sources, such as a spreadsheet. We will constrain our discussion to SQL relational databases. Using JDBC API, SQL statements can be run in a database. JDBC started as JDBC 1.0 API; JDBC 1.0 covered the basics of establishing a connection with a database, running SQL statements, retrieving values from result sets, and using transactions. JDBC 2.0 introduced scrollable result sets, JDBC methods to update a result set or a database table, batch updates, and SQL3 data types such as, BLOB, CLOB, Array, Ref, and Struct. JDBC 3.0 introduced savepoints, connection pooling of prepared statements, multiple open ResultSet objects, BOOLEAN data type, and an interface for parameter metadata and for retrieving database metadata. JDBC 4.0 specifications added some new features, which we will discuss in this chapter.

The JDBC API provides various interfaces and classes for accessing a database; creating tables in the database; and adding, updating, deleting data, in the database tables. In the following sections, we will discuss some of the JDBC classes and interfaces. We will also discuss the new methods added to these classes or interfaces, in JDBC 4.0 specifications. To run a JDBC 4.0 application, install a RDBMS database such as the open-source MySQL database or the commercial Oracle database. A JDBC driver class is required to establish a connection with the database. JDBC drivers are vendor-specific. A JDBC driver class implements the java.sql.Driver interface.

DriverManager Class

The DriverManager class is used to obtain a connection with a database. A JDBC driver is required to be loaded before obtaining a connection with the database. In JDBC 3.0, a JDBC driver can be loaded either by specifying it in the jdbc.drivers system property, or by using the Class.forName() method. We require invoking the Class.forName() method by loading the Oracle JDBC driver, oracle.jdbc.OracleDriver, using JDBC 3.0.

Class.forName("oracle.jdbc.OracleDriver");

In JDBC 4.0 specifications, the DriverManager class has added support to getConnection() and getDrivers() methods, for the Java SE (Service Provider) mechanism. By using these methods, JDBC drivers may be loaded automatically. The Class.forName() method is not required to be invoked. Loading drivers using the Java SE Service Provider mechanism will be discussed in the Automatic SQL Driver Loading section.

A JDBC connection is represented by a java.sql.Connection object, and may be obtained from a DriverManager by calling the overloaded static getConnection() methods. The getConnection() method is listed in following table:

getConnection() Method

Description

getConnection(String url )

Obtains a connection with the specified database URL.

getConnection(String url, Properties properties)

Username and password may be specified in the Properties Hashtable.

getConnection(String url, String user, String password)

Obtains a connection with a URL username, and password.

For example, a connection with the Oracle database may be obtained as shown below:

String url="jdbc:oracle:thin:@localhost:1521:ORCL";
Connection connection = DriverManager.getConnection(url, "oe", "pw");