servlet连接jdbc显示500,jar包也导了就是不知道为什么
Java代码
import javax.servlet.*;
import java.io.IOException;
import java.sql.*;
public class jdbc implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
try {
Driver d = new com.mysql.cj.jdbc.Driver();
DriverManager.registerDriver(d);
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/servlet","root","xionghui");
Statement sta = con.createStatement();
ResultSet res ;
res=sta.executeQuery("select * from class");
if( res!=null){System.out.println("i");
res.next();
String s=res.getString("name");
System.out.println(s);}
else{
res.close();
}
} catch (Exception e) {
}
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
错误信息
HTTP Status 500 – Internal Server Error
Type 异常报告
消息 Servlet execution threw an exception
描述 服务器遇到一个意外的情况,阻止它完成请求。
Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NoClassDefFoundError: com/mysql/cj/jdbc/Driver
jdbc.service(jdbc.java:23)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1363)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1186)
jdbc.service(jdbc.java:23)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note 主要问题的全部 stack 信息可以在 server logs 里查看