Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6542164
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: Mysql/postgreSQL

2015-09-23 17:10:40


点击(此处)折叠或打开
  1. package com.hxl;

  2. import redis.clients.jedis.Jedis;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.Map;

  13. public class LoadDataToHset {
  14.     public static void main(String[] args) {
  15.         // 连接本地的 Redis 服务
  16.         Jedis jedis = new Jedis("192.168.56.101");
  17.         System.out.println(new Date());    
  18.         
  19.         Map map = new HashMap();
  20.         map = getData();
  21.         Iterator iter = map.entrySet().iterator();
  22.         while (iter.hasNext()) {
  23.             Map.Entry entry = (Map.Entry) iter.next();
  24.             String mkey =(String) entry.getKey();
  25.             String val =(String) entry.getValue();
  26.             //System.out.println(key+"--"+val);
  27.             jedis.hset("hset_test", mkey, val);
  28.         }
  29.         
  30.         System.out.println(new Date());    

  31.     }

  32.     @SuppressWarnings("static-access")
  33.     public static HashMap<String, String> getData() {

  34.         HashMap<String, String> map = new HashMap<String, String>();

  35.         PreparedStatement ps = null;
  36.         ResultSet rs = null;
  37.         Connection conn = null;

  38.         try {
  39.             conn = new DbManager().getConnection();
  40.             ps = (PreparedStatement) conn.prepareStatement(
  41.                     "SELECT usernumber,act_cnt FROM hxl.tb_user",
  42.                     ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

  43.             ps.setFetchSize(Integer.MIN_VALUE);
  44.             ps.setFetchDirection(ResultSet.FETCH_REVERSE);
  45.             rs = ps.executeQuery();
  46.             // 处理结果集
  47.             while (rs.next()) {
  48.                 map.put(rs.getString("usernumber"), rs.getString("act_cnt"));
  49.             }

  50.             rs.close(); // 关闭数据库
  51.             conn.close();

  52.         } catch (InstantiationException e) {
  53.             e.printStackTrace();
  54.         } catch (IllegalAccessException e) {
  55.             e.printStackTrace();
  56.         } catch (ClassNotFoundException e) {
  57.             e.printStackTrace();
  58.         } catch (SQLException e) {
  59.             e.printStackTrace();
  60.         }
  61.         return map;
  62.     }
  63. }

数据库连接:

  1. package com.hxl;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;

  5. public class DbManager {

  6.     final static String url = "jdbc:mysql://192.168.56.102/hxl";
  7.     final static String user = "root";
  8.     final static String pwd = "mysql";

  9.     public static Connection getConnection() throws InstantiationException,
  10.             IllegalAccessException, ClassNotFoundException, SQLException {
  11.         Connection conn = null;
  12.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  13.         // 建立到MySQL的连接
  14.         conn = DriverManager.getConnection(url, user, pwd);
  15.         return conn;
  16.     }

  17.     public static void main(String[] args) throws InstantiationException,
  18.             IllegalAccessException, ClassNotFoundException, SQLException {

  19.         DbManager dbm = new DbManager();

  20.         if (DbManager.getConnection() != null) {
  21.             System.out.println("Connect Success!");
  22.         }

  23.     }

  24. }


阅读(3738) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~