下载 jedis.jar :https://mvnrepository.com/art...java
String host = "127.0.0.1"; //主机地址 int port = 6379; //端口号 String pwd = "foobared"; //登陆密码 try { Jedis jedis = new Jedis(host, port); // 链接redis服务器 String auth = jedis.auth(pwd); // 权限认证 //链接 完成会返回 ok System.out.println("connet the redis:"+auth); } catch (Exception e) { System.out.println("缓存连接错误"); }
public void findAllKeys(){ // jedis.keys("*") 查询全部的key * 为通配符 Set<String> set = jedis.keys("*"); for (String str : set) { System.out.println(str); } }
public void ClearDB() { // flushDB 是清除全部的 key 的命令 String str = jedis.flushDB(); //若是清理完成,会返回 ok System.out.println("flush all Keys:" + str); }
import java.util.Set; import redis.clients.jedis.Jedis; public class RedisStudy { //声明 redis 对象 private static Jedis jedis; private String host = "127.0.0.1"; //测试地址 private int port = 6379; //端口 private String pwd = "foobared"; //密码 /** * 链接redis */ public void getJedis() { try { jedis = new Jedis(host, port); // 链接redis服务器 String auth = jedis.auth(pwd); // 权限认证 System.out.println("connet the redis:"+auth); } catch (Exception e) { System.out.println("缓存连接错误"); } } /** * 清除全部的缓存 */ public void ClearDB() { String str = jedis.flushDB(); System.out.println("flush all Keys:" + str); } /** * 找到全部的KEY */ public void findAllKeys(){ Set<String> set = jedis.keys("*"); for (String str : set) { System.out.println(str); } } public static void main(String[] args) { //声明当前类 RedisStudy rs = new RedisStudy(); //链接 rs.getJedis(); } }