假如我们已经配置好环境,安装了redis
然后下载我们项目中需要的 Jedis.jar包,下载地址 http://download.csdn.net/detail/sinat_30474567/9863667 要下载最新版本的驱动包,添加到类路径下
连接的时候需要权限验证 ,不要忘了授权验证
然后 测试后项目中能否连同redis
下面进行验证
package Services;
import redis.clients.jedis.Jedis;
public class jredis {
public static void main(String[] args) {
//Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
jedis.auth("dhb");//
jedis.set("xd", "I lova java");//往redis 中写值,存到内存中
System.out.println("Connection to server sucessfully");
//check whether server is running or not
System.out.println("Server is running: "+jedis.ping());
jedis.auth("dhb");
System.out.println("通过键值对中键值,来获取值" + jedis.get("xd"));//得到值
}
}
结果为;
Connection to server sucessfully
Server is running: PONG
通过键值对中键值,来获取值I lova java
通过命令 查看写入的值