码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • jedis连接redis


    1. package com.wsd;
    2. import redis.clients.jedis.Jedis;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.util.HashMap;
    6. import java.util.Map;
    7. import java.util.Properties;
    8. public class Redis {
    9. public static void main(String[] args) {
    10. //读取properties file
    11. String fileName = "redis.properties";
    12. Map<String,String> map = getProperties(fileName);
    13. String host = map.get("host");
    14. int port = Integer.valueOf( map.get("port") );
    15. String auth = map.get("auth");
    16. int select = Integer.valueOf( map.get("select") );
    17. //连接redis
    18. Jedis jedis = new Jedis(host, port);
    19. //设置密码
    20. jedis.auth(auth);
    21. //选择库
    22. jedis.select(select);
    23. //执行set命令
    24. String OK = jedis.set("name","罗小黑");
    25. System.out.println(OK);
    26. //执行get命令
    27. String name = jedis.get("name");
    28. System.out.println("name=" + name);
    29. if(jedis != null)
    30. jedis.close();
    31. }
    32. private static Map<String,String> getProperties(String fileName){
    33. Map<String,String> map = new HashMap<>();
    34. Properties properties = new Properties();
    35. InputStream inputStream = null;
    36. try{
    37. //读取properties file
    38. inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    39. properties.load(inputStream);
    40. String host = properties.getProperty("host");
    41. String port = properties.getProperty("port");
    42. String auth = properties.getProperty("auth");
    43. String select = properties.getProperty("select");
    44. map.put("host",host);
    45. map.put("port",port);
    46. map.put("auth",auth);
    47. map.put("select",select);
    48. }catch (IOException ex){
    49. System.out.println("read file:" + fileName + " fail");
    50. }finally {
    51. if(inputStream != null);
    52. try {
    53. inputStream.close();
    54. } catch (IOException e) {
    55. e.printStackTrace();
    56. }
    57. }
    58. return map;
    59. }
    60. }

     

  • 相关阅读:
    ElasticSearch查询工具类分享
    10.20 - 每日一题 - 408
    微信小程序:装B神器P图修改微信流量主小程序源码下载趣味恶搞图制作免服务器域名
    全局异常处理+JSR303验证
    【机器学习】01. 波士顿房价为例子学习线性回归(代码注释,思路推导)
    ElementPlus·表单验证
    人工智能之双色球预测系统(指针)
    Swift 中的并发:Continuations
    贯穿化学制品制造业采购全过程,供应商协同管理系统实现企业订单管理全链协同
    寻找最小覆盖子串 - LeetCode 76
  • 原文地址:https://blog.csdn.net/weixin_41812346/article/details/134442334
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号