ShowJdbcDrivers.java - Connector/J JDBC Driver Example

Q

How to verify if Connector/J JDBC Driver is loaded corrected from the classpath? I want to see a Java program example.

✍: FYIcenter.com

A

Here is an example program, ShowJdbcDrivers.java, that can be used to verify if Connector/J JDBC Driver is loaded corrected from the classpath or not:

// Copyright (c) 2016 FYIcenter.com
import java.sql.DriverManager;
import java.sql.Driver;
import java.util.Enumeration;

// Example show all JDBC drivers loaded in the classpath
public class ShowJdbcDrivers {
   public static void main(String [] args) throws Exception {
      System.out.println("Loaded JDBC driver classes:");
      for (Enumeration<Driver> drivers=DriverManager.getDrivers(); 
         drivers.hasMoreElements(); ) {
         System.out.println(drivers.nextElement().getClass().getName());
      }
   }
}

You can compile and run the above example in a command window. You should see Connector/J JDBC driver class listed in the output:

C:\fyicenter>\local\jdk-1.8.0\bin\javac 
   ShowJdbcDrivers.java

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\mysql-connector-java-5.1.40\mysql-connector-java-5.1.40-bin.jar 
   ShowJdbcDrivers

Loaded JDBC driver classes:
com.mysql.jdbc.Driver
com.mysql.fabric.jdbc.FabricMySQLDriver

If you run the example again with a typo in the classpath, you will not see the Connector/J JDBC driver class in the output:

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\mysql-connector-java-5.1.40\mysql-connector-java-5.1.40.jar 
   ShowJdbcDrivers
   
Loaded JDBC driver classes:

 

Examples for Connector/J - JDBC Driver for MySQL

⇒⇒FAQ for Connector/J - JDBC Driver for MySQL

2016-12-04, 1037👍, 0💬