The DataSource Interface and JDBC Drivers
Before we
get
started with the examples, let’s look at how to
identify the database you want to access. The JSTL actions can find
this information in many different ways, to make the simple scenario
simple and the more complex ones possible. In all cases, though, they
get access to the database through an instance of a JDBC interface
named javax.sql.DataSource
.
The
DataSource
interface is part of the Java 2 Standard Edition (J2SE) 1.4, and for
prior versions of the J2SE, it’s available in the
JDBC 2.0 Optional Package. To access a database, a connection to the
database must first be established. Opening a database connection is
very time-consuming. A nice thing with a
DataSource
is that it can represent something
called a
connection
pool. Connection pools are described in more
detail in Chapter 24, but it’s
exactly what it sounds like: a pool of database connections that can
be shared by multiple clients. With a connection pool, a connection
to the database is opened once and stays open until the application
is shut down. When a database action needs a connection, it gets it
from the pool through the DataSource
object and
uses it to execute one or more SQL statements. When the action closes
the connection, the connection is returned to the pool where it can
be picked up by the next action that needs it.
In addition to the DataSource
, the JDBC API
contains other classes and interfaces that allow a Java application to process SQL statements ...
Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.