Java Connection Example

Last updated: 2020-05-08 14:44:55

Preparations before running:
Download the Jedis client.

Sample code:

import redis.clients.jedis.Jedis;

public class HelloRedis {

  public static void main(String[] args) {
        try:
            /**Enter your Redis instance private IP, port number, instance ID, and password in the following parameters*/
            String host = "192.168.0.195";
            int port = 6379;
            String instanceid = "84ffd722-b506-4934-9025-645bb2a0997b";
            String password = "1234567q";
            // Connect to Redis
            Jedis jedis = new Jedis(host, port);
            // Authenticate
            jedis.auth(instanceid + ":" + password);

            /**You can start manipulating the Redis instance. For more information, see 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:

Was this page helpful?

Was this page helpful?

  • Not at all
  • Not very helpful
  • Somewhat helpful
  • Very helpful
  • Extremely helpful
Send Feedback
Help