我知道這里有很多關于這個主題的問題。我嘗試了所有解決方案,但仍然無法連接到資料庫。請幫忙!我正在開發網路應用程式。NetBeans、Tomcat 9、MySQL。
錯誤:
SQLException: No suitable driver found for jdbc:mysql://localhost:3306user=webstudent&password=webstudent
SQLState: 08001
VendorError: 0
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "con" is null
我在專案中的檔案:
背景關系.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/LibraryWebApp"/>
DB.java:
package dao;
import java.sql.*;
//Database connection method
public class DB {
public static Connection getCon() throws ClassNotFoundException{
Connection con=null;
try {
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306"
"user=webstudent&password=webstudent");
Class.forName("com.mysql.jdbc.Driver");
} catch(SQLException ex){
System.out.println("SQLException: " ex.getMessage());
System.out.println("SQLState: " ex.getSQLState());
System.out.println("VendorError: " ex.getErrorCode());
}
return con;
}
}
查看專案中匯入的庫。
uj5u.com熱心網友回復:
SQLException: 找不到適合 jdbc:mysql://localhost:3306user=webstudent&password=webstudent 的驅動程式
您的連接字串不正確。您錯過了資料庫名稱。因此,更新以下字串中的資料庫名稱并使用它。
jdbc:mysql://localhost:3306/database_name?user=webstudent&password=webstudent
另外,在連接 stmt 之前放置以下陳述句
Class.forName("com.mysql.jdbc.Driver");
更新:
serverTimezone在連接字串中顯式添加例如:
jdbc:mysql://localhost:3306/database_name?user=webstudent&password=webstudent&serverTimezone=UTC
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442570.html
標籤:javascript mysql mysql-工作台 mysql 连接器 tomcat9
