问题描述
- Tomcat连接池提示驱动错误
-
tomcat中的server.xml
...
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/webdb?characterEncoding=utf-8"
username="root"
password="sa123456"
maxActive="200"
maxIdle="50"
maxWait="3000"
/>
...tomcat中的webdemo3.xml
项目里的web.xml
<?xml version="1.0" encoding="UTF-8"?>webdemo3
index.html
index.htm
index.jsp
default.html
default.htm
default.jspviewDictory
webdemo3.chapter04.ViewDictionaryviewDictory
/servlet/ViewDictionaryDB Connection
jdbc/webdb2
javax.sql.DataSource
Container项目中的ViewDictionary.java
package webdemo3.chapter04;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/**
- Servlet implementation class ViewDictionary
*/
public class ViewDictionary extends HttpServlet {
private static final long serialVersionUID = 1L;/**
- @see HttpServlet#HttpServlet()
*/
public ViewDictionary() {
super();
// TODO Auto-generated constructor stub
}
/**
- @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
try{
javax.naming.Context ctx=new javax.naming.InitialContext();
javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/webdb2");
Connection conn=ds.getConnection();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){
e.printStackTrace();
out.println(e.getMessage());
}
}
/**
- @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
- @see HttpServlet#HttpServlet()
}
控制台错误信息:
java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2065)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1939)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
at webdemo3.chapter04.ViewDictionary.doGet(ViewDictionary.java:37)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2049)
... 26 morejar包好用,在项目和tomcat都有
还有个问题一直没有解决,不知道和这个有没有关:
conn=DriverManager.getConnection("jdbc:mysql://localhost:3360/newsmanager","root","sa123456");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/newsmanager?user=root&password=sa123456");
第一个不好使 第二个好使 为什么 - Servlet implementation class ViewDictionary
解决方案
查看getConnection的参数要求,你构造的字符串一个正确,一个不对。
解决方案二:
getConnection的参数,,,仔细看看
解决方案三:
getConnection的参数,
解决方案四:
建立连接时出错,建议你看一下,我的这篇博文,对你会有帮助的
http://blog.csdn.net/mo_fan_qing_wa/article/details/48027635
解决方案五:
driverClassName和url这两个配置参数都没有读取到。
解决方案六:
tomcat连接池提示错误