EN
Java - how to load MySQL JDBC driver proper way?
14
points
Working with MySQL and JDBC in Java requires to make some steps depending of driver version we work.
Universal steps:
- download JDBC driver visit maven repository web page select some version and download
*.jar
file, - attach
*.jar
file to your project as library, - load "
com.mysql.jdbc.Driver
" or "com.mysql.cj.jdbc.Driver
" withClass.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).