package com.jery.javaee.dbtest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
import com.jery.javaee.utils.UUIDUtils; public class UserTest {
Connection conn = null;
String sql = "";
PreparedStatement pstmt = null;
int target=0;
public UserTest() {
super();
initial();
} public void initial() {
// 1. 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("加载驱动失败"a);
}
// 2. 创建连接
try {
conn = DriverManager.getConnection("jdbc:mysql:///openeyes", "root", "root");
} catch (SQLException e) {
System.out.println("创建连接失败");
e.printStackTrace();
}
}
public void close() {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int insertUser(int userid,String username,String password,String gender,
int age,String description,String email,int identify,int fsNum,int gzNum) {
sql = "INSERT INTO user(userid,username,password,gender,age,description,email,identify,fsNum,gzNum,ucreatetime) VALUES(?,?,?,?,?,?,?,?,?,?,NOW())";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, userid);
pstmt.setString(2,username);
pstmt.setString(3,password);
pstmt.setString(4,gender);
pstmt.setInt(5, age);
pstmt.setString(6,description);
pstmt.setString(7,email);
pstmt.setInt(8,identify);
pstmt.setInt(9,fsNum);
pstmt.setInt(10,gzNum); pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("数据库操作异常");
e.printStackTrace();
}finally{
close();
}
return target;
} public static void main(String[] args) { UserTest test =null; //循环插入数据
for (int z = 0; z < 100; z++) {
test= new UserTest(); /***
* 随机userid1
*/
int userid = UUIDUtils.getNumUUID();//永不重复的UUID,此类详细在最后 /***
* 随机username2
*/
Random random = new Random();
String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
"何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
"鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "殷",
"罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和",
"穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒",
"屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"};
String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯悦昭冰爽琬茗羽希宁欣飘育滢筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄钧冠策腾楠榕风航弘";
int index = random.nextInt(Surname.length - 1);
String username = Surname[index]; //获得一个随机的姓氏
int i = random.nextInt(3);//可以根据这个数设置产生的男女比例
if(i==2){
int j = random.nextInt(girl.length()-2);
if (j % 2 == 0) {
username = username + girl.substring(j, j + 2);
} else {
username = username + girl.substring(j, j + 1);
}
}
else{
int j = random.nextInt(girl.length()-2);
if (j % 2 == 0) {
username = username + boy.substring(j, j + 2);
} else {
username = username + boy.substring(j, j + 1);
}
} /***
* 随机password3
*/
String str = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*.";
int randomNum;
char passchar;
StringBuffer passwordbuffer = new StringBuffer(); for (int j = 0; j < 9; j++) {
// 可生成[0,n)之间的整数,获得随机位置
randomNum = random.nextInt(str.length());
// 获得随机位置对应的字符
passchar = str.charAt(randomNum);
// 组成一个随机字符串
passwordbuffer.append(passchar);
}
String oldpassword=passwordbuffer.toString();
String password = MD5Utils.toMd5(bpassword);//密码采用MD5加密,此类方法在最下面
/***
* 随机性别4
*/
String[] sex = { "男", "女" };
int sexIndex = 0;
sexIndex = random.nextInt(2);
String gender = sex[sexIndex]; /***
* 随机年龄5
*/
int age = 0;
age = random.nextInt(60) + 1; /***
* 随机个性签名6
*/
String description = "开眼是最棒的"; /***
* 随机邮箱7
*/
String nemail = "0123456789";
StringBuffer sb = new StringBuffer();
for (int l = 0; l < 9; l++) {
int number = (int) (Math.random() * nemail.length());
sb.append(nemail.charAt(number));
}
String[] stremail = {"@gmail.com","@yahoo.com","@msn.com","@live.com","@qq.com","@163.com","@163.net","@googlemail.com","@sina.com","@sohu.com","@yahoo.com.cn"};
int sindex = 0;
sindex = random.nextInt(11);
String ssemail = stremail[sindex]; String email = sb.toString()+ssemail; /***
* 随机身份8
*/
int[] ident = {0, 1};
int a = 0;
a = random.nextInt(2);
int identify = ident[a]; /***
* 随机粉丝数9
*/
int fsNum = 0;
fsNum = random.nextInt(9999) + 1; /***
* 随机关注数10
*/
int gzNum = 0;
gzNum = random.nextInt(9999) + 1; test.insertUser(userid, username, password, gender, age, description, email, identify, fsNum, gzNum);
}
System.out.println("成功插入100条数据!");
} }

永不重复的UUID,类如下:

 package com.jery.javaee.utils;

 import java.util.UUID;

 public class UUIDUtils {

     public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
String id = uuid.toString();
System.out.println(id.hashCode());
}
public static String getUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString().replace("-", "");
}
public static int getNumUUID() {
UUID uuid = UUID.randomUUID();
int num = uuid.toString().replace("-", "").hashCode();
return num > 0 ? num : -num;
}
}

MD5加密明文密码,类如下:

 package com.jery.javaee.utils;

 import org.apache.commons.codec.digest.DigestUtils;

 public class MD5Utils {

     public static void main(String[] args) {
String pass = DigestUtils.md5Hex("");
System.out.println(pass);
} public static String toMd5(String pass) {
return DigestUtils.md5Hex(pass);
}
}

此例子中需要的UUID,MD5类包可以在网上下载,数据库插入了不同类型的测试数据,是为了方便进行JavaWeb开发,内容仅供参考,转载请注明出处。谢谢

最新文章

  1. atitit &#160;验证码理论与概览与&#160;验证码规范&#160;解决方案.docx
  2. MFC资源冲突解决方法
  3. linux上传下载
  4. 利用查询提示优化SQL
  5. “权限系统_基于HUI”的简单介绍和交流
  6. liunx命令1
  7. 基于pytorch实现word2vec
  8. 【转】Java中的新生代、老年代、永久代和各种GC
  9. Network Security final project---War Game
  10. 《C#与.NET程序员面试宝典》学习札记
  11. Electron桌面应用打包流程
  12. [转帖]一段关于Unix与 Linux的暗黑史
  13. 信息检索(IR)的评价指标介绍 - 准确率、召回率、F1、mAP、ROC、AUC
  14. 浏览器兼容java小程序配置说明
  15. [Ubuntu] fg、bg让你的进程在前后台之间切换
  16. 吴恩达 Deep learning 第二周 神经网络基础
  17. 【贪心算法】POJ-1328 区间问题
  18. 如何查找元素对应事件的js代码
  19. RedHot 不能联网 ifconfig命令只显示lo 不显示eth0的解决方法!
  20. 1014-C程序的语法树

热门文章

  1. django获取字段列表(values/values_list/flat)
  2. 第一次C语言作业:博客随笔
  3. JavaScript 组件编写
  4. WCF把书读薄(3)——数据契约、消息契约与错误契约
  5. [GO]给导入包起别名
  6. 第17章-Spring消息
  7. springboot调用微信的jscode2session报JSONObject异常
  8. 将以太坊封装为 ERC20
  9. 《C#多线程编程实战》1.10 lock关键字
  10. Ubuntu 14.10,准备C/C++的编译环境