Home > Articles > Programming > Java

  • Print
  • + Share This

The JDBC Optional Package

The Core JDBC package is part of the standard Java Development Kit. There is also an additional JDBC package that is part of J2EE that addresses some of the enterprise-level uses of JDBC. These extensions belong to the javax.sql package as opposed to java.sql.

Data Sources

One of the irritating parts of JDBC is the way to load drivers. It can be difficult to reconfigure an application to use a different database. Using the standard JDBC API, you must use a specific JDBC URL to access a database. The Optional JDBC package includes an alternative to the DriverManager class for creating database connections.

A DataSource object works like the DriverManager class, it has a getConnection method and also a getLogWriter method. The big difference is that a data source is typically stored in a naming service and accessed through the Java Naming API (JNDI). The naming service gives you an extra level of indirection—you don't code the JDBC URL into your application, and you don't need to reconfigure every application when you change the database URL. Instead, your application can always ask for a specific name in the naming service. When you configure your application server, you can change the database that a particular named DataSource object refers to. You need to change it in only one place instead of every place that uses the database.

Connection Pools

A connection pool is actually just a special data source that maintains a collection of database connections. In a typical application, you need to perform many database operations at once, so you usually need several database connections. The problem is, you don't know when you'll need a particular database connection, so your best bet is to put the connections into a common pool and pull out a connection when you need it.

In the past, programmers have solved the connection pooling problem by creating their own connection pool. One of the problems you often encounter with custom connection pools is that the users of the connections must be extremely polite to each other. If you use a connection from the pool, you can't close the connection, because someone else will need it. You must close all your open statements and result sets, however. Also, you must either commit or rollback any pending database transactions.

Connection pools wrap a special class around a physical database connection. This special class behaves just like a database connection, but you can close the connection and you don't have to worry about any pending transactions. The wrapper class performs any necessary cleanup.

RowSets

A RowSet object is similar to a ResultSet; in fact, the RowSet interface extends ResultSet. The RowSet interface includes set methods in addition to the get methods (remember, the ResultSet interface uses update methods instead of set). The set methods use only indexed, not column names. The end result is that the RowSet is a Java bean with readable and writable properties. The idea is that you can return a RowSet to a client object where it can be manipulated and sent back to the server. Also, because a RowSet is a bean, you can use it from a GUI design tool.

There isn't anything in the JDBC API that specifies how you get a RowSet from a ResultSet. Each implementation of RowSet must come up with its own way to copy ResultSet data into a RowSet. Of course, because a RowSet is a ResultSet, you can use a RowSet anywhere you would use a ResultSet.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.