今天用Idea写JavaWeb时,出现了java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at lee.GetConnListener.contextInitialized(GetConnListener.java:17) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4685) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5146) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705) at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1728) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456) at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357) at sun.rmi.transport.Transport$1.run(Transport.java:200) at sun.rmi.transport.Transport$1.run(Transport.java:197) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:196) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
Idea没找到数据库驱动
我先想到的是没有配置好mysql-connector-java-5.0.8-bin.jar,所以我先配置了Project Structure->Moudles->Dependencies->JARs or directories找到mysql-connector-java-5.0.8-bin.jar 点击ok,然后勾上mysql-connector-java-5.0.8-bin.jar就可以了
这样配置后我发现写个普通的java测试类测试一下
可以连接上数据库`package lee;
import java.sql.Connection;
import java.sql.DriverManager;
public class Test {
public void test() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”, “root”, “root”);
System.out.println(“连接数据库成功!”);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[]args){
new Test().test();
}
}
`
但是写一个ServletContextListener的实现类时就出现开始时的错误。
代码如下
import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import java.sql.*;
@WebListener
public class GetConnListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
String driver = application.getInitParameter("driver");
String url = application.getInitParameter("url");
String user = application.getInitParameter("user");
String pass = application.getInitParameter("pass");
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,pass);
application.setAttribute("conn",conn);
} catch (Exception e) {
System.out.println("获取数据库连接时异常"+e.getMessage());
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
Connection conn = (Connection) application.getAttribute("conn");
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
我又怀疑是web.xml中的com.mysql.jdbc.Driver没写对检查后发现没错,
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>pass</param-name>
<param-value>root</param-value>
</context-param>
<listener>
<listener-class>lee.GetConnListener</listener-class>
</listener>
</web-app>
然后我又debug ServletContextListener实例类
发现到第17行之前都没有问题,然后Step Over(F8)后直接跳到了第21行,发现什么问题都没有,这时就怀疑人生了。。。然后,在网上找到只要把mysql-connector-java-5.1.7-bin.jar复制到tomcat的lib目录下面!就可以了 然后启动tomecat运行就没问题了
补充:
页面效果由jsp呈现。
代码:
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %><%--
Created by IntelliJ IDEA.
User: Admin
Date: 2020/2/6
Time: 16:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>GetConn</h1>
<%
Connection conn = (Connection) application.getAttribute("conn");
if(conn!=null){
Statement stmt = conn.createStatement();
stmt.executeQuery("select * from student");
ResultSet resultSet = stmt.getResultSet();
%>
<table bgcolor="blue" border="1" width="300">
<% while (resultSet.next()){
%>
<tr>
<td><%=resultSet.getString(2)%></td>
</tr>
<%
}
}
%>
<tr>
<td>2</td>
</tr>
</table>
</body>
</html>