# Connecting with Java

This guide explains how to establish a connection between a Java application and a MySQL database using the `<span class="s3">mysql-connector-j</span>` 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:

<table border="1" id="bkmrk-variable-description" style="width: 100%; border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr><th style="width: 10.1293%; border-color: rgb(0, 0, 0);">**Variable**

</th><th style="width: 40.2879%; border-color: rgb(0, 0, 0);">**Description**

</th><th style="width: 49.5828%; border-color: rgb(0, 0, 0);">**Purpose**

</th></tr></thead><tbody><tr><td style="width: 10.1293%; border-color: rgb(0, 0, 0);">`USER`

</td><td style="width: 40.2879%; border-color: rgb(0, 0, 0);">MySQL username, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">Identifies the database user who has permission to access the MySQL database.

</td></tr><tr><td style="width: 10.1293%; border-color: rgb(0, 0, 0);">`PASSWORD`

</td><td style="width: 40.2879%; border-color: rgb(0, 0, 0);">MySQL password, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The authentication key is required for the specified USER to access the database.

</td></tr><tr><td style="width: 10.1293%; border-color: rgb(0, 0, 0);">`HOST`

</td><td style="width: 40.2879%; border-color: rgb(0, 0, 0);">Hostname for MySQL connection, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The address of the server hosting the MySQL database.

</td></tr><tr><td style="width: 10.1293%; border-color: rgb(0, 0, 0);">`PORT`

</td><td style="width: 40.2879%; border-color: rgb(0, 0, 0);">Port for MySQL connection, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The network port used to connect to MySQL. The default port is 3306.

</td></tr><tr><td style="width: 10.1293%; border-color: rgb(0, 0, 0);">`DATABASE`

</td><td style="width: 40.2879%; border-color: rgb(0, 0, 0);">Database Name for MySQL connection, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The name of the database being accessed. A MySQL instance can contain multiple databases.

</td></tr></tbody></table>

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.

[![image.png](https://docs.elest.io/uploads/images/gallery/2025-04/scaled-1680-/Siiimage.png)](https://docs.elest.io/uploads/images/gallery/2025-04/Siiimage.png)

## **Prerequisites**

- **Install Java**
    - Check if Java is installed by running: `java -version`.
    - If not installed, download it from [oracle.com](https://www.oracle.com/java/technologies/javase-downloads.html) or install OpenJDK.

- **Install MySQL Connector/J**
    - Download the latest version `<span class="s2">mysql-connector-j</span>` from the [official MySQL site.](https://dev.mysql.com/downloads/connector/j/)

## **Code**

Once all prerequisites are set up, create a new file named `<span class="s2">MySQLConnect.java</span>` and add the following code:

```java
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
```