java生成12位随机数_Java随机字符串生成(12位)

Java随机字符串生成 , 生成12位的随机字符串,同时插入到数据库,一天可以生成一千万个。

1.[代码][Java]代码

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package uuidtest;

import java.sql.*;

import java.util.Random;

import java.util.UUID;

/**

*

* @author Abdiweli

*/

public class MyUUID extends Thread{

private String ThreadName = "";

public MyUUID(String _threadName){

this.ThreadName = _threadName;

}

public void run(){

System.out.println(this.ThreadName + ">Started ...");

// 随机数生成数量

double max = 1000000;

// 数据库连接配置

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://127.0.0.1:3306/barcode";

String user = "root";

String password = "1234";

try {

Class.forName(driver);

Connection conn = DriverManager.getConnection(url, user, password);

if(!conn.isClosed())

{

System.out.println(this.ThreadName + ">Succeeded connecting to the Database!");

}

Statement statement = conn.createStatement();

SHA1 sha1 = new SHA1();

//base64 BASE64 = new base64();

timer ti = new timer();

ti.start();

double i = 1;

int rs;

for( ; i <= max ; ++i)

{

UUID uuid = UUID.randomUUID();

String guid = uuid.toString().replace("-", "").toUpperCase();

Random random = new Random(System. currentTimeMillis());

int k = random.nextInt();

int start = Math.abs(k % 32);

guid = MD5.getMD5(guid.getBytes());

guid = sha1.getDigestOfString(guid.getBytes());

guid = it.sauronsoftware.base64.Base64.encode(guid);

guid = guid.substring(start, start + 12);

guid = guid.toUpperCase();

String sql = "INSERT INTO barcode.GUID2 (GUID) VALUE('" + guid + "')";

rs = statement.executeUpdate(sql);

}

ti.stop();

System.out.println(this.ThreadName + "> Total Time :" + ti.getTime() + " Second !" + " Count :" + (int)(i-1));

}

catch(ClassNotFoundException e)

{

System.out.println(this.ThreadName + ">Sorry,can`t find the Driver!");

//e.printStackTrace();

}

catch(SQLException e)

{

System.out.println(this.ThreadName + ">\nSQLException ! \nMessage :" + e.getMessage());

//e.printStackTrace();

}

catch(Exception e)

{

System.out.println(this.ThreadName + ">\nException ! \nMessage :" + e.getMessage());

//e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值