关于tomcat配置JNDI错误:Cannot load JDBC driver class
我用的数据库是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)
.......
本人菜鸟,望高人指正。