Java连接Redis

帮助 ww9992019com 发表于 5 天前

www.hj8828.com开户请拨15894452155西安购房新政出台东莞道滘镇vMt8og<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="9651">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>在Java程序中使用Redis之前,需要确保在机器上安装了Redis的Java驱动程序和Java环境。可以先在将Java电脑上并配置好环境。</p>

<h2 id="h2-u5B89u88C5"><a name="%E5%AE%89%E8%A3%85" class="reference-link"></a><span class="header-link octicon octicon-link"></span>安装</h2><p>现在,让我们看看如何设置Redis Java驱动程序。</p>

<ul>

<li>下载<code>jedis.jar</code> - <a href="http://repo1.maven.org/maven2/redis/clients/jedis/2.1.0/jedis-2.1.0-sources.jar">http://repo1.maven.org/maven2/redis/clients/jedis/2.1.0/jedis-2.1.0-sources.jar</a> ,确保下载的<code>jedis.jar</code>是最新版本。</li><li>将<code>jedis.jar</code>包含到类路径中。</li></ul>

<h2 id="h2-java-redis-"><a name="Java%E8%BF%9E%E6%8E%A5%E5%88%B0Redis%E6%9C%8D%E5%8A%A1%E5%99%A8" class="reference-link"></a><span class="header-link octicon octicon-link"></span>Java连接到Redis服务器</h2><p>请参考以下一个简单的示例代码 - </p>

<pre><code class="lang-java">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());

}

}

</code></pre>

<p>现在,编译并运行上面的程序来测试与Redis服务器的连接。可以根据需要更改路径。假设<code>jedis.jar</code>的当前版本在当前路径中可以使用。<br>执行上面代码,将生成以下结果 - </p>

<pre><code class="lang-java">$javac RedisJava.java

$java RedisJava

Connection to server sucessfully

Server is running: PONG

</code></pre>

<h3 id="h3-redis-java-"><a name="Redis%20Java%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%A4%BA%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>Redis Java字符串示例</h3><pre><code class="lang-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("tutorialname"));

}

}

</code></pre>

<p>执行上面代码,将生成以下结果 - </p>

<pre><code class="lang-java">$javac RedisStringJava.java

$java RedisStringJava

Connection to server sucessfully

Stored string in redis:: Redis tutorial

</code></pre>

<h3 id="h3-redis-java-"><a name="Redis%20Java%E5%88%97%E8%A1%A8%E7%A4%BA%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>Redis Java列表示例</h3><pre><code class="lang-java">import redis.clients.jedis.Jedis;

public 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&lt;String&gt; list = jedis.lrange("tutorial-list", 0 ,5);



  for(int i = 0; i&lt;list.size(); i++) {

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

  }

}

}

</code></pre>

<p>执行上面代码,将生成以下结果 - </p>

<pre><code class="lang-java">$javac RedisListJava.java

$java RedisListJava

Connection to server sucessfully

Stored string in redis:: Redis

Stored string in redis:: Mongodb

Stored string in redis:: Mysql

</code></pre>

<h3 id="h3-redis-java-"><a name="Redis%20Java%E9%94%AE%E7%A4%BA%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>Redis Java键示例</h3><pre><code class="lang-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&lt;String&gt; list = jedis.keys("*");



  for(int i = 0; i&lt;list.size(); i++) {

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

  }

}

}

</code></pre>

<p>执行上面代码,将生成以下结果 - </p>

<pre><code class="lang-java">$javac RedisKeyJava.java

$java RedisKeyJava

Connection to server sucessfully

List of stored keys:: tutorial-name

List of stored keys:: tutorial-list

</code></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Redis分区"><i class="layui-icon"></i>
                            Redis分区</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:PHP连接Redis"><i class="layui-icon"></i>
                            PHP连接Redis</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复