Languages
[Edit]
EN

Java - how to load MySQL JDBC driver proper way?

14 points
Created by:
Jax
388

Working with MySQL and JDBC in Java requires to make some steps depending of driver version we work.

Universal steps:

  1. download JDBC driver visit maven repository web page select some version and download *.jar file,
  2. attach *.jar file to your project as library,
  3. load "com.mysql.jdbc.Driver" or "com.mysql.cj.jdbc.Driver" with Class.forName() method call if it is necessary.

Below fast descriptions how to load drivers have been described.

1. How to recognize, if we have new or old one driver?

If bellow message will appear in console it means we have new driver.

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Read more about version with API changes here.

2. New JDBC driver loading way example

If projects uses the newest vesion of JDBC, this approach should be used.

package com.dirask.examples;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Program {
	
	// Optionally for new version of driver:
	//     private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
	
	private static final String DB_NAME = "test";
	private static final String DB_HOST = "127.0.0.1"; // 'localhost'
	private static final String DB_USER = "root";
	private static final String DB_PASSWORD = "root";
	
	private static final String DB_URL = "jdbc:mysql://" + DB_HOST + "/" + DB_NAME + "?serverTimezone=UTC";
	
	public static void main(String[] args) throws ClassNotFoundException {

		// Optionally new versions of driver can be loaded here:
		//     Class.forName(JDBC_DRIVER);
			
		String sql = "SELECT * FROM `users`";
			
		try (
			Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);	
			Statement statement = connection.createStatement();
		) {
			try (ResultSet result = statement.executeQuery(sql)) {
				System.out.print("[id]\t[name]\t[role]\n");
					
				while (result.next()) {
					int id = result.getInt("id");
					String name = result.getString("name");
					String role = result.getString("role");
						
					System.out.print(id + "\t" + name + "\t" + role + "\n");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

Result:

[id]	[name]	[role]
1		John	admin
2		Chris	moderator
3		Kate	user
4		Denis	moderator
5		Matt	moderator

Note: data preparation sql can be found here (at the end of article).

3. Old JDBC driver loading way example

package com.dirask.examples;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Program {
	
	// old version of driver
	private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	
	private static final String DB_NAME = "test";
	private static final String DB_HOST = "127.0.0.1"; // 'localhost'
	private static final String DB_USER = "root";
	private static final String DB_PASSWORD = "root";
	
	private static final String DB_URL = "jdbc:mysql://" + DB_HOST + "/" + DB_NAME;
	
	public static void main(String[] args) throws ClassNotFoundException {

		// old versions of driver must be loaded before making connection
		Class.forName(JDBC_DRIVER);
			
		String sql = "SELECT * FROM `users`";
			
		try (
			Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
			Statement statement = connection.createStatement();
		) {
			try (ResultSet result = statement.executeQuery(sql)) {
				System.out.print("[id]\t[name]\t[role]\n");
					
				while (result.next()) {
					int id = result.getInt("id");
					String name = result.getString("name");
					String role = result.getString("role");
						
					System.out.print(id + "\t" + name + "\t" + role + "\n");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

Result:

[id]	[name]	[role]
1		John	admin
2		Chris	moderator
3		Kate	user
4		Denis	moderator
5		Matt	moderator

Note: data preparation sql can be found here (at the end of article).

Alternative titles

  1. Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI ...
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - MySQL

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join