SQLException: No suitable driver found for jdbc:mysql. SQLState: 08001. VendorError: 0

SQLException: No suitable driver found for jdbc:mysql. SQLState: 08001. VendorError: 0

我知道这里有很多关于这个主题的问题。我尝试了所有解决方案,但仍然无法连接到数据库。请帮忙! 我正在开发网络应用程序。 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

我在项目中的文件:

Context.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;
}     
}

See imported libraries in the project.

SQLException: No suitable driver found for 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