提问人:Akshay 提问时间:10/19/2023 更新时间:10/19/2023 访问量:39
我在servlet项目文件中将jdbc与mysql服务器连接时出现错误[duplicate]
I am getting an error while connecting jdbc with mysql server inside a servlet project file [duplicate]
问:
使用 eclipse,我试图在 servlet 程序中与 mysql 连接,但即使我为 mysql 连接器设置了类路径,我也一次又一次地收到相同的错误
this is the error i am getting
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/javasql
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at in.ineuron.controller.Connect.init(Connect.java:38)
at javax.servlet.GenericServlet.init(GenericServlet.java:143)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1106)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1063)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:747)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:116)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:670)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:928)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1794)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)
null
这是我试图在eclipse中运行的代码
package in.ineuron.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Connect
*/
@WebServlet(
urlPatterns = { "/reg" },
initParams = {
@WebInitParam(name = "url", value = "jdbc:mysql://localhost:3306/javasql"),
@WebInitParam(name = "user", value = "root"),
@WebInitParam(name = "password", value = "password")
})
public class Connect extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
ServletConfig config = getServletConfig();
String url = config.getInitParameter("url");
String user = config.getInitParameter("user");
String password = config.getInitParameter("password");
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(connection);
}
}
[在此处输入图像描述](https://i.stack.imgur.com/zHDGA.png) 在这里,我在eclipse的类路径中设置了jar文件
答: 暂无答案
评论
Class.forName("com.example.jdbc.Driver");