博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.oracle32位,Java、JavaScript、php、mysql、oracle生成uuid(32位)
阅读量:4362 次
发布时间:2019-06-07

本文共 5962 字,大约阅读时间需要 19 分钟。

Java、JavaScript、php、mysql、oracle生成uuid(32位)的方法

一、Java:

import java.net.InetAddress;

public class IDGenerator {

private String sep = "";

private static final int IP;

private static short counter = (short) 0;

private static final int JVM = (int) (System.currentTimeMillis() >>> 8);

private static IDGenerator uuidgen = new IDGenerator();

static {

int ipadd;

try {

ipadd = toInt(InetAddress.getLocalHost().getAddress());

} catch (Exception e) {

ipadd = 0;

}

IP = ipadd;

}

public static IDGenerator getInstance() {

return uuidgen;

}

public static int toInt(byte[] bytes) {

int result = 0;

for (int i = 0; i < 4; i++) {

result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];

}

return result;

}

protected String format(int intval) {

String formatted = Integer.toHexString(intval);

StringBuffer buf = new StringBuffer("00000000");

buf.replace(8 - formatted.length(), 8, formatted);

return buf.toString();

}

protected String format(short shortval) {

String formatted = Integer.toHexString(shortval);

StringBuffer buf = new StringBuffer("0000");

buf.replace(4 - formatted.length(), 4, formatted);

return buf.toString();

}

protected int getJVM() {

return JVM;

}

protected synchronized short getCount() {

if (counter < 0) {

counter = 0;

}

return counter++;

}

protected int getIP() {

return IP;

}

protected short getHiTime() {

return (short) (System.currentTimeMillis() >>> 32);

}

protected int getLoTime() {

return (int) System.currentTimeMillis();

}

public String generate() {

return new StringBuffer(36).append(format(getIP())).append(sep).append(

format(getJVM())).append(sep).append(format(getHiTime()))

.append(sep).append(format(getLoTime())).append(sep).append(

format(getCount())).toString();

}

}

二、JavaScript:

(function(window){

//On creation of a UUID object, set it's initial value

function UUID() {

this.id = this.createUUID();

}

// When asked what this Object is, lie and return it's value

UUID.prototype.valueOf = function() {

return this.id;

};

UUID.prototype.toString = function() {

return this.id;

};

//

// INSTANCE SPECIFIC METHODS

//

UUID.prototype.createUUID = function() {

//

// Loose interpretation of the specification DCE 1.1: Remote Procedure Call

// since JavaScript doesn't allow access to internal systems, the last 48 bits

// of the node section is made up using a series of random numbers (6 octets long).

//

var dg = new Date(1582, 10, 15, 0, 0, 0, 0);

var dc = new Date();

var t = dc.getTime() - dg.getTime();

var tl = UUID.getIntegerBits(t, 0, 31);

var tm = UUID.getIntegerBits(t, 32, 47);

var thv = UUID.getIntegerBits(t, 48, 59) + '1'; // version 1, security version is 2

var csar = UUID.getIntegerBits(UUID.rand(4095), 0, 7);

var csl = UUID.getIntegerBits(UUID.rand(4095), 0, 7);

// since detection of anything about the machine/browser is far to buggy,

// include some more random numbers here

// if NIC or an IP can be obtained reliably, that should be put in

// here instead.

var n = UUID.getIntegerBits(UUID.rand(8191), 0, 7)

+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)

+ UUID.getIntegerBits(UUID.rand(8191), 0, 7)

+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)

+ UUID.getIntegerBits(UUID.rand(8191), 0, 15); // this last number is two octets long

return tl + tm + thv + csar + csl + n;

};

//Pull out only certain bits from a very large integer, used to get the time

//code information for the first part of a UUID. Will return zero's if there

//aren't enough bits to shift where it needs to.

