tomcat配置数据源通过JNDI访问mysql数据库
2010-04-08 14:31
459 查看
<p>4.tomcat配置数据源通过JNDI访问mysql数据库</p>
<p>Cannot create JDBC driver of class '' for connect URL 'null'错误</p>
1.添加mysql jdbc驱动至build path,并复制到tomcat目录/lib下
2.在tomcat目录/conf/server.xml的<host></host>之间添加
<Context path="/BBS" docBase="BBS" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/bbs" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="0." driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bbs?autoReconnect=true"/>
</Context>
3.在工程的web-inf/web.xml中的<web-app></web-app>之间添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/bbs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4.创建测试jsp
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>测试</title>
</head>
<body bgcolor="#DAF9FE">
<h1>mysql jndi test</h1>
<hr>
<%
DataSource ds = null;
try {
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("Initial Failed!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
if (ctx != null)
ds = (DataSource) ctx.lookup("jdbc/bbs");
if (ds == null)
throw new Exception("Look up DataSource Failed!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
%>
<%
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
while (rs.next()) {
%>
<%=rs.getInt(1)%>
<%=rs.getString(2)%>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
5.相关错误:
<1>Cannot create JDBC driver of class '' for connect URL 'null'
Tomcat先找到web.xml下的<resource-ref>,然后再找server.xml下面的<Resource>。如果没有找到<Resource name=”JDBC/TestDB”>,或者名字错了,则会报“Cannot create JDBC driver of class '' for connect URL 'null'”错误。
注意步骤2和3中jdbc/bbs处名称要相同,3处的url信息要正确
<2>Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
参加步骤1
<p>Cannot create JDBC driver of class '' for connect URL 'null'错误</p>
1.添加mysql jdbc驱动至build path,并复制到tomcat目录/lib下
2.在tomcat目录/conf/server.xml的<host></host>之间添加
<Context path="/BBS" docBase="BBS" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/bbs" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="0." driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bbs?autoReconnect=true"/>
</Context>
3.在工程的web-inf/web.xml中的<web-app></web-app>之间添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/bbs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4.创建测试jsp
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>测试</title>
</head>
<body bgcolor="#DAF9FE">
<h1>mysql jndi test</h1>
<hr>
<%
DataSource ds = null;
try {
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("Initial Failed!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
if (ctx != null)
ds = (DataSource) ctx.lookup("jdbc/bbs");
if (ds == null)
throw new Exception("Look up DataSource Failed!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
%>
<%
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from user");
while (rs.next()) {
%>
<%=rs.getInt(1)%>
<%=rs.getString(2)%>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
5.相关错误:
<1>Cannot create JDBC driver of class '' for connect URL 'null'
Tomcat先找到web.xml下的<resource-ref>,然后再找server.xml下面的<Resource>。如果没有找到<Resource name=”JDBC/TestDB”>,或者名字错了,则会报“Cannot create JDBC driver of class '' for connect URL 'null'”错误。
注意步骤2和3中jdbc/bbs处名称要相同,3处的url信息要正确
<2>Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
参加步骤1
相关文章推荐
- JNDI:如何在TOMCAT中配置数据源通过JNDI访问测试操作步骤
- JNDI:如何在TOMCAT中配置数据源通过JNDI访问测试操作步骤
- tomcat中通过JNDI数据源配置使用
- tomcat中通过JNDI数据源配置使用
- Tomcat中通过JNDI配置访问数据库
- Spring通过JNDI获取在Tomcat容器中配置的C3P0数据源
- Tomcat中通过JNDI配置访问数据库
- tomcat中通过JNDI数据源配置使用
- tomcat中通过JNDI数据源配置使用
- Tomcat 中通过 JNDI 配置访问数据库
- 使用JNDI配置Tomcat数据源[提高访问数据库效率]
- Spring通过JNDI获取在Tomcat容器中配置的C3P0数据源
- tomcat中通过JNDI数据源配置使用--时间沙漏
- Tomcat8.0配置JNDI数据源完整例子
- 在Tomcat配置JNDI数据源的三种方式
- Tomcat下通过配置提高网页访问速度[gzip压缩技术]
- JNDI Tomcat服务器 数据源配置
- Tomcat 6.0配置数据源(DBCP)和JNDI使用
- Tomcat中配置JNDI数据源
- Tomcat 6.0配置数据源(DBCP)和JNDI使用