1
votes

I want to use JDBC i added Mysql driver to classpath as below :

^enter image description here

But even that i get this error :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:191) at dao.JdbcTest1.main(JdbcTest1.java:23)

This is my code :

public static void main(String[] args) {
    String url = "jdbc:mysql://localhost:3306/bd_italiano";
    String utilisateur = "root";
    String motDePasse = "";

    Connection connexion = null;
    try{
        Class.forName("com.mysql.jdbc.Driver ");
        connexion = DriverManager.getConnection( url, utilisateur, motDePasse );
        DatabaseMetaData dbMetaData = connexion.getMetaData();

        System.out.println("Type de la base de données : " + dbMetaData.getDatabaseProductName());
        System.out.println("Veresion de la base de données : " + dbMetaData.getDatabaseMajorVersion());
        System.out.println("Nom du driver : " + dbMetaData.getDriverName());
        System.out.println("Nom du user de la base de données : " + dbMetaData.getUserName());

    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        if(connexion!=null)
            try {
                connexion.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}

Pleas help me

2
Using Class.forName to load a JDBC driver is no longer necessary (assuming you use a JDBC 4 (or higher) compliant driver. Also note that MySQL Connector/J 5.1.18 is pretty old. - Mark Rotteveel

2 Answers

2
votes

You have an extra space. It should be Class.forName("com.mysql.jdbc.Driver") not Class.forName("com.mysql.jdbc.Driver ")

1
votes

Remove the trailing space from

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