Java链接redis_动力节点Java学院整理

网友投稿 249 2023-04-15


Java链接redis_动力节点Java学院整理

要在java程序中使用使用操作Redis,需要确保有Redis的Java驱动程序和Java设置在机器上。现在,让我们来看看如何设置Redis的Java驱动程序。

需要下载jedis.jar。请一定要下载它的最新版本。

需要包括jedis.jar到你的类路径中。

连接到Redis服务器

import redis.clients.jedis.Jedis;

public class RedisJava {

public static void main(String[] args) {

//Connecting to Redis server on localhost

Jedis jedis = new Jedis("localhost");

System.out.println("Connection to server sucessfully");

//check whether server is running or not

System.out.println("Server is running: "+jedis.ping());

}

}

现在,让我们来编译和运行上面的程序来测试连接Redis服务器。你可以根据实际情况改变路径。这里假设jedis.jar的当前版本在当前路径中可用

$javac RedisJava.java

$java RedisJava

Connection to server sucessfully

Server is running: PONG

Redis和Java字符串实例

import redis.clients.jedis.Jedis;

public class RedisStringJava {

public static void main(String[] args) {

//Connecting to Redis server on localhost

Jedis jedis = new Jedis("localhost");

System.out.println("Connection to server sucessfully");

//set the data in redis string

jedis.set("tutorial-name", "Redis tutorial");

// Get the stored data and print it

System.out.println("Stored string in redis:: "+ jedis.get("tutorial-name"));

}

}

现在,让我们编译并运行上述程序。

$javac RedisStringJava.java

$java RedisStringJava

Connection to server sucessfully

Stored string in redis:: Redis tutorial

Redis和Java列表示例

import redis.clients.jedis.Jedis;

phttp://ublic class RedisListJava {

public static void main(String[] args) {

//Connecting to Redis server on localhost

Jedis jedis = new Jedis("localhost");

System.out.println("Connection to server sucessfully");

//store data in redis list

jedis.lpush("tutorial-list", "Redis");

jedis.lpush("tutorial-list", "Mongodb");

jedis.lpush("tutorial-list", "mysql");

// Get the stored data and print it

List list = jedis.lrange("tutorial-list", 0 ,5);

for(int i=0; i

System.out.println("Stored string in redis:: "+list.get(i));

}

}

}

现在,让我们编译并运行上述程序。

$javac RedisListJava.java

$java RedisListJava

Connection to server sucessfully

Stored string in redis:: Redis

Stored string in redis:: Mongodb

Stored string in redis:: Mysql

Redis和Java的键实例

import redis.clients.jedis.Jedis;

public class RedisKeyJava {

public static void main(String[] args) {

//Connecting to Redis server on localhost

Jedis jedis = new Jedis("localhost");

System.out.println("Connection to server sucessfully");

//store data in redis list

// Get the stored data and print it

List list = jedis.keys("*");

for(int i=0; i

System.out.println("List of stored keys:: "+list.get(i));

}

}

}

现在,让我们编译并运行上述程序。

$javac RedisKeyJava.java

$java RedisKeyJava

Connection to server sucessfully

List of stored keys:: tutorial-name

List of stored keys:: tutorial-list

System.out.println("Stored string in redis:: "+list.get(i));

}

}

}

现在,让我们编译并运行上述程序。

$javac RedisListJava.java

$java RedisListJava

Connection to server sucessfully

Stored string in redis:: Redis

Stored string in redis:: Mongodb

Stored string in redis:: Mysql

Redis和Java的键实例

import redis.clients.jedis.Jedis;

public class RedisKeyJava {

public static void main(String[] args) {

//Connecting to Redis server on localhost

Jedis jedis = new Jedis("localhost");

System.out.println("Connection to server sucessfully");

//store data in redis list

// Get the stored data and print it

List list = jedis.keys("*");

for(int i=0; i

System.out.println("List of stored keys:: "+list.get(i));

}

}

}

现在,让我们编译并运行上述程序。

$javac RedisKeyJava.java

$java RedisKeyJava

Connection to server sucessfully

List of stored keys:: tutorial-name

List of stored keys:: tutorial-list

System.out.println("List of stored keys:: "+list.get(i));

}

}

}

现在,让我们编译并运行上述程序。

$javac RedisKeyJava.java

$java RedisKeyJava

Connection to server sucessfully

List of stored keys:: tutorial-name

List of stored keys:: tutorial-list


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:mock一个工具类(mock一个方法)
下一篇:Java时间轮算法的实现代码示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~