Tomcat6配置数据源:Cannot create JDBC driver of class '' for connect URL 'null'解决方案
2012-03-31 18:55
531 查看
在使用Eclipse+Lomboz+Tomcat6开发 Java Web的时候,如果要访问如mysql 数据库,可以使用tomcat自带的数据源缓冲池。方法如下:
1、在使用mysql数据库的时候,如同使用jdbc连接数据库一样,mysql的驱动jar包是一定需要的,可以放在tomcat安装目录的lib目录下,必要的话,还要想设置java的jdk一样,设置环境变量,是CATALINA =tomcat的安装目录。将mysql的jar包放在%CATALINA_HOME%/lib的目录下;
2、配置tomcat/conf/server.xml文件,
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase"
auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/mysqls"
type="javax.sql.DataSource"
password="zhou"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="30"
maxWait="10000"
username="root"
url="jdbc:mysql://localhost:3306/dbdemo"
maxActive="100"/>
</GlobalNamingResources>
<ContextdocBase="guestbook" path="/guestbook5"reloadable="true"source="org.eclipse.jst.jee.server:guestbook">
<ResourceLink
name="jdbc/mysqls"
type="javax.sql.DataSource"
global="jdbc/mysqls"/>
</Context>
其中红色是新加入的。
并且最好是将该server.xml文件拷贝一份放在\conf\Catalina\localhost目录下,很关键,我就是因为这一步没弄,耽误了很多时间。
3、在当前项目中,找到web.xml文件,添加
<resource-ref>
<res-ref-name>jdbc/mysqls</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4、附上检验代码:
1
<%@ page language="java"import="java.util.*"
pageEncoding="UTF-8"%>
2
<%@
pageimport="java.sql.*,javax.sql.*,javax.naming.*"%>
3
4
<!DOCTYPE HTML PUBLIC"-//W3C//DTD
HTML 4.01 Transitional//EN">
5
<html>
6
<head>
7
<title>My
JSP'index.jsp' starting page</title>
8
</head>
9
10
<body>
11
<%
12
Context initCtx=null;
13
final String
JNDINAME=
"java:comp/env/jdbc/MySQL";//java:comp/env/是固定的,jdbc/MySQL则是我们配置的JNDI名称
14
Connection conn=null;
15
try
{
16
initCtx=new
InitialContext();
17
DataSource ds=
(DataSource) initCtx.lookup(JNDINAME);
18
conn=
ds.getConnection();
19
out.println("数据连接为:"+
conn);
20
}
catch (NamingException e)
{
21
e.printStackTrace();
22
}
catch (SQLException e)
{
23
e.printStackTrace();
24
}
finally
{
25
if
(conn !=
null)
{
26
try
{
27
conn.close();
28
}
catch (SQLException e)
{
29
e.printStackTrace();
30
}
31
}
32
}
33
%>
34
<br>
35
</body>
36
</html>
37
若成功,则输出:
数据连接为:jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver
1、在使用mysql数据库的时候,如同使用jdbc连接数据库一样,mysql的驱动jar包是一定需要的,可以放在tomcat安装目录的lib目录下,必要的话,还要想设置java的jdk一样,设置环境变量,是CATALINA =tomcat的安装目录。将mysql的jar包放在%CATALINA_HOME%/lib的目录下;
2、配置tomcat/conf/server.xml文件,
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase"
auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/mysqls"
type="javax.sql.DataSource"
password="zhou"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="30"
maxWait="10000"
username="root"
url="jdbc:mysql://localhost:3306/dbdemo"
maxActive="100"/>
</GlobalNamingResources>
<ContextdocBase="guestbook" path="/guestbook5"reloadable="true"source="org.eclipse.jst.jee.server:guestbook">
<ResourceLink
name="jdbc/mysqls"
type="javax.sql.DataSource"
global="jdbc/mysqls"/>
</Context>
其中红色是新加入的。
并且最好是将该server.xml文件拷贝一份放在\conf\Catalina\localhost目录下,很关键,我就是因为这一步没弄,耽误了很多时间。
3、在当前项目中,找到web.xml文件,添加
<resource-ref>
<res-ref-name>jdbc/mysqls</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4、附上检验代码:
1
<%@ page language="java"import="java.util.*"
pageEncoding="UTF-8"%>
2
<%@
pageimport="java.sql.*,javax.sql.*,javax.naming.*"%>
3
4
<!DOCTYPE HTML PUBLIC"-//W3C//DTD
HTML 4.01 Transitional//EN">
5
<html>
6
<head>
7
<title>My
JSP'index.jsp' starting page</title>
8
</head>
9
10
<body>
11
<%
12
Context initCtx=null;
13
final String
JNDINAME=
"java:comp/env/jdbc/MySQL";//java:comp/env/是固定的,jdbc/MySQL则是我们配置的JNDI名称
14
Connection conn=null;
15
try
{
16
initCtx=new
InitialContext();
17
DataSource ds=
(DataSource) initCtx.lookup(JNDINAME);
18
conn=
ds.getConnection();
19
out.println("数据连接为:"+
conn);
20
}
catch (NamingException e)
{
21
e.printStackTrace();
22
}
catch (SQLException e)
{
23
e.printStackTrace();
24
}
finally
{
25
if
(conn !=
null)
{
26
try
{
27
conn.close();
28
}
catch (SQLException e)
{
29
e.printStackTrace();
30
}
31
}
32
}
33
%>
34
<br>
35
</body>
36
</html>
37
若成功,则输出:
数据连接为:jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver
相关文章推荐
- tomcat 5.5 Cannot create JDBC driver of class '' for connect URL 'null' 配置数据源错误解决办法
- IBatis配置tomcat的JNDI问题解决Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null' 解决方案
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null' 及can not load org.gjt.mm.mysql.Driver)
- Cannot create JDBC driver of class '' for connect URL 'null'
- SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 解决办法
- Cannot create JDBC driver of class '' for connect URL 'null' 错误
- JNDI数据源局部配置(解决Cannot create JDBC driver of class '' for connect URL 'null')
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- 如何配置tomcat数据源以及Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null' 的解决办法
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- Cannot create JDBC driver of class '' for connect URL 'null'
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- Cannot create JDBC driver of class '' for connect URL 'null'
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT 连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- tomcat7配置数据源异常---Cannot create JDBC driver of class '' for connect URL 'null'