gpt4 book ai didi

java - 我已将 java mysql 连接器添加到我的项目中,但我的项目仍然无法使用它

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:43 26 4
gpt4 key购买 nike

这是我的类,它用于获取连接

// Database credentials
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/pornism";
static final String USER = "root";
static final String PASS = "1";
static final String HOST_NAME = "localhost";
static final String DB_NAME = "pornism";

// Connection and Statement of current session

public static Connection getConnection() throws ClassNotFoundException {
try {
String connectionURL = "jdbc:mysql://" + HOST_NAME + ":3306/" + DB_NAME;
Connection conn = DriverManager.getConnection(connectionURL,USER,PASS);
System.out.println("Successfull!!");
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

这就是我尝试连接时所显示的内容

    java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/crewup
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at data.Database.getConnection(Database.java:22)
at prototype.LoginMethod.doPost(LoginMethod.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

我已经将 mysql 连接器添加到我的项目中: Eclipse build path

谁能告诉我为什么它不起作用?我试图找到。

最佳答案

您必须设置Class.forName

尝试下面的代码片段。

public static Connection getConnection() throws ClassNotFoundException {
try {
Class.forName(JDBC_DRIVER); // Add this line.
String connectionURL = "jdbc:mysql://" + HOST_NAME + ":3306/" + DB_NAME;
Connection conn = DriverManager.getConnection(connectionURL,USER,PASS);
System.out.println("Successfull!!");
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

关于java - 我已将 java mysql 连接器添加到我的项目中,但我的项目仍然无法使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60584128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com