java 连接 Redis

使用 jedis-2.9.0.jar 包 连接 Docker 中的 Redis 容器。

package redisTest; /**
 * @Author:YangLianjun
 * @Description:
 * @Date: 2018/11/2 8:43
 * @Modify By
 **/

import redis.clients.jedis.Jedis;

/**
 * @api
 * @apiDescription  java 使用jar包,连接Redis数据库。此Redis是 Docker中的容器
 * @apiVersion
 * @apiName
 * @apiGroup
 * @apiHeader {String} token
 * @apiParam
 * @apiParam
 * @apiParam
 * @apiSuccessExample
 **/
public class RedisTest {
    static String  host = "***.***.***.***" ;   // 服务器地址
    static int port = 6379 ;                // 端口号
    static String password = "****" ;      // 密码
    static Jedis jedis = null ;
    //  定义连接Redis 数据库方法
    public static void connectRedis (){
        try {
            jedis = new Jedis(host,port) ;  // 连接redis服务器
            jedis.auth(password) ;   // 连接密码
            String connect = jedis.ping();
            if (connect.equals("PONG")){
                System.out.println("Redis数据库连接成功");
            }else {
                System.out.println("失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null)
                jedis.close();
        }
    }
    public static void main (String[]args){
        // 调用方法
        connectRedis();
    }
}

 

posted @ 2018-11-02 09:24  Yang-lianjun  阅读(575)  评论(0编辑  收藏  举报