2008-08-11(http://spaceflysky-163-com.javaeye.com/blog/226650)
tomcat dbcp jndi 配置
使用tomcat6,mysql6
1)添加jar包
tomcat6中 TOMCAT_HOME/lib 下是公用jar包
dbcp需要3个jar包:Jakarta-Commons DBCP,Jakarta-Commons Collections,Jakarta-Commons Pool,
但是tomcat6已经用1个tomcat-dbcp.jar包含了这3个jar包,该包在 TOMCAT_HOME/lib 下,因此在tomcat下不需要再添加dbcp相关的3个包;
将mysql-connector-java-5.1.6-bin.jar 拷贝到 TOMCAT_HOME/lib 下;
2)添加数据源
在 TOMCAT_HOME/conf/context.xml 中 添加数据源:
Xml代码
- <!-- The contents of this file will be loaded for each web application -->
- <Context>
- <!-- Default set of monitored resources -->
- <WatchedResource>WEB-INF/web.xml</WatchedResource>
- <!-- Uncomment this to disable session persistence across Tomcat restarts -->
- <!--
- <Manager pathname="" />
- -->
- <!-- Uncomment this to enable Comet connection tacking (provides events
- on session expiration as well as webapp lifecycle) -->
- <!--
- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
- -->
- <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
- maxActive="100" maxIdle="30" maxWait="10000"
- username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/testit?autoReconnect=true"/>
- </Context>
3)在web.xml 中引用数据源
Xml代码
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <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 web-app_2_4.xsd"
- version="2.4">
- <display-name>JNDI Test</display-name>
- <description>A test for using of JNDI</description>
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/test</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
4)在jsp(或java)中使用数据源
Java代码
- <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <sql:query var="rs" dataSource="jdbc/test">
- select * from test
- </sql:query>
- <html>
- <head>
- <title>DB Test</title>
- </head>
- <body>
- <h2>Results</h2>
- <c:forEach var="row" items="${rs.rows}">
- id ${row.id}<br/>
- str ${row.str}<br/>
- </c:forEach>
- </body>
- </html>
5)tomcat的jndi实用类
Java代码
- package dbcp;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- /**
- * @author space
- * @date Aug 12, 2008 12:57:30 PM
- */
- public class TomcatDbcp {
- private static Context CTT;
- static {
- try {
- CTT = (Context) new InitialContext().lookup("java:comp/env");
- } catch (NamingException e) {
- e.printStackTrace();
- throw new RuntimeException("jndi 数据源加载失败!");
- }
- }
- /** 默认构造函数,没有创建数据源 */
- public TomcatDbcp() {
- }
- /** 参数是数据源名,创建数据源 */
- public TomcatDbcp(String resourceName) {
- setDs(resourceName);
- }
- private DataSource ds;
- public void setDs(String resourceName) {
- try {
- ds = (DataSource) CTT.lookup(resourceName);
- } catch (NamingException e) {
- e.printStackTrace();
- throw new RuntimeException("jndi 数据源创建失败!");
- }
- }
- private Connection conn;
- /** 其它类通过该方法调用 conn */
- public Connection getConn() {
- return conn;
- }
- /** 初始化conn */
- public void initConn() {
- try {
- conn = ds.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.println("获得连接失败!");
- }
- }
- /** 关闭conn */
- public void closeConn() {
- try {
- if (conn != null) {
- conn.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- TomcatDbcp td = new TomcatDbcp("jdbc/test");
- td.initConn();
- try {
- Statement stmt = td.getConn().createStatement();
- ResultSet rs = stmt.executeQuery("select * from test limit 1 ");
- rs.first();
- System.out.println("id:" + rs.getInt(1) + ", str:" + rs.getString(2));
- td.closeConn();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
6)jsp中调用tomcat dbcp实用类
Java代码
- <%@ page language="java" import="dbcp.TomcatDbcp ,java.sql.*" %>
- <html>
- <head>
- <title>DB Test</title>
- </head>
- <body>
- <h2>Results</h2>
- <hr/>
- <%
- TomcatDbcp td = new TomcatDbcp("jdbc/test");
- td.initConn();
- try {
- Statement stmt = td.getConn().createStatement();
- ResultSet rs = stmt.executeQuery("select * from test limit 1 ");
- rs.first();
- out.println("id:" + rs.getInt(1) + ", str:" + rs.getString(2));
- td.closeConn();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- %>
- </body>
- </html>