我在servlet项目文件中将jdbc与mysql服务器连接时出现错误[duplicate]

I am getting an error while connecting jdbc with mysql server inside a servlet project file [duplicate]

提问人:Akshay 提问时间:10/19/2023 更新时间:10/19/2023 访问量:39

问:

使用 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文件

java mysql eclipse servlet jdbc

评论

0赞 nitind 10/19/2023
如何设置类路径?
0赞 Logan Murphy 10/19/2023
自从我不得不这样做以来已经有一段时间了,但我记得随机必须做这样的事情:以便将驱动程序加载到程序中。希望这会有所帮助。Class.forName("com.example.jdbc.Driver");
0赞 Samuel Marchant 10/19/2023
连接变量!只有全局变量才能声明为 null !方法中的变量(例如 init)必须在声明它们的那一刻分配,它们不允许为 null!
0赞 nitind 10/19/2023
@SamuelMarchant 那不是真的。任何非基元都可以为 null - 实际上是没有赋值语句时对象的默认值。
0赞 Samuel Marchant 10/20/2023
毫无疑问,任何内容都可以被赋值为 null,但在赋值 null 后删除 null 只允许在全局变量上。此外,只有复杂类型使用 null。如果连接变量第一个为 null,则它必须是全局变量。奇怪的是,init() 的重点通常是实例化类中的全局变量。

答: 暂无答案