Search code examples
javascriptmysqlmysql-workbenchmysql-connectortomcat9

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


I know here are lot of questions here on this topic. I tried all solutions but still can't get connection to data base. Please help! I am working on web app. NetBeans, Tomcat 9, MySQL.

Error:

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

My files in project:

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.


Solution

  • SQLException: No suitable driver found for jdbc:mysql://localhost:3306user=webstudent&password=webstudent

    Your connection string is incorrect. You've missed the database name. So, update the database name in the following string and use it.

    jdbc:mysql://localhost:3306/database_name?user=webstudent&password=webstudent
    

    Also, place the following statement prior to connection stmt

    Class.forName("com.mysql.jdbc.Driver"); 
    

    Updated:

    Add the serverTimezone explicitly in the connection string Eg:

    jdbc:mysql://localhost:3306/database_name?user=webstudent&password=webstudent&serverTimezone=UTC