# Connecting with Java

This guide explains how to establish a connection between a Java application and a KeyDB database using the <span class="s3">Jedis</span> library. It walks through the necessary setup, configuration, and execution of a simple KeyDB command.

## **Variables**

Certain parameters must be provided to establish a successful connection to a KeyDB 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: 11.5595%; border-color: rgb(0, 0, 0);">**Variable**

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

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

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

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

</td><td style="width: 46.1263%; border-color: rgb(0, 0, 0);">The address of the server hosting your KeyDB instance.

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

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

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

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

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

</td><td style="width: 46.1263%; border-color: rgb(0, 0, 0);">The authentication key required to connect securely to KeyDB.

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

[![Screenshot 2025-06-26 at 1.13.23 PM.jpg](https://docs.elest.io/uploads/images/gallery/2025-06/scaled-1680-/Jd1screenshot-2025-06-26-at-1-13-23-pm.jpg)](https://docs.elest.io/uploads/images/gallery/2025-06/Jd1screenshot-2025-06-26-at-1-13-23-pm.jpg)

## **Prerequisites**

**Install Java**

Check if Java is installed by running:

```
java -version
```

If not installed, download it from oracle.com and install.

**Download Jedis and Dependencies**

The Jedis library enables Java applications to interact with KeyDB. You need to download two JAR files manually:

1. <span class="s1">**Jedis JAR**</span> (Jedis 5.1.0):
    
    [https://repo1.maven.org/maven2/redis/clients/jedis/5.1.0/jedis-5.1.0.jar](https://repo1.maven.org/maven2/redis/clients/jedis/5.1.0/jedis-5.1.0.jar)
2. **Apache Commons Pool2 JAR**<span class="s1"> (Required by Jedis):</span>
    
    [https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.11.1/commons-pool2-2.11.1.jar](https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.11.1/commons-pool2-2.11.1.jar)

Place both JAR files in the same directory as your Java file.

## **Code**

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

```java
import redis.clients.jedis.JedisPooled;

public class KeyDBTest {
    public static void main(String[] args) {
        String host = "HOST";
        int port = PORT; // e.g., 6379
        String password = "PASSWORD";

        JedisPooled jedis = new JedisPooled(host, port, password);

        try {
            jedis.set("testKey", "Hello KeyDB");
            String value = jedis.get("testKey");

            System.out.println("Connected to KeyDB");
            System.out.println("Retrieved value: " + value);

        } catch (Exception e) {
            System.out.println("KeyDB connection or operation failed: " + e.getMessage());
        }
    }
}
```

To execute the script, open the terminal or command prompt and navigate to the directory where KeyDB<span class="s1">Test.java</span> is located. Once in the correct directory, run the following commands:

**On Linux/macOS :**

```bash
javac -cp "jedis-5.1.0.jar:commons-pool2-2.11.1.jar" KeyDBTest.java
java -cp ".:jedis-5.1.0.jar:commons-pool2-2.11.1.jar" KeyDBTest
```

**On Windows :**

```bash
javac -cp "jedis-5.1.0.jar;commons-pool2-2.11.1.jar" KeyDBTest.java
java -cp ".;jedis-5.1.0.jar;commons-pool2-2.11.1.jar" KeyDBTest
```

If the connection is successful, the terminal will display output similar to:

```bash
Connected to KeyDB  
Retrieved value: Hello KeyDB
```