About this tutorial
This tutorial introduces the different concepts involved in establishing and managing a database connection from within a Java application using Java Database Connection (JDBC). It is targeted primarily at developers who want to understand what is "going on under the hood" when using a database from within a Java application.
This tutorial assumes familiarity with the Java programming language. The links in Resources include referrals to additional information on both JDBC and specific databases.
This tutorial demonstrates how to connect to a database using JDBC. While seemingly innocuous, this subject is actually a stumbling block for both newcomers and veterans alike.
This tutorial will discuss how a Java application inside a JVM discovers and communicates with a database, starting with the traditional JDBC driver and DriverManager objects. After several examples that demonstrate the four different types of JDBC drivers, the tutorial moves on to discuss DataSource objects that use JNDI. A discussion of JNDI, and how to bind, use, rebind, and delete the DataSource object is also included. Finally, the concept of a connection pool, and specifically PooledConnection objects are introduced and demonstrated. The tutorial concludes with a discussion of tuning issues that are often overlooked when developing database connectivity applications.
While the tutorial provides numerous code snippets to reflect concepts and methods described in the text, most people learn better by actually working through the examples. To work through the examples, you will need to have the following tools installed and working correctly:
- A text editor: Java source files are simply text, so to create and read them,
all you need is a text editor. If you have access to a Java IDE, you can also use it, but sometimes they hide too many of the details.
- A Java development environment, such as the Java2 SDK, which is available at
http://java.sun.com/j2se/1.4/.
The Java2 SDK, Standard Edition version 1.4, includes the JDBC standard extensions as well as JNDI, which are both necessary for some of the latter examples in the book.
- An SQL-compliant database: The examples in this tutorial use a wide variety of different databases to help demonstrate how database independent JDBC programming can be. Resources contains links to more information on both JDBC and databases.
- A JDBC driver: Because the JDBC API is predominantly composed of interfaces, you need to obtain an actual JDBC driver implementation in order to actually connect to a database using JDBC. If your database (or your wallet) does not allow the use of JDBC, you can always use the JDBC-ODBC bridge driver to connect to any database (or data source) that supports the ODBC protocol.


