0

enter image description here

I have this Exception but the Jar file are in the referenced libraries.

I dont't know where is the problem. The code is alright and I have added all in the build path. String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";

final String DBMS = "jdbc:mysql";

final String SERVER="localhost";

final String DATABASE = "mapDB";

final int PORT=3306; 

final String USER_ID = "MapUser";

final String PASSWORD = "map";

Connection conn;//gestisce una connessione 

private void initConnection() throws DatabaseConnectionException {
    try {
        Class.forName(DRIVER_CLASS_NAME);/
    }catch(ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {

        conn=(Connection)DriverManager.getConnection(DBMS + "://" + SERVER + ":" + PORT + "/" + DATABASE,USER_ID,PASSWORD);
    }catch(SQLException ex) {
        throw new DatabaseConnectionException();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

3 Answers3

1

Your image shows you have mysql-connector-java-5.1.7-bin.jar on your classpath.

When looking for that file in the Maven Repository, there are many 5.1.x versions, but not 5.1.7.

It would seem that 5.1.7 is flawed and have been retracted.

Try using another 5.1.x version, e.g. the latest, which currently is 5.1.46.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

It appears that there are different versions of MySQL JDBC connectors, as the second print shows.

Maybe the existence of 2 references to the same lib is causing the ClassNotFoundException, or your project's build is not up to date.

I suggest you to keep only one version of the connector on the references of your project, clear and build the project again.

Matheus Canon
  • 115
  • 1
  • 9
  • Yes. But I realized I had another project that has the same String DRIVER_CLASS_NAME and do the same thing. Is this the problem? – Sante Altamura May 31 '18 at 21:26
  • I don't think so. Maybe changing the reference order on the classpath can solve this. Try putting the connector reference up on the classpath, like in this print: http://i.stack.imgur.com/zjqPr.png – Matheus Canon May 31 '18 at 21:32
  • Also, if you're using Maven or Gradle, try to build the project on the command line, like: `mvn clean install`. – Matheus Canon May 31 '18 at 21:35
0

Have you tried removing mysql-connector-java-5.1.7-bin.jar, just keep mysql-connector-java-8.0.11.jar

  • Yes. But I realized I had another project that has the same String DRIVER_CLASS_NAME and do the same thing. Is this the problem? I must remove this project from eclipse? There is another solution? – Sante Altamura May 31 '18 at 21:16
  • If you keep both version then you have to check you import statements: import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.Connection; and import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; since in version 8.0.11 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. T – Anshul Khandelwal May 31 '18 at 21:40