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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access