tencent cloud

Java Connection Sample
Last updated: 2025-08-22 19:44:02
Java Connection Sample
Last updated: 2025-08-22 19:44:02
This document provides client code samples for Java to help you access a database with or without SSL encryption enabled.

Preparations

Get the private IPv4 address and port information for database connection in the Network Info section on the Instance Details page in the TencentDB for Redis® console. For detailed directions, see Viewing Instance Information.
Get the account and password for database access. For detailed directions, see Managing Account.
If you want to connect to the database over SSL, enable SSL encryption to get the SSL certificate file.
Prepare the client Jedis and use the latest edition.
1.1 Download Jedis and its dependency library (jar package) as follows.
wget https://repo1.maven.org/maven2/redis/clients/jedis/4.3.1/jedis-4.3.1.jar
wget https://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.9/gson-2.8.9.jar
wget https://repol.maven.org/maven2/org/apache/commons/commons-pool2/2.11.1/commons-pool2-2.11.1.jar
1.2 Create a test project with the directory as shown below.


Connection sample with SSL encryption not enabled

You need to modify the parameters based on the comments, including IP, port, account, and password for database access.
Note:
If an exception occurs during connection, please refer to the Private Network Unable to Connect Location Guide to fix it.
import redis.clients.jedis.Jedis;

public class HelloRedis {

public static void main(String[] args) {
try {
/**Enter your Redis instance's private IP, port number, instance ID, and password in the following parameters in case of private network access.
Configure the instance's public network address, port number, and password in case of public network access.*/
String host = "192.xx.xx.195";
int port = 6379;
String instanceid = "crs-09xxxqv";
String password = "123ad6aq";
// Connect to Redis
Jedis jedis = new Jedis(host, port);
// Authenticate
jedis.auth(instanceid + ":" + password);

/**You can start manipulating the Redis instance. For more information, visit https://github.com/xetorthio/jedis.*/
// Set the key
jedis.set("redis", "tencent");
System.out.println("set key redis suc, value is: tencent");
// Get the key
String value = jedis.get("redis");
System.out.println("get key redis is: " + value);

// Close and exit
jedis.quit();
jedis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output information, as shown below.


Connection sample with SSL encryption enabled

You need to modify the parameters based on the comments, including SSL certificate file, IP, port, account, and password for database access. To obtain an SSL certificate, please refer to SSL Encryption.
Note:
If an exception occurs during connection, please refer to the Private Network Unable to Connect Location Guide to fix it.
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;


public class Main {

public static void main(String[] args) throws Exception {
KeyStore trustStore = KeyStore.getInstance("jks");
// `ca.jks` is the certificate file name.
try (InputStream inputStream = new FileInputStream("ca.jks") ){
trustStore.load(inputStream, null);
}
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX");
trustManagerFactory.init(trustStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();

//with ssl config jedis pool
// `vip` is the private IPv4 address for database connection, `6379` is the default port number, and `pwd` is the password of the default account. You need to replace them as needed.
JedisPool pool = new JedisPool(genericObjectPoolConfig, "vip",
6379, 2000, "pwd", 0, true, sslSocketFactory, null, null);
Jedis jedis = pool.getResource();
System.out.println(jedis.ping());
jedis.close();
}
}

Execution result

Run the sample program (SSL non-encryption connection example or SSL encryption connection example) under the project.

javac -cp ".:lib/*" src/HelloRedis. java -d ./
java-cp ".:lib/*" HelloRedis
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback