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();
}
}
}