0

I have added the JDBC connector to my class path but I still receive "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" error message. Is there a fault with my code I am not seeing?

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {

    public static Connection conn = null;

    public static Connection ConnectDB () {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = 
            DriverManager.getConnection("jdbc:mysql://localhost/destore", 
            "root", "1234");
            return conn;
        }catch(Exception e) {
            javax.swing.JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }

}

1 Answers1

0

Add the port number

Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/"+dbname,username,pwd);
Thejas
  • 379
  • 5
  • 24
  • Thanks for the reply. I tried that as well as commenting out the `Class.forName` part and receieved this error message: "java.sql.SQLException: No suitable driver was found for jdbc:mysql://localhost/destore:3306" – Paolok59717 Nov 21 '17 at 20:40
  • Dont comment out anything . Just add :3306 next to localhost.. Refer my code – Thejas Nov 21 '17 at 20:42
  • I created the database through XAMMP phpMyAdmin and named it destore so the class path is correct so do not understand the errors – Paolok59717 Nov 21 '17 at 20:42
  • Replace, DriverManager.getConnection("jdbc:mysql://localhost/destore", "root", "1234"); with DriverManager.getConnection("jdbc:mysql://localhost:3306/destore", "root", "1234"); – Thejas Nov 21 '17 at 20:44
  • Yeah I tried that, it doesn't make it past `Class.forName` and shows the error I mentioned in my first post. I have added the port number as you said. When I comment out `Class.forName` i get the "no suitable driver found error" – Paolok59717 Nov 21 '17 at 20:47
  • Okay in that case there is something wrong in you class path, your code looks fine – Thejas Nov 21 '17 at 20:54
  • thanks so much for your replies, I manage to sort it. I removed the driver, set up a folder within the project named lib and pasted the JDBC driver in it. I then configured the build path and added the .jar to my library and it worked – Paolok59717 Nov 21 '17 at 21:14