Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ‚̧ ūüíĽ
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java - how to load MySQL JDBC driver proper way?

0 contributions
11 points

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).

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey ūüĎč
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more