Пример #1
0
 public static void main(String[] args) {
   for (int i = 0; i < 10; i++) System.out.println(UUID.randomUUID().toString().length());
 }
Пример #2
0
  /**
   * 根据主键类型和主键的前缀生成表的主键
   *
   * @param type 表的主键类型
   * @param prefix 表的主键前缀
   * @return
   * @throws SQLException
   */
  public Sequence generateObjectKey(String type, String prefix, Connection con)
      throws SQLException {

    Sequence sequence = new Sequence();

    if (type.equals("sequence")) // 不需要锁
    {
      long curValue = this.curValue;
      //				String sql = "select " + this.generator + ".nextval from dual";
      do {
        //					PreparedDBUtil dbutil = new PreparedDBUtil();
        try {
          //						dbutil.preparedSelect(this.dbname,sql);
          //						dbutil.executePrepared(con);
          //						curValue = dbutil.getLong(0,0);
          //						if(this.synsequece && this.exist(con,curValue))
          //							continue;
          curValue = this.dbAdapter.getNextValue(this.seqfunction, generator, con, this.dbname);
          if (this.synsequece && this.exist(con, curValue)) continue;

        } catch (SQLException e) {

          e.printStackTrace();
          throw e;
        }
        //					sequence.setPrimaryKey(new Long(curValue));
        //					sequence.setSequence(curValue);
        //					return sequence;
        break;
      } while (true);
      this.curValue = curValue;
      if (this.metaType.equals("int")
          || this.metaType.equals("java.lang.Integer")
          || this.metaType.equals("java.lang.integer")
          || this.metaType.equalsIgnoreCase("integer")) {
        sequence.setPrimaryKey(new Long(curValue));
        sequence.setSequence(curValue);
        return sequence;
      }
      if (this.metaType.equals("java.lang.Long")
          || this.metaType.equals("java.lang.long")
          || this.metaType.equalsIgnoreCase("long")) {
        sequence.setPrimaryKey(new Long(curValue));
        sequence.setSequence(curValue);
        return sequence;
      }

      if (this.metaType.equals("java.lang.String") || this.metaType.equalsIgnoreCase("string")) {
        sequence.setPrimaryKey(this.prefix + curValue + "");
        sequence.setSequence(curValue);
        return sequence;
      } else {
        sequence.setPrimaryKey(this.prefix + curValue + "");
        sequence.setSequence(curValue);
        return sequence;
      }
    } else if (type.equals("uuid")) {
      //              UUID.randomUUID().toString();
      sequence.setPrimaryKey(UUID.randomUUID().toString());
      sequence.setSequence(this.curValue);
      return sequence;
    } else // 需要锁
    {
      synchronized (this) {
        switch (keygenerator_mode) {
          case 0: // 自动模式
            curValue += increment;
            break;
          case 1:
            curValue += increment;
            synchroDB(con);
            break;
        }
        if (this.metaType.equals("int")
            || this.metaType.equals("java.lang.Integer")
            || this.metaType.equals("java.lang.integer")
            || this.metaType.equalsIgnoreCase("integer")) {
          sequence.setPrimaryKey(new Long(curValue));
          sequence.setSequence(curValue);
          return sequence;
        }
        if (this.metaType.equals("java.lang.Long")
            || this.metaType.equals("java.lang.long")
            || this.metaType.equalsIgnoreCase("long")) {
          sequence.setPrimaryKey(new Long(curValue));
          sequence.setSequence(curValue);
          return sequence;
        }

        if (this.metaType.equals("java.lang.String") || this.metaType.equalsIgnoreCase("string")) {
          sequence.setPrimaryKey(this.prefix + curValue + "");
          sequence.setSequence(curValue);
          return sequence;
        } else {
          sequence.setPrimaryKey(this.prefix + curValue + "");
          sequence.setSequence(curValue);
          return sequence;
        }
      }
    }

    //			Sequence sequence = new Sequence();

    // return curValue;
  }