您现在的位置: 爱51代码网 >> 范文 >> 文章正文
Java中UUID的代替方法?Oracle中系列的代替方法

一、
您有么有一种办法来代替UUID,就是说保证全球无重复!
Ps:不用说用 时间 + 随机字符,就是这么说的,技术说那也可能有重复的!
不用说 Base64或Base58,这个也是基于UUID的
现在说的是你自己创造的"UUID"
二、
Oracle中有Sequence,那么您如何在不用Sequence的情况下,做出类似Sequence的效果呢?比方说我当前的最大Id是5,那么下一个应该就是6,但是我1秒可能有上百万的数据存入,您如何实现呢

一、除了你说的2种我想不出来,坐等答案。
二、可以自己写Sequence生成器。

我项目中用的:
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.Map;   /** * * <b>Note</b>: Java实现的Sequence工具 */public class SequenceUtil {                   /**          * 单例模式          */        private static SequenceUtil instance = new SequenceUtil();                   /**          * 存放序列的MAP          */        private Map<String, KeyInfo> keyMap = new HashMap<String, KeyInfo>(20);         private static final int POOL_SIZE = 1;           /**          * 防止外部实例化          */        private SequenceUtil() {         }           /**          * 单例模式,获取单例          *          * @return SequenceUtils的单例对象          */        public static SequenceUtil getInstance() {                 return instance;         }           /**          * 获取下一个sequence值          *          * @param keyName          * Sequence名称          * @return 下一个Sequence键值          */        public synchronized int getNextKeyValue(String keyName) {                 KeyInfo keyInfo = null;                 Integer keyObject = null;                 try {                         if (keyMap.containsKey(keyName)) {                                 keyInfo = keyMap.get(keyName);                         } else {                                 keyInfo = new KeyInfo(keyName, POOL_SIZE);                                 keyMap.put(keyName, keyInfo);                         }                         keyObject = keyInfo.getNextKey();                 } catch (SQLException e) {                         e.printStackTrace();                 }                 return keyObject;         } }   class KeyInfo {                   /**          * 当前Sequence载体的最大值          */        private int maxKey;                   /**          * 当前Sequence的最小值          */        private int minKey;                   /**          * 下一个Sequence值          */        private int nextKey;                   /**          * Sequence缓存值          */        private int poolSize;                   /**          * Sequence的名称          */        private String keyName;                   /**          * 更新存放Sequence表的语句          */        private static final String sql_update = "UPDATE intpub_Sequence SET KEYVALUE = KEYVALUE + ? WHERE KEYNAME = ?";                   /**          * 查询Sequence表中的当前值          */        private static final String sql_query = "SELECT KEYVALUE FROM intpub_Sequence WHERE KEYNAME = ?";           public KeyInfo(String keyName, int poolSize) throws SQLException {                 this.poolSize = poolSize;                 this.keyName = keyName;                 retrieveFromDB();         }           public String getKeyName() {                 return keyName;         }           public int getMaxKey() {                 return maxKey;         }           public int getMinKey() {                 return minKey;         }           public int getPoolSize() {                 return poolSize;         }           /**          * 获取下一个Sequence值          *          * @return 下一个Sequence值          * @throws SQLException          */        public synchronized int getNextKey() throws SQLException {                 if (nextKey > maxKey) {                         retrieveFromDB();                 }                 return nextKey++;         }           /**          * 执行Sequence表初始化和更新工作          *          * @throws SQLException          */        private void retrieveFromDB() throws SQLException {                   Connection conn = ConnectionPool.getInstance().getConnection();                 // 查询数据库                 PreparedStatement pstmt_query = conn.prepareStatement(sql_query);                 pstmt_query.setString(1, keyName);                 ResultSet rs = pstmt_query.executeQuery();                 if (rs.next()) {                         maxKey = rs.getInt(1) + poolSize;                         minKey = maxKey - poolSize + 1;                         nextKey = minKey;                         rs.close();                         pstmt_query.close();                 } else {                         String init_sql = "INSERT INTO intpub_Sequence(KEYNAME,KEYVALUE) VALUES('"                                        + keyName + "',10000 + " + poolSize + ")";                         Statement stmt = conn.createStatement();                         stmt.executeUpdate(init_sql);                         maxKey = 10000 + poolSize;                         minKey = maxKey - poolSize + 1;                         nextKey = minKey;                         stmt.close();                         return;                 }                   // 更新数据库                 conn.setAutoCommit(false);                 PreparedStatement pstmt_up = conn.prepareStatement(sql_update);                 pstmt_up.setLong(1, poolSize);                 pstmt_up.setString(2, keyName);                 pstmt_up.executeUpdate();                 pstmt_up.close();                 conn.commit();                   rs.close();                 pstmt_query.close();                 conn.close();         }   }

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    GIS用百度行政区域的经纬度直接画
    不小心卸载了.net 4 extend,vs2
    C#byte【】里的数转换成string时
    C#的session取不到值
    java如何判断一个字符串里的数字
    随机找出24个不一样的字,在把一
    java怎么实现html转为pdf
    lotus数据列表文档个数如何实时统
    lotus代理中LS如何将字符串保存到
    在lotus BS系统里怎样方便实现统
    不小心卸载了.net 4 extend,vs2
    C#byte【】里的数转换成string时
    C#的session取不到值
    java如何判断一个字符串里的数字
    java怎么实现html转为pdf
    lotus数据列表文档个数如何实时统
    在lotus BS系统里怎样方便实现统
    undefined reference timer_crea
    linux文件/usr/lib破坏了,还原后
    linux上运行system函数时,print
    POI 导入Excel 提示Leftover
    整合ssh后如何获取同一个ses
    通过IP获取局域网内手机的Ne
    Hibernate优缺点有哪些
    Hibernate工作原理是什么
    java解析xml,如何拿到Test节
    client.executeMethod(httpp
    The APR based Apache Tomca
    tabPanel嵌套内部panel 内部
    failed to lazily initializ
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |