要一份详细的tomcat6.0配置JDNI的实例代码 分只给完整解决方案的第一个人

qqqqqwwqqq 2008-12-24 05:28:11
今天弄了一天 也没弄明白 太郁闷了
希望给出解决方案的人说的明白一些 比如要不要配置环境变量 怎么配置
在server.xml中添加什么内容 在哪添加
在context.xml中添加什么内容 在哪添加
测试类怎么写 写什么 等等等等

等待完美的答案 近来几天那个连接池的帖子我也看了 前几楼的代码我试验了 出错误 还是来一份完整的吧

...全文
212 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiy08 2009-01-07
  • 打赏
  • 举报
回复
1. 第一步Context.xml中加 下面的红色的内容

example:


<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">


<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->

<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->

<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->

<!-- username and password: MySQL dB username and password for dB connections -->

<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->

<!-- url: The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

</Context>





2. 配置web.xml
在 WEB-INF/web.xml

<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">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>





3. 测试



<%@ 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/TestDB">
select id, foo, bar from testdata
</sql:query>

<html>
<head>
<title>DB Test</title>
</head>
<body>

<h2>Results</h2>

<c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar ${row.bar}<br/>
</c:forEach>

</body>
</html>

你试试希望能帮助你




feiy08 2009-01-07
  • 打赏
  • 举报
回复
tomcat的版本不同就有不同的配置方法,5.5以上的版本配置简单些,5.5下的版本复杂些
可以参照官方网站http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
chenbo124 2008-12-24
  • 打赏
  • 举报
回复
你开启tomcat6.0,
输入:http://localhost:8080/docs
查看下面内容:
8) JNDI Resources
9) JDBC DataSources
JDBC DataSources 有配置样例,你可以照着配置,自己要知道怎么查找资料.
sunboard 2008-12-24
  • 打赏
  • 举报
回复
1 在项目的WebRoot下建立META-INF文件夹

2 在META-INF下建立context.xml文件

3 在context.xml中加入如下样例代码

<Context path="/appName" docBase="appName"
debug="5" crossContext="true" reloadable="false"
cachingAllowed="true" cacheMaxSize="20480"
cacheTTL="10000">
<Resource name="jdbc/mysql" auth="Container" removeAbandoned="true"
removeAbandonedTimeout="60" logAbandoned="true"
type="javax.sql.DataSource" maxActive="50" maxIdle="10"
maxWait="10000" username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true" />
</Context>


4 在web.xml中加入

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5 以上配置文件,参数自己在重新设置设置,别忘了把驱动放到你的项目的classpath下

6 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="javax.naming.*" %>
<%@page import="javax.sql.DataSource" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>JNDITest Page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
This is my JSP page. <br>
JNDI配置测试开始 ...<br>
<%
try {
Context ctx = new InitialContext();

Context envContext = (Context)ctx.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/db"); //查找配置
//方法二:
//DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");

Connection conn = ds.getConnection();

conn.close();
out.println("JNDI数据源配置成功了");
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>


81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