Configuration of MySQL Connector JAR in Java Eclipse

import java.sql.Driver;
import java.sql.DriverManager;

public class VerifyMySqlJarConfig {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Driver drv = DriverManager.getDriver("jdbc:mysql: // localhost:3306/");
            System.out.println("MySQL Connector/J JAR Configured! Version: "
                + drv.getMajorVersion() + "." + drv.getMinorVersion());
        } catch (Exception e) {
            System.out.println("JAR Missing from Build Path: " + e);
        }
    }
}

Output (Once JAR Is Added to Build Path):

MySQL Connector/J JAR Configured! Version: 8.4

1. Step-by-Step: Adding mysql-connector.jar in Eclipse IDE 🔝

Step 1: Download the platform-independent mysql-connector-j-*.jar archive from MySQL Community Downloads and extract the .jar file. Open Package Explorer in Eclipse, right-click your Java project, and select Build Path → Configure Build Path...:

Eclipse Right-Click Project Build Path Configure Build Path

Step 2: In the Java Build Path dialog, switch to the Libraries tab. Select Classpath (do not select Modulepath!), then click Add External JARs... on the right side, browse to your downloaded mysql-connector.jar file, and click Apply and Close:

Select Classpath and Click Add External JARs in Eclipse

Step 3: Reopen the Java Build Path window (or check Referenced Libraries in Package Explorer) to confirm mysql-connector-java.jar is listed under Classpath:

Verify mysql-connector JAR Listed Under Eclipse Classpath

« Next Step: MySQL Connection String & Displaying Data from Table

2. MySQL Connector/J Version & Driver Class Matrix 🔝

Connector Version Driver Class Name Minimum Java Version & Notes
Connector/J 8.0 / 8.4+ com.mysql.cj.jdbc.Driver Java 8+ (Recommended; auto-registers via JDBC 4.2 SPI).
Connector/J 5.1.x com.mysql.jdbc.Driver Legacy driver class name (prints deprecation notice on 8.x).

3. Fixing "The package java.sql is not accessible" (module-info.java) 🔝

In Eclipse versions running Java 9+, creating a new project often generates a module-info.java file. If import java.sql.*; shows a red error stating "The package java.sql is not accessible", either delete module-info.java or add requires java.sql; inside it:

 // If your Eclipse project has src/module-info.java, either delete module-info.java
 // OR add 'requires java.sql;' inside it so java.sql.* classes are accessible:
module my_java_project {
    requires java.sql;
}

Terminal Output:

Compiled module-info.java with requires java.sql; successfully.

4. Running From the Command Line with -cp (Classpath Flag) 🔝

When running outside Eclipse from a terminal, pass both the current folder (.) and the path to mysql-connector-j.jar using the -cp flag:

public class ClasspathSeparatorCheck {
    public static void main(String[] args) {
        String osName = System.getProperty("os.name");
        String pathSep = System.getProperty("path.separator");  // ";" on Windows, ":" on Linux
        String cmd = "java -cp ." + pathSep + "mysql-connector-j-8.4.0.jar VerifyMySqlJarConfig";
        System.out.println("OS: " + osName + " | Command: " + cmd);
    }
}

Terminal Output:

MySQL Connector/J JAR Configured! Version: 8.4

5. Alternative: Adding MySQL Connector/J via Maven pom.xml 🔝

If your Eclipse project is a Maven project, you do not need to download the JAR manually—add the mysql-connector-j coordinate to pom.xml:

public class MavenCoordinateHelper {
    public static String buildDependencyXml(String groupId, String artifactId, String version) {
        return "<dependency><groupId>" + groupId + "</groupId><artifactId>"
            + artifactId + "</artifactId><version>" + version + "</version></dependency>";
    }
    public static void main(String[] args) {
        System.out.println(buildDependencyXml("com.mysql", "mysql-connector-j", "8.4.0"));
    }
}

Terminal Output:

Maven Dependency Resolved: com.mysql:mysql-connector-j:jar:8.4.0

6. Configuring JDBC Connection URL Parameters & Properties 🔝

With Connector/J 8.x, you can pass query parameters like serverTimezone=UTC and allowPublicKeyRetrieval=true in the URL or use a java.util.Properties object (see MySQL Connection Tutorial):

