Tomcat连接池提示驱动错误
tomcat中的server.xml...
<Resource name="jdbc/webdb2" auth="Container"
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
<Context path="/webdemo3" docBase="webdemo3" debug="0">
<ResourceLink name="jdbc/webdb2" global="jdbc/webdb2" type="javax.sql.DataSource"/>
</Context>
项目里的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www. xmlns="http://xmlns. xsi:schemaLocation="http://xmlns. http://xmlns. id="WebApp_ID" version="3.1">
<display-name>webdemo3</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>viewDictory</servlet-name>
<servlet-class>webdemo3.chapter04.ViewDictionary</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>viewDictory</servlet-name>
<url-pattern>/servlet/ViewDictionary</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/webdb2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
项目中的ViewDictionary.java
package webdemo3.chapter04;
import
import
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
}
}
控制台错误信息:
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.(NioEndpoint.java:1521)
at org.apache.tomcat.(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 more
jar包好用,在项目和tomcat都有
还有个问题一直没有解决,不知道和这个有没有关:
conn=DriverManager.getConnection("jdbc:mysql://localhost:3360/newsmanager","root","sa123456");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/newsmanager?user=root&password=sa123456");
第一个不好使 第二个好使 为什么