⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 635830.xml

📁 论坛精华帖子
💻 XML
📖 第 1 页 / 共 3 页
字号:
   * Static block to take care of one time secureRandom seed.
   * It takes a few seconds to initialize SecureRandom.  You might
   * want to consider removing this static block or replacing
   * it with a "time since first loaded" seed to reduce this time.
   * This block will run only once per JVM instance.
   */

  static {
    mySecureRand = new SecureRandom();
    long secureInitializer = mySecureRand.nextLong();

    myRand = new Random(secureInitializer);
  }

  /*
   * Default constructor.  With no specification of security option,
   * this constructor defaults to lower security, high performance.
   */
  public RandomGUID() {
    getRandomGUID(false);
  }

  /*
   * Constructor with security option.  Setting secure true
   * enables each random number generated to be cryptographically
   * strong.  Secure false defaults to the standard Random function seeded
   * with a single cryptographically strong random number.
   */
  public RandomGUID(boolean secure) {
    getRandomGUID(secure);
  }

  /*
   * Method to generate the random GUID
   */
  private void getRandomGUID(boolean secure) {
    MessageDigest md5 = null;
    StringBuffer sbValueBeforeMD5 = new StringBuffer();

    try {
      md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      System.out.println("Error: " + e);
    }

    try {
      InetAddress id = InetAddress.getLocalHost();
      long time = System.currentTimeMillis();
      long rand = 0;

      if (secure) {
        rand = mySecureRand.nextLong();
      } else {
        rand = myRand.nextLong();
      }

      // This StringBuffer can be a long as you need; the MD5
      // hash will always return 128 bits.  You can change
      // the seed to include anything you want here.
      // You could even stream a file through the MD5 making
      // the odds of guessing it at least as great as that
      // of guessing the contents of the file!
      sbValueBeforeMD5.append(id.toString());
      sbValueBeforeMD5.append(":");
      sbValueBeforeMD5.append(Long.toString(time));
      sbValueBeforeMD5.append(":");
      sbValueBeforeMD5.append(Long.toString(rand));

      valueBeforeMD5 = sbValueBeforeMD5.toString();
      md5.update(valueBeforeMD5.getBytes());

      byte[] array = md5.digest();
      StringBuffer sb = new StringBuffer();

      for (int j = 0; j < array.length; ++j) {
        int b = array[j] & 0xFF;

        if (b < 0x10) sb.append('0');
        sb.append(Integer.toHexString(b));
      }

      valueAfterMD5 = sb.toString();

    } catch (UnknownHostException e) {
      System.out.println("Error:" + e);
    }
  }

  /*
   * Convert to the standard format for GUID
   * (Useful for SQL Server UniqueIdentifiers, etc.)
   * Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
   */
  public String toString() {
    String raw = valueAfterMD5.toUpperCase();
    StringBuffer sb = new StringBuffer();

    sb.append(raw.substring(0, 8));
    sb.append("-");
    sb.append(raw.substring(8, 12));
    sb.append("-");
    sb.append(raw.substring(12, 16));
    sb.append("-");
    sb.append(raw.substring(16, 20));
    sb.append("-");
    sb.append(raw.substring(20));

    return sb.toString();
  }
    
  /*
   * Demonstraton and self test of class
   */
  public static void main(String args[]) {
    RandomGUID myGUID = new RandomGUID();

    System.out.println("Seeding String=" + myGUID.valueBeforeMD5);
    System.out.println("rawGUID=" + myGUID.valueAfterMD5);
    System.out.println("RandomGUID=" + myGUID.toString());
  }
}
</Content>
<PostDateTime>2002-4-12 16:34:01</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>谭振</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>4228333</ReplyID>
<TopicID>635830</TopicID>
<PostUserId>212683</PostUserId>
<PostUserName>tanzhen</PostUserName>
<Point>10</Point>
<Content>/**
&#32;&#32;&#32;&#32;&#32;*&#32;&#32;Get&#32;the&#32;next&#32;available&#32;ID
&#32;&#32;&#32;&#32;&#32;*&#32;&#32;@param&#32;tablename&#32;The&#32;table&#32;name
&#32;&#32;&#32;&#32;&#32;*&#32;&#32;@param&#32;pkname&#32;The&#32;primary&#32;key&#32;column,&#32;must&#32;be&#32;Integer
&#32;&#32;&#32;&#32;&#32;*&#32;&#32;@param&#32;conn&#32;The&#32;database&#32;connection&#32;object
&#32;&#32;&#32;&#32;&#32;*&#32;&#32;@return&#32;int&#32;The&#32;new&#32;ID
&#32;&#32;&#32;&#32;&#32;*&#32;&#32;@throws&#32;SQLException
&#32;&#32;&#32;&#32;&#32;*/
&#32;&#32;&#32;&#32;public&#32;static&#32;int&#32;getNextID(&#32;String&#32;tablename,&#32;String&#32;pkname,&#32;Connection&#32;conn&#32;)
&#32;&#32;&#32;&#32;throws&#32;SQLException
&#32;&#32;&#32;&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;int&#32;id&#32;=&#32;1;
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;String&#32;str&#32;=&#32;"Select&#32;MAX("&#32;+&#32;pkname&#32;+&#32;")&#32;As&#32;A&#32;From&#32;"&#32;+&#32;tablename;

&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;Statement&#32;stmt&#32;=&#32;conn.createStatement(&#32;);
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;ResultSet&#32;rs&#32;=&#32;stmt.executeQuery(&#32;str&#32;);
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;if&#32;(&#32;rs.next()&#32;)
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;id&#32;+=&#32;rs.getInt(&#32;"A"&#32;);
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;}

&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;rs.close();
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;stmt.close();

&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;return&#32;id;
&#32;&#32;&#32;&#32;}

然后进行&#32;insert&#32;操作。

但整个过程需要一个transaction以应付并发访问。
</Content>
<PostDateTime>2002-4-12 16:35:11</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>网井</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>110</credit>
<ReplyID>4229715</ReplyID>
<TopicID>635830</TopicID>
<PostUserId>86631</PostUserId>
<PostUserName>netpit</PostUserName>
<Point>0</Point>
<Content>非常之之感谢。

</Content>
<PostDateTime>2002-4-12 17:27:04</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>网井</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>110</credit>
<ReplyID>4275757</ReplyID>
<TopicID>635830</TopicID>
<PostUserId>86631</PostUserId>
<PostUserName>netpit</PostUserName>
<Point>0</Point>
<Content>结帐了。</Content>
<PostDateTime>2002-4-15 23:41:46</PostDateTime>
</Reply>
</Replys>
</Topic>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -