jedis连接redis

news/2024/7/24 8:06:08 标签: redis, 数据库, 缓存

package com.wsd;


import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Redis {
    public static void main(String[] args)  {

        //读取properties file
        String fileName = "redis.properties";
        Map<String,String> map = getProperties(fileName);

        String host = map.get("host");
        int port = Integer.valueOf( map.get("port") );
        String auth = map.get("auth");
        int select = Integer.valueOf( map.get("select") );

        //连接redis
        Jedis jedis = new Jedis(host, port);
        //设置密码
        jedis.auth(auth);
        //选择库
        jedis.select(select);

        //执行set命令
        String OK = jedis.set("name","罗小黑");
        System.out.println(OK);

        //执行get命令
        String name = jedis.get("name");
        System.out.println("name=" + name);

        if(jedis != null)
            jedis.close();

    }


   private static Map<String,String> getProperties(String fileName){
       Map<String,String> map = new HashMap<>();

       Properties properties = new Properties();
       InputStream inputStream = null;

       try{
           //读取properties file
           inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
           properties.load(inputStream);

           String host = properties.getProperty("host");
           String port = properties.getProperty("port");
           String auth = properties.getProperty("auth");
           String select = properties.getProperty("select");

           map.put("host",host);
           map.put("port",port);
           map.put("auth",auth);
           map.put("select",select);

       }catch (IOException ex){
           System.out.println("read file:" + fileName + " fail");
       }finally {
           if(inputStream != null);
           try {
               inputStream.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return map;
   }


}

 


http://www.niftyadmin.cn/n/5185764.html

相关文章

C_9练习题

一、单项选择题(本大题共20小题,每小题2分,共40分。在每小题给出的四个备选项中,选出一个正确的答案,并将所选项前的字母填写在答题纸的相应位置上。) C语言程序中,要使用数学库函数(例sqrt、sin等),需要在程序最前面加上包含文件的预处理命令&#xff08;)。 A. #include <…

三策略,六步骤,Jenkins 迁移到极狐GitLab CI 的终极指南

目录 迁移到极狐GitLab 为什么需要迁移到极狐GitLab 极狐GitLab CI 概览 极狐GitLab CI 的功能特性 Jenkins 到极狐GitLab CI 的迁移指南 迁移准备&#xff1a;培训和沟通 Jenkins 到极狐GitLab CI 的三种迁移策略 迁移策略 1&#xff1a;为新项目使用极狐GitLab CI 迁…

对盒子中的材料进行计数

背景 在做AI算法分析项目的时候&#xff0c;有时候需要我们使用影像分析结合机器学习算法对某些材料盒中的材料进行数目计数&#xff0c;通过自己的分析&#xff0c;给出以下两种解决问题的思路。 1.图像处理方法对材料计数 要使用图像处理方式对盒子中的材料进行数目分析&a…

LeetCode(16)接雨水【数组/字符串】【困难】

目录 1.题目2.答案3.提交结果截图 链接&#xff1a; 42. 接雨水 1.题目 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图&#xff0c;计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 示例 1&#xff1a; 输入&#xff1a;height [0,1,0,2,1,0,1,3,2,1,2,1] 输出&…

Java获取Jar、War包路径,并生成可编辑修改的本地配置文件

前言 本地的可修改配置文件的编写理应是一个很常用的功能&#xff0c;但由于数据库的存在&#xff0c;它鲜少被提及&#xff0c;大多数我们直接存储到数据库中了。 以至于现今&#xff0c;除了没接触数据库的新手时常使用它以外&#xff0c;它没有太多的出场机会。 也因此&am…

settings.json配置

settings.json配置 {"editor.tabSize": 2,"git.ignoreWindowsGit27Warning": true,"workbench.editor.untitled.hint": "hidden","security.workspace.trust.untrustedFiles": "open","[vue]": {"…

【已解决】启动SSH服务报“could not load host key”错误

文章目录 问题复现解决方案 问题复现 解决方案 yum remove openssh-* && yum install -y openssl openssh-server && systemctl restart sshd

我对需求分析的理解

一、背景 最近做了一个项目&#xff0c;也算是踩坑过程&#xff0c;产品上线了&#xff0c;用户不怎么买单&#xff0c;使用者聊聊无几&#xff0c;前期一直不清楚为什么会这样&#xff0c;诚然新系统的开发设计上采用了更新的技术&#xff0c;设计上采用了更好的理念&#xf…