jndi mysql数据库_tomcat配置数据源通过JNDI访问mysql数据库 | 学步园

4.tomcat配置数据源通过JNDI访问mysql数据库

Cannot create JDBC driver of class '' for connect URL 'null'错误

1.添加mysql jdbc驱动至build path,并复制到tomcat目录/lib下

2.在tomcat目录/conf/server.xml的之间添加

maxActive="100" maxIdle="30" maxWait="10000"

username="root" password="0." driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/bbs?autoReconnect=true"/>

3.在工程的web-inf/web.xml中的之间添加

DB Connection

jdbc/bbs

javax.sql.DataSource

Container

4.创建测试jsp

测试

mysql jndi test


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.close();

stmt.close();

conn.close();

%>

5.相关错误:

<1>Cannot create JDBC driver of class '' for connect URL 'null'

Tomcat先找到web.xml下的,然后再找server.xml下面的。如果没有找到,或者名字错了,则会报“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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值