0

I keep having errors says that Cannot Invoke java.sql.Connecton.preparestatement(String) because con is null

import java.sql.Connection;

import java.util.logging.Level; import java.util.logging.Logger;

public class myConnection {

 static Connection getConnection(){
     Connection con = null;
     try{
        Class.forName("com.mysql.jdbc.Driver");
        con = (Connection) java.sql.DriverManager.getConnection("jdbc:mysql://localhost/summative", "root", "");
        
    }
    catch(Exception ex){
        System.out.print(ex.getMessage());
        
    }
return con;
   }

}

And this is the error Photo of error

violet
  • 1
  • 1
  • 2

1 Answers1

0

If you are not using an ancient driver your class name is not valid anymore. You should use com.mysql.cj.jdbc.Driver instead of com.mysql.jdbc.Driver.

See also also the official documentation or here on stackoverflow.

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46