键值数据库—Redis(一) 基础入门

Redis的知识准备

redis的基础介绍:http://blog.csdn.net/Java2King/archive/2010/07/27/5769738.aspx

redis的安装:http://hanqunfeng.javaeye.com/blog/684108

 

JDBC—Redis的使用

redis的java-jdbc的下载地址 : http://code.google.com/p/jdbc-redis/

 

代码例子:插入10万的数据

package com.redis; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * @author zhujiadun.pt * */ public class TestRedis { /** * @param args * @throws ClassNotFoundException * @throws SQLException */ public static void main(String[] args) throws ClassNotFoundException, SQLException { //装载Redis数据库的驱动器 Class.forName("br.com.svvs.jdbc.redis.RedisDriver"); //连接数据库 Connection conn = DriverManager.getConnection("jdbc:redis://localhost"); //获取Statement Statement stmt = conn.createStatement(); //执行SQL语句 for(int i=0;i<100000;i++){ stmt.execute("set my_first_key"+i+" my first value"+i); } stmt.execute("get my_first_key"); ResultSet r = stmt.getResultSet(); while (r.next()) { System.out.println(">" + r.getString(0) + "<"); } } }

 

另一段经典的jdbc代码

package com.Redis.Demo; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; public class RedisDemo { private static Connection conn = null; private static Statement stm = null; private static ResultSet rs = null; /** * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { init(); oneKey(); manyKey(); } /* * 初始化 */ private static void init() { try { // 加载redis jdbc驱动 Class.forName("br.com.svvs.jdbc.redis.RedisDriver"); // 连接 conn = DriverManager.getConnection("jdbc:redis://192.168.1.117"); stm = conn.createStatement(); } catch (ClassNotFoundException e) { System.out.println(e.toString()); } catch (SQLException e) { System.out.println(e.toString()); } } /* * 关闭stm,conn */ private static void close() { try { // rs.close(); stm.close(); conn.close(); } catch (SQLException e) { System.out.println(e.toString()); } } /* * 一个键值的操作 * */ private static void oneKey() throws SQLException { String sql = "set my_first_key myfirstvalue"; stm.execute(sql); stm.execute("get my_first_key"); ResultSet rs = stm.getResultSet(); while (rs.next()) { System.out.println(rs.getString("my_first_key")); } close(); } /* * 一个key对多个value操作 */ private static void manyKey() throws SQLException { stm.execute("lpush mylist value1"); stm.execute("lpush mylist value2"); stm.execute("lpush mylist value3"); stm.execute("lrange mylist 0 -1"); ResultSet rs=stm.getResultSet(); while(rs.next()){ System.out.println(rs.getString("mylist")); } } }

 

你可能感兴趣的:(redis,sql,数据库,String,jdbc,null)