关于tomcat配置JNDI错误:Cannot load JDBC driver class

Sunksubmarine 2008-05-23 09:19:54
我用的数据库是SQLserver
打的sp4补丁
一开始我老觉得是SQLserver驱动包有问题。
后来干脆在DAO中定义了两个方法getJndiConn()和getJDBCconn();
用getJDBCconn(){.....this.conn = DriverManager.getConnection(connectUrl,userName,userPass);......}就没有问题,这应该说明SQLserver驱动包没有问题。
但getJndiConn()就连不上,报错Cannot load JDBC driver class:"com.microsoft.jdbc.sqlserver.SQLServerDriver"-_-!


配置方面我是照着网上写的,如下:
tomcat 5.5.20\apache-tomcat-5.5.20\conf\context.xml:
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000"
username="sa"
password="123"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=lingxdb" />
</Context>

web.xml:
<resource-ref>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

BaseDao:
public void getJndiConn() throws SQLException{
Context ctx;
try {
ctx = new InitialContext();
Context envctx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envctx.lookup("jdbc/TestDB");
Connection conn=ds.getConnection();
Statement st=conn.createStatement();
String sql="select * from userdb";
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
System.out.println("getJndiConn():"+rs.getString(1));
System.out.println("getJndiConn():"+rs.getString(2));
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

错误信息:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.jdbc.sqlserver.SQLServerDriver'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:766)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at bean.BaseDao.getJndiConn(BaseDao.java:37)
.......

本人菜鸟,望高人指正。

...全文
1423 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaxhome 2010-01-30
  • 打赏
  • 举报
回复
偶遇到了同样的问题,但是按照上面的步骤还是没有解决,高手帮帮看看
Sunksubmarine 2008-05-23
  • 打赏
  • 举报
回复
晕 我真该死还是没考到/common/lib下 谢谢大家了
nanjg 2008-05-23
  • 打赏
  • 举报
回复
1把JDBC驱动部署在/WEB-INF/lib和Tomcat_Home/common/lib中。
2分步打印
Context jndiCntx = new InitialContext();

System.out.println("Looking up jdbc/DefaultDS");

DataSource ds = (javax.sql.DataSource)jndiCntx.lookup("java:comp/env/jdbc/DefaultDS");

System.out.println("Found. Connecting to jdbc/DefaultDS");

con = ds.getConnection();


3.Your question asking if sqlodbc.jar was in $CATALINA_HOME/common/lib directory got me started down the right path. Tomcat 6 did away with the $CATALINA_HOME/common/lib directory. The correct place to put libraries that need to be loaded before your application starts is now in the $CATALINA_HOME/lib directory. Not sure why they chnaged this, but this was why Tomcat could not load the driver.

这个问题挺普遍的 还不能确定是tom猫的bug......
希望你解决后 总结下

Sunksubmarine 2008-05-23
  • 打赏
  • 举报
回复
奥,我试试。为什么Sp4不行?
xiaolu_yun 2008-05-23
  • 打赏
  • 举报
回复
重新打补丁,打sp3
Sunksubmarine 2008-05-23
  • 打赏
  • 举报
回复
放了,绝对放了,不然getJDBCconn(){.....this.conn = DriverManager.getConnection(connectUrl,userName,userPass);......}也不会能用。
M_song 2008-05-23
  • 打赏
  • 举报
回复
把相关驱动包放到:Commom\lib下试试!
xiaolu_yun 2008-05-23
  • 打赏
  • 举报
回复
驱动包有没放到lib下?

67,542

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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