【发布时间】:2019-08-20 06:27:38
【问题描述】:
每当我在 Eclipse 中运行我的 servlet 时,它都会显示:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
我已经正确连接了 jar 文件并在构建路径配置中设置了路径。还有什么我可以做的吗?
卸载并重新安装 MySQL?
{
[...]
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/example","root","root");
String qr="select * from details where name=? and pwd=?";
PreparedStatement ps=con.prepareStatement(qr);
ps.setString(1, name);
ps.setString(2, pwd);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
out.println("home");
}
else
{
out.println("Invalid name and password");
}
con.close();
} catch(Exception e) {
out.println(e);
}
我希望输出“home”或“invalid name and password”
【问题讨论】:
-
带有 JDBC 驱动程序的 JAR 文件必须在应用程序的
WEB-INF/lib文件夹中,或者在 Tomcat 的lib文件夹中。因此,将 JDBC 驱动程序 JAR 文件放到 web-application Eclipse 项目的WebContent/WEB-INF/lib文件夹中,然后(重新)部署您的 web 应用程序。
标签: java mysql eclipse servlets