Connecting with Java
This guide explains how to establish a connection between a Java application and a MySQL database using the mysql-connector-j
JDBC driver. It walks through the necessary setup, configuration, and execution of a simple SQL query.
Variables
Certain parameters must be provided to establish a successful connection to a MySQL database. Below is a breakdown of each required variable, its purpose, and where to find it. Here’s what each variable represents:
Variable |
Description |
Purpose |
---|---|---|
|
MySQL username, from the Elestio service overview page |
Identifies the database user who has permission to access the MySQL database. |
|
MySQL password, from the Elestio service overview page |
The authentication key is required for the specified USER to access the database. |
|
Hostname for MySQL connection, from the Elestio service overview page |
The address of the server hosting the MySQL database. |
|
Port for MySQL connection, from the Elestio service overview page |
The network port used to connect to MySQL. The default port is 3306. |
|
Database Name for MySQL connection, from the Elestio service overview page |
The name of the database being accessed. A MySQL instance can contain multiple databases. |
These values can usually be found in the Elestio service overview details as shown in the image below, make sure to take a copy of these details and add it to the code moving ahead.
Prerequisites
- Install Java
- Check if Java is installed by running:
java -version
. - If not installed, download it from oracle.com or install OpenJDK.
- Check if Java is installed by running:
- Install MySQL Connector/J
- Download the latest version
mysql-connector-j
from the official MySQL site.
- Download the latest version
Code
Once all prerequisites are set up, create a new file named MySQLConnect.java
and add the following code:
import java.sql.*;
import java.util.*;
public class MySQLConnect {
public static void main(String[] args) {
Map<String, String> config = new HashMap<>();
for (int i = 0; i < args.length - 1; i += 2)
config.put(args[i], args[i + 1]);
String url = String.format("jdbc:mysql://%s:%s/%s?useSSL=true",
config.get("-host"), config.get("-port"), config.get("-database"));
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try (Connection conn = DriverManager.getConnection(url, config.get("-username"), config.get("-password"));
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT VERSION()")) {
System.out.println("Connected to MySQL");
if (rs.next()) System.out.println("MySQL Version: " + rs.getString(1));
}
} catch (Exception e) {
System.err.println("Connection error: " + e.getMessage());
}
}
}
To compile and run the Java program, use the following commands in your terminal:
javac MySQLConnect.java && java -cp mysql-connector-j-9.3.0.jar:. MySQLConnect -host HOST -port PORT -database DATABASE -username avnadmin -password PASSWORD
If the connection is successful, the terminal will display output similar to:
Connected to MySQL
MySQL Version: 8.0.41
No Comments