1

I am relatively new to Java. Need to use Java with JDBC. I currently try to connect to the database local on my machine.
This is a part of training which is very important to me now. I am using Java 8:

enter image description here

The training and demonstration accordingly is done using Java 7 Driver provided with an exercise is: mysql-connector-java-5.1.21-bin.jar However I was recommended to use Java 8 driver, so I downloaded mysql-connector-java-8.0.29.jar (Platform independent) from https://dev.mysql.com/downloads/connector/j/

MySQL (local DB) looks like following: Host name: 127.0.0.1 User: root Port: 3306

enter image description here

enter image description here Copy JDBC Connection String to the clipboard brings the following: jdbc:mysql://127.0.0.1:3306/?user=root

enter image description here

Driver: mysql-connector-java-8.0.29.jar was copied into the project folder "libs" and added to the build path:

enter image description here

enter image description here

The following code I created (class main):

package com.lynda.javatraining.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
private static final String USERNAME = "root";
private static final String PASSWORD = "secret";

//private static final String CONN_STRING =
//"jdbc:mysql://localhost/explorecalifornia";

//private static final String CONN_STRING =
//"jdbc:mysql://127.0.0.1/explorecalifornia";

**//This is the string I use currently:** 
private static final String CONN_STRING =
"jdbc:mysql://127.0.0.1:3306/explorecalifornia";

//"jdbc:mysql://127.0.0.1:3306/?user=root/explorecalifornia";

  public static void main(String[] args) throws SQLException {

    Connection conn = null;
    try {
        conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
        System.out.println("Connected");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        System.err.println(e);
    }finally {
        if (conn != null) {
            conn.close();
        }
      } 
   }
}

When I run the code I am getting: java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/explorecalifornia

I tried different combinations for CONN_STRING (see all of them commented in the code above), still not working. I tried driver provided originally (mysql-connector-java-5.1.21-bin.jar) with no success (this was expected). Can somebody give an advice, please.

Vladislav
  • 121
  • 1
  • 9

1 Answers1

2

MySQL Connector/J 8 requires Java 8, while you're trying to run it on Java 7 (as shown by the "JRE System Library [JavaSE-1.7]" in your IDE screenshot). Either upgrade or switch your project to Java 8 (or higher), or downgrade to an older MySQL Connector/J version (e.g. MySQL Connector/J 5.1.49, which supports Java 5 or higher).

If you add Class.forName("com.mysql.cj.jdbc.Driver") to the start of your main method, you probably get a NoClassDefFoundError with cause UnsupportedClassVersionError.

For the general causes of error "java.sql.SQLException: No suitable driver found for jdbc:mysql://...", see also Connect Java to a MySQL database

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Thank you for the answer. I am new to Java. So, my apologies if the question is silly. How can I upgrade project to Java 8, please ? Do I need to import JRE System Library corresponding to Java 8 and delete the recent one (JavaSE-1.7)? Could you describe the steps please. – Vladislav May 19 '22 at 08:03
  • @Vladislav I haven't used Eclipse in a long time, so I can't readily give you the answer, but maybe these question help: [How to change JDK version for an Eclipse project](https://stackoverflow.com/questions/12588537/how-to-change-jdk-version-for-an-eclipse-project), [Setting JDK in Eclipse](https://stackoverflow.com/questions/13635563/setting-jdk-in-eclipse) – Mark Rotteveel May 19 '22 at 08:58
  • Got it working. So, connection string: private static final String CONN_STRING = "jdbc:mysql://127.0.0.1:3306/explorecalifornia"; In the Java Build Path window (right click on mysql-connector-java-8.0.29.jar - Build path/Configure Build Path), in tab Libraries. Noticed that Java 7 connector (mysql-connector-java-5.1.21-bin.jar) which I removed from Project Explorer was still shown along with Java 8 connector mysql-connector-java-8.0.29.jar. I highlighted Java 7 connector and removed it by clicking on the button Remove. Right click on the proj. hit Build Project and Run and all is working – Vladislav May 20 '22 at 07:13