Creating a JNDI DBCP Connection Pool

Copy the source code below into a file called test.jsp in your application's main folder.

Your application directory structure will look something like this:

Source Code (Copy and Paste): <%-- /* * @(#)test.jsp (Metawerx customer example classes) * * Copyright (c) 1998-2007 Metawerx. All Rights Reserved. * * This software is the confidential and proprietary information of Metawerx * ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the * license agreement you entered into with Metawerx. * * METAWERX MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE OR THE SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METAWERX SHALL NOT BE LIABLE * FOR ANY DAMAGES SUFFERED BY ANY PARTY AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE AND DOCUMENTATION OR ITS DERIVATIVES * * * Synopsis * ===================================================================================== * This code demonstrates the use of a JNDI DBCP Connection Pool entry * to run and output data from an SQL query using JSTL. * * Requirements * ===================================================================================== * This code requires the jstl.jar and standard.jar files to use JSTL. * Using JNDI, the JDBC driver must be available in Tomcat's CLASSPATH. * Making it part of the application's CLASSPATH is not sufficient. * * Complete file structure for Tomcat for this sample * ===================================================================================== JAKARTA-WEBAPPS JAKARTA-WEBAPPS/JNDIApp JAKARTA-WEBAPPS/JNDIApp/test.jsp JAKARTA-WEBAPPS/JNDIApp/WEB-INF JAKARTA-WEBAPPS/JNDIApp/WEB-INF/web.xml JAKARTA-WEBAPPS/JNDIApp/WEB-INF/lib JAKARTA-WEBAPPS/JNDIApp/WEB-INF/lib/jstl.jar JAKARTA-WEBAPPS/JNDIApp/WEB-INF/lib/standard.jar JAKARTA-WEBAPPS/JNDIApp/META-INF JAKARTA-WEBAPPS/JNDIApp/META-INF/context.xml * * Complete, minimal web.xml file for this sample * ===================================================================================== <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <!-- Note there are no resource-reference tags required here, because they come from context.xml automatically. --> <!-- This is a minimal web.xml - it doesn't even have a description for the application. Feel free to add whatever else you need. --> </web-app> * History * ===================================================================================== * 2006/04/19 9:22 Neale Rudd * Added this example * */ --%> <%@ page pageEncoding="UTF-8" contentType="text/html" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <html> <head> <title>JNDI DBCP Test Page</title> </head> <body> <h1>JNDI DBCP Test Page</h1> <br/>Executing the query ... <br/> <%-- Note: Enter a query that is valid for your database here --%> <sql:query var="result" dataSource="jdbc/mydatabase"> SELECT * FROM products </sql:query> <table border="1"> <%-- Output column names on a header row --%> <tr> <c:forEach var="columnName" items="${result.columnNames}"> <th><c:out value="${columnName}"/></th> </c:forEach> </tr> <%-- Output each row of data --%> <c:forEach var="row" items="${result.rowsByIndex}"> <tr> <%-- Output each column of data --%> <c:forEach var="col" items="${row}"> <td><c:out value="${col}"/></td> </c:forEach> </tr> </c:forEach> </table> </body> </html>