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();
    }
}

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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 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: i.sstatic.net/zjqPr.png
Also, if you're using Maven or Gradle, try to build the project on the command line, like: mvn clean install.
0

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

2 Comments

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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.