Home TOC |
![]() ![]() ![]() |
Accessing Databases from Web Applications
Data that is shared between Web components and persistent between invocations of a Web application is usually maintained by a database. Web applications use the JDBC 2.0 API to access relational databases. For information on this API, see
http://java.sun.com/docs/books/tutorial/jdbc
The Examples
The examples discussed in the chapters Java
Servlet Technology (page 393), JavaServer Pages
Technology (page 429), Custom Tags in JSP
Pages (page 461), and JavaServer Pages
Standard Tag Library (page 497) require a database. For this release we have tested the examples with the Pointbase database and we provide an
ant
build file to create the database tables and populate the database. The remainder of this section describes how to install and start the Pointbase database server, set up the example tables, configure the Web application to use the database, and configure Tomcat to recognize the database.Downloading and Starting the Database Server
You can download a copy of the Pointbase database from:
http://www.pointbase.comAfter you have downloaded and installed the Pointbase database, you will need to do the following:
- Set the
PB_HOME
environment variable to point to your Pointbase install directory.- Copy <
PB_HOME
>/client/lib/pbclient41ev.jar
to <JWSDP_HOME>
/common/lib
to make the Pointbase client library available to the example applications.- In a terminal window, go to <
PB_HOME
>/server
.- Start the Pointbase server by typing
Server
.Populating the Database
- In a terminal window, set the environment variable
PB_HOME
to point to your Pointbase installation to make the Pointbase libraries available to theant
task that populates the database.- Go to
<
JWSDP_HOME
>/docs/tutorial/examples/web
.- Execute
ant
. At the end of the processing, you should see the following output:[java] ID [java] ---------- [java] 201 [java] 202 [java] 203 [java] 204 [java] 205 [java] 206 [java] 207 [java] [java] 7 Rows Selected. [java] [java] SQL> [java] [java] COMMIT; [java] OKConfiguring the Web Application to Use the Database
In order to access a database from a Web application you must declare resource reference in the application's Web application deployment descriptor (see References to Environment Entries, Resource Environment Entries, or Resources). The resource reference declares the name and type of resource and the type of authentication used when the resource is accessed.
Configuring the Server to Recognize the Database
Since the resource reference declared in the Web application deployment descriptor uses a JNDI name to refer to the database, you must connect the name to an actual database by providing a resource factory in the Tomcat's configuration. Here is the resource factory used by the application discussed in all the Web technology chapters:
<Resource name="jdbc/BookDB" reloadable="true" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/BookDB"> <parameter> <name>user</name> <value>public</value> </parameter> <parameter> <name>password</name> <value>public</value> </parameter> <parameter> <name>driverClassName</name> <value>com.pointbase.jdbc.jdbcUniversalDriver</value> </parameter> <parameter> <name>driverName</name> <value>jdbc:pointbase: server://localhost/sample</value> </parameter> </ResourceParams>Since the resource factory is a subentry of the
Context
entry described in Running Web Applications, you add this entry to Tomcat's configuration in the same ways that you can add theContext
entry.
Home TOC |
![]() ![]() ![]() |