redis

package firstRedisExLesson;

import java.util.List;
import java.util.Set;
import java.util.*;
import redis.clients.jedis.Jedis;



public class MT {

    public void setData() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        Map map = new HashMap();
        Scanner in = new Scanner(System.in);
        System.out.println("输入信息。。。。。");
        System.out.print("数据组数: ");
        int num = in.nextInt();
        while (num > 0) {
            int temp = 0;
            for (int i = 0; i < 4; i++) {
                int a = (int) (Math.random() * 10);
                if (a == 0)
                    a = 1;
                temp += a * Math.pow(10, i);
            }
            String id = String.valueOf(temp);
            System.out.println("用户id:" + temp);
            jedis.lpush("id",id);
            System.out.println("用户名:");
            String n = in.next();
            map.put("username", n);
            jedis.hmset(id, map);
            System.out.println("密码:");
            String pass = in.next();
            map.put("password", pass);
            jedis.hmset(id, map);
            System.out.println("用户生日:");
            String bir = in.next();
            map.put("birth", bir);
            jedis.hmset(id, map);
            num--;
        }
        return;

    }

    public void getData(String id) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println("查找。。。。。");
        if (!jedis.exists(id)) {
            System.out.println("用户id不存在");
            return;
        }
        System.out.println("用户id:" + id + "--用户名:" + jedis.hget(id,"username") + "--密码:" + jedis.hget(id, "password") + "--破蛋日:" + jedis.hget(id, "birth"));
        return;
    }

    public void insert(String id, String pos, String x) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println("插入。。。。。");
        if (!jedis.exists(id)) {
            System.out.println("用户id不存在");
            return;
        }
        Map map = new HashMap();
        map.put(pos, x);
        jedis.hmset(id, map);
        System.out.println("插入成功");
        return;
    }

    public void update(String id, String pos, String x) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println("跟新。。。。。");
        if (!jedis.exists(id)) {
            System.out.println("用户id不存在");
            return;
        }
        Map map = new HashMap();
        map.put(pos,x);
        jedis.hmset(id, map);
        System.out.println("更新成功");
        return;
    }

    public void delete(String id) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println("删除。。。。。");
        Set set = jedis.keys(id);
        if (set.isEmpty()) {
            System.out.println("用户id不存在");
            return;
        }
        jedis.del(id);
        jedis.lrem("id", 0, id);
        System.out.println("删除成功");
        return;
    }

    public void query(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println("遍历。。。。。");
        List l = jedis.lrange("id",0,-1);
        String use;
        for(int i = 0; i < l.size(); i++){

            use = l.get(i);

            System.out.println("用户id:" + use + "--用户名:" + jedis.hget(use,"username") + "--密码:" + jedis.hget(use, "password") + "--破蛋日:" + jedis.hget(use, "birth"));
        }
        return;

    }

    public void queryByName(String name){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println("根据用户名查找。。。。。");
        List list = jedis.lrange("id",0, -1);
        for(int i = 0; i < list.size(); i++){
            String idCompare = list.get(i);
            String nameCompare = jedis.hget(idCompare, "username");
            if(nameCompare.equals(name)){
                System.out.println("用户id:" + idCompare + "--用户名:" + jedis.hget(idCompare,"username") + "--密码:" + jedis.hget(idCompare, "password") + "--破蛋日:" + jedis.hget(idCompare, "birth"));
                break;
            }
        }

    }


    public void cover(){
        System.out.println();System.out.println();
        System.out.println("1.输入数据");
        System.out.println("2.查找");
        System.out.println("3.插入");
        System.out.println("4.更新");
        System.out.println("5.删除");
        System.out.println("6.遍历");
        System.out.println("7.根据用户名遍历");
        System.out.print("...");
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        MT m = new MT();
        m.cover();
        int n = in.nextInt();
        while (n > 0) {
            switch (n) {
                case 1:
                    m.setData();
                    break;
                case 2:
                    System.out.print("输入要查找的用户id:");
                    int idget = in.nextInt();
                    String a = String.valueOf(idget);
                    m.getData(a);
                    break;
                case 3:
                    System.out.print("输入要更新的用户id:");
                    int idinsert = in.nextInt();
                    String b = String.valueOf(idinsert);
                    System.out.print("field:");
                    String pos = in.next();
                    System.out.print("插入信息:");
                    String info = in.next();
                    m.insert(b, pos, info);
                    break;
                case 4:
                    System.out.print("输入要更新的用户id:");
                    int idupdate = in.nextInt();
                    String c = String.valueOf(idupdate);
                    System.out.print("field:");
                    String pos1 = in.next();
                    System.out.print("更新信息:");
                    String infoo = in.next();
                    m.update(c, pos1, infoo);
                    break;
                case 5:
                    System.out.print("输入要删除的用户id:");
                    int iddelete = in.nextInt();
                    String d = String.valueOf(iddelete);
                    m.delete(d);
                    break;
                case 6:
                    m.query();
                    break;
                case 7:
                    System.out.print("输入要查找的用户名:");
                    String qName = in.next();
                    m.queryByName(qName);
            }
            m.cover();
            n = in.nextInt();
        }
    }
}

你可能感兴趣的:(redis)