jdbc-redis 主页 https://code.google.com/p/jdbc-redis/
jdbc-redis 源码 https://code.google.com/p/jdbc-redis/source/browse
jdbc-redis jar 下载 http://download.csdn.net/detail/sdn_superuser/6493779
点开源码页的samples 里面有相关的列子
package jackson;
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.234");
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 value = "myfirstvalue1";
String sql = "set my_first_key "+value;
stm.execute(sql);
stm.execute("get my_first_key");
rs = stm.getResultSet();
System.out.println("onekey:");
while (rs.next()) {
System.out.println(rs.getString(0));
// 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");
rs=stm.getResultSet();
while(rs.next()){
System.out.println(rs.getString("mylist"));
}
}
}
这里代码有问题, 就是说 rs.getString的时候返回的是 key的长度而不是key,网上找了很久没有答案,项目放到linux上跑也是同样的问题
http://bbs.csdn.net/topics/370083835
http://www.oschina.net/question/118696_24434
目前找到的唯一的合理解释
这个主要是redis版本问题,redis-jdbc发送redis命令采用了Bulk commands方式,
这是redis-1.2之前的协议,你可以改用redis-1.2的版本,这样操作是成功的。结果和预期一样。
对于新版本的redis,官方上推荐的是jredis,你可以使用这个,而且redis-jdbc开发也不活跃,基本没更新了