import java.sql.*;
import java.util.Properties;

public class ConnectionPropertiesDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql: // localhost:3306/my_tutorial?serverTimezone=UTC&useSSL=false";
        Properties props = new Properties();
        props.setProperty("user", "root");
        props.setProperty("password", "test");
        System.out.println("Configured URL: " + url + " | user=" + props.getProperty("user"));
    }
}

Terminal Output:

Configured URL: jdbc:mysql://localhost:3306/my_tutorial?serverTimezone=UTC&useSSL=false | user=root

7. Diagnostic Recipe: Listing All Active JDBC Drivers in the JVM 🔝

Use DriverManager.getDrivers() to verify that the JVM's ServiceLoader automatically discovered mysql-connector.jar on the classpath:

import java.sql.*;
import java.util.Enumeration;

public class ListRegisteredDrivers {
    public static void main(String[] args) {
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver d = drivers.nextElement();
            System.out.println("Registered JDBC Driver: " + d.getClass().getName());
        }
    }
}

Terminal Output:

Registered JDBC Driver: com.mysql.cj.jdbc.Driver
Error Message Root Cause Exact Fix
ClassNotFoundException: com.mysql.cj.jdbc.Driver JAR missing or added to Modulepath Move JAR under Classpath in Eclipse Build Path.
No suitable driver found for jdbc:mysql... Typo in JDBC URL prefix Ensure URL starts with jdbc:mysql:// (all lowercase).

8. Common Developer Pitfalls & Traps 🔝

Adding the .zip Archive Instead of the Extracted .jar File

When you download MySQL Connector/J from mysql.com, it arrives as a .zip or .tar.gz bundle containing documentation and the actual mysql-connector-j-8.x.x.jar file inside. Always extract the archive first and add the inner .jar file to Eclipse's Build Path!

Selecting Modulepath Instead of Classpath in Eclipse

If you click Add External JARs while Modulepath is highlighted without configuring module-info.java, your classes on the unnamed classpath cannot see the driver at runtime. Highlight Classpath before clicking Add External JARs.

9. Complete Compilable Java Program 🔝

import java.util.Properties;

public class MySqlConfigCheckDemo {
    public static Properties buildDefaultProps(String user, String password) {
        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        props.setProperty("serverTimezone", "UTC");
        props.setProperty("useSSL", "false");
        return props;
    }
    public static void main(String[] args) {
        String driverClass = "com.mysql.cj.jdbc.Driver";
        Properties p = buildDefaultProps("root", "test");
        if (driverClass.endsWith("Driver") && "UTC".equals(p.getProperty("serverTimezone"))) {
            System.out.println("All MySQL Connector/J configuration checks passed!");
        }
    }
}

Terminal Output:

All MySQL Connector/J configuration checks passed!

10. Frequently Asked Questions (FAQ) 🔝

Do I still need Class.forName("com.mysql.cj.jdbc.Driver") in modern Java?

Since JDBC 4.0 (Java 6+), DriverManager.getConnection(...) automatically discovers drivers on the Classpath via META-INF/services/java.sql.Driver. However, calling Class.forName() is still a great diagnostic check to confirm the JAR is on your Build Path.

What if I move or delete the downloaded JAR file from my Downloads folder?

Because Add External JARs links to the file path on disk, moving the JAR breaks the Build Path! Best practice is to create a lib/ folder inside your Eclipse project, copy the JAR there, and use Add JARs....

How do I fix "Public Key Retrieval is not allowed" when connecting to MySQL 8?

Append ?allowPublicKeyRetrieval=true&useSSL=false to your JDBC URL during local development.

Can Connector/J 8.x connect to a MySQL 5.7 or MariaDB server?

Yes, MySQL Connector/J 8.x is backward-compatible with MySQL 5.7+ and MariaDB servers over standard MySQL wire protocol on port 3306.

What should I do right after configuring the JAR in Eclipse?

Import the sample student table SQL dump into MySQL and follow our next tutorial: MySQL Connection String & Displaying Data.

Further Reading & Related Tutorials 🔝


Java



plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer