📄 635830.xml
字号:
   * 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>/**
     *  Get the next available ID
     *  @param tablename The table name
     *  @param pkname The primary key column, must be Integer
     *  @param conn The database connection object
     *  @return int The new ID
     *  @throws SQLException
     */
    public static int getNextID( String tablename, String pkname, Connection conn )
    throws SQLException
    {
        int id = 1;
        String str = "Select MAX(" + pkname + ") As A From " + tablename;
        Statement stmt = conn.createStatement( );
        ResultSet rs = stmt.executeQuery( str );
        if ( rs.next() )
        {
            id += rs.getInt( "A" );
        }
        rs.close();
        stmt.close();
        return id;
    }
然后进行 insert 操作。
但整个过程需要一个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 + -