学习JNDI创建数据库连接池出错

adtimo001 2012-03-06 11:50:23
出现这个错误
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
无论是tomcat的lib 还是项目本身的lib 都有mysql-connector-java-5.1.18-bin.jar
Servlet代码:

package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ViewDictionary extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Connection conn=getFromJNDI();
//Connection conn=getFromJDBC();
PreparedStatement pstmt = conn
.prepareStatement("SELECT * FROM t_dictionary ");
ResultSet rs = pstmt.executeQuery();
StringBuilder table = new StringBuilder();
table.append("<table border='1'>");
table.append("<tr><td>书名</td><td>价格</td></tr>");
while (rs.next()) {
table.append("<tr><td>" + rs.getString("english") + "</td><td>"
+ rs.getString("chinese") + "</td></tr>");
}
table.append("</table>");
out.println(table.toString());
pstmt.close();
} catch (Exception e) {
System.out.println(e);
}
}
public Connection getFromJNDI() throws NamingException, SQLException{
javax.naming.Context ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx
.lookup("java:/comp/env/jdbc/webdb");
Connection conn = ds.getConnection();
return conn;
}
public Connection getFromJDBC() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/webdb?characterEncoding=UTF-8";
Connection conn =DriverManager.getConnection(url,"root","123456");
return conn;
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ViewDictionary</servlet-name>
<servlet-class>Servlet.ViewDictionary</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewDictionary</servlet-name>
<url-pattern>/Servlet/ViewDictionary</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>jdbc/webdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/webdb" quth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/webdb?characterEncoding=UTF-8"
username="root" password="123456" maxActive="200" maxIdle="50"
maxWait="3000" />
</Context>

...全文
124 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
夏日彩虹 2012-03-08
  • 打赏
  • 举报
回复
在tomcat中配置数据源,试一试。
s478853630 2012-03-07
  • 打赏
  • 举报
回复
你换一个jar试试吧!
另外给你点建议:
现在大部分公司用com.mchange.v2.c3p0.ComboPooledDataSource或org.apache.commons.dbcp.BasicDataSource
你的那个jndi貌似过时了
adtimo001 2012-03-06
  • 打赏
  • 举报
回复
直接用JDBC获取连接是可以的 但是用JNDI就会提示
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
adtimo001 2012-03-06
  • 打赏
  • 举报
回复
菜鸟求指教

67,550

社区成员

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

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