UUID.getIntegerBits = function(val, start, end) {

var base16 = UUID.returnBase(val, 16);

var quadArray = new Array();

var quadString = '';

var i = 0;

for (i = 0; i < base16.length; i++) {

quadArray.push(base16.substring(i, i + 1));

}

for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {

if (!quadArray[i] || quadArray[i] == '')

quadString += '0';

else

quadString += quadArray[i];

}

return quadString;

};

//Replaced from the original function to leverage the built in methods in

//JavaScript. Thanks to Robert Kieffer for pointing this one out

UUID.returnBase = function(number, base) {

return (number).toString(base).toUpperCase();

};

//pick a random number within a range of numbers

//int b rand(int a); where 0 <= b <= a

UUID.rand = function(max) {

return Math.floor(Math.random() * (max + 1));

};

//变成全局函数

window.createUUID = function(type){

var _type = (typeof type == 'undefined' || (type!='l'&&type!='u')) ? 'l' : type;

if(_type=='u'){

return new UUID().toString();

}else {

return new UUID().toString().toLowerCase();

}

};

})(window);

三、PHP:

class System {

function currentTimeMillis() {

list ($usec, $sec) = explode(" ", microtime());

return $sec . substr($usec, 2, 3);

}

}

class NetAddress {

var $Name = 'localhost';

var $IP = '127.0.0.1';

function getLocalHost() // static

{

$address = new NetAddress();

$address->Name = $_ENV["COMPUTERNAME"];

$address->IP = $_SERVER["SERVER_ADDR"];

return $address;

}

function toString() {

return strtolower($this->Name . '/' . $this->IP);

}

}

class Random {

function nextLong() {

$tmp = rand(0, 1) ? '-' : '';

return $tmp . rand(1000, 9999) . rand(1000, 9999) . rand(1000, 9999) . rand(100, 999) . rand(100, 999);

}

}

// 三段

// 一段是微秒 一段是地址 一段是随机数

class Guid {

var $valueBeforeMD5;

var $valueAfterMD5;

function Guid() {

$this->getGuid();

}

//

function getGuid() {

$address = NetAddress :: getLocalHost();

$this->valueBeforeMD5 = $address->toString() . ':' . System :: currentTimeMillis() . ':' . Random :: nextLong();

$this->valueAfterMD5 = md5($this->valueBeforeMD5);

}

function newGuid() {

$Guid = new Guid();

return $Guid;

}

function toString() {

$raw = strtoupper($this->valueAfterMD5);

return substr($raw, 0, 8) . substr($raw, 8, 4) . substr($raw, 12, 4) . substr($raw, 16, 4) . substr($raw, 20);

}

}

//生成32位uuid

function createUUID(){

$Guid = new Guid();

$uuid = $Guid->toString();

return strtolower($uuid);

}

四、MySql:

SELECT UUID(); //带“-”

SELECT REPLACE(UUID(), '-', ''); //不带“-”

五、Oracle:

select sys_guid() from dual;//大写

select LOWER(sys_guid()) from dual;//小写

欢迎大家指正和补充!

转载地址:http://yhkfs.baihongyu.com/

你可能感兴趣的文章
Jquery的普通事件和on的委托事件
查看>>
IE低版本兼容的感悟
查看>>
ACE网络编程笔记(2):IPC SAP、ACE_SOCKET和TCP/IP通信实例
查看>>
关于递归
查看>>
数据库水平分表(一个大数据量的表)
查看>>
HDU 2829 四边形不等式优化
查看>>
(剑指Offer)面试题50:树中两个结点的最低公共祖先
查看>>
MySQL入门书籍和方法分享
查看>>
为什么数组是从0开始的
查看>>
爬虫之MongoDB
查看>>
PAT1043 BST 镜像管不了了
查看>>
动态规划——四边形优化
查看>>
使用 Charles 获取 https 的数据
查看>>
php递归读取目录
查看>>
js遍历json
查看>>
C++用法的学习心得
查看>>
信号简介
查看>>
显示调用析构函数潜在隐患分析
查看>>
如何写好函数
查看>>
vmware Converter Agent
查看>>