tencent cloud

All product documents
TencentDB for Redis®
Java Connection Sample
Last updated: 2024-11-05 10:10:15
Java Connection Sample
Last updated: 2024-11-05 10:10:15
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.
Download and install Jedis. The latest version is recommended.
If you want to connect to the database over SSL, enable SSL encryption to get the SSL certificate file.

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.
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();
}
}
}
Execution results:
img



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.
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();
}
}
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon