JavaEE 数据库随机值插入测试

  1 package com.jery.javaee.dbtest;
  2 import java.sql.Connection;
  3 import java.sql.DriverManager;
  4 import java.sql.PreparedStatement;
  5 import java.sql.SQLException;
  6 import java.util.Random;
  7 import com.jery.javaee.utils.UUIDUtils;
  8 
  9 public class UserTest {
 10     Connection conn = null;
 11     String sql = "";
 12     PreparedStatement pstmt = null;
 13     int target=0;
 14     public UserTest() {
 15         super();
 16         initial();
 17     }
 18 
 19     public void initial() {
 20         // 1. 加载驱动
 21         try {
 22             Class.forName("com.mysql.jdbc.Driver");
 23         } catch (ClassNotFoundException e) {
 24             e.printStackTrace();
 25             System.out.println("加载驱动失败"a);
 26         }
 27         // 2. 创建连接
 28         try {
 29             conn = DriverManager.getConnection("jdbc:mysql:///openeyes", "root", "root");
 30         } catch (SQLException e) {
 31             System.out.println("创建连接失败");
 32             e.printStackTrace();
 33         }
 34     }
 35     public void close() {
 36         try {
 37             if (pstmt != null)
 38                 pstmt.close();
 39             if (conn != null)
 40                 conn.close();
 41         } catch (Exception e) {
 42             e.printStackTrace();
 43         }
 44     }
 45     public int insertUser(int userid,String username,String password,String gender,
 46             int age,String description,String email,int identify,int fsNum,int gzNum) {
 47         sql = "INSERT INTO user(userid,username,password,gender,age,description,email,identify,fsNum,gzNum,ucreatetime) VALUES(?,?,?,?,?,?,?,?,?,?,NOW())";
 48         try {
 49             pstmt = conn.prepareStatement(sql);
 50             pstmt.setInt(1, userid);
 51             pstmt.setString(2,username);
 52             pstmt.setString(3,password);
 53             pstmt.setString(4,gender);
 54             pstmt.setInt(5, age);
 55             pstmt.setString(6,description);
 56             pstmt.setString(7,email);
 57             pstmt.setInt(8,identify);
 58             pstmt.setInt(9,fsNum);
 59             pstmt.setInt(10,gzNum);
 60             
 61             pstmt.executeUpdate();
 62         } catch (SQLException e) {
 63             System.out.println("数据库操作异常");
 64             e.printStackTrace();
 65         }finally{
 66             close();
 67         }
 68         return target;
 69     }
 70     
 71     
 72     public static void main(String[] args) {
 73         
 74         UserTest test =null;
 75         
 76         //循环插入数据
 77         for (int z = 0; z < 100; z++) {
 78             test= new UserTest();
 79             
 80             /***
 81              * 随机userid1
 82              */
 83             int userid = UUIDUtils.getNumUUID();//永不重复的UUID,此类详细在最后
 84             
 85             /***
 86              * 随机username2
 87              */
 88             Random random = new Random();
 89             String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
 90                     "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
 91                     "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "殷",
 92                     "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和",
 93                     "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒",
 94                     "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"};
 95             String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯悦昭冰爽琬茗羽希宁欣飘育滢筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
 96             String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄钧冠策腾楠榕风航弘";
 97             int index = random.nextInt(Surname.length - 1);
 98             String username = Surname[index]; //获得一个随机的姓氏
 99             int i = random.nextInt(3);//可以根据这个数设置产生的男女比例
100             if(i==2){
101                 int j = random.nextInt(girl.length()-2);
102                 if (j % 2 == 0) {
103                     username = username + girl.substring(j, j + 2);
104                 } else {
105                     username = username + girl.substring(j, j + 1);
106                 }
107             }
108             else{
109                 int j = random.nextInt(girl.length()-2);
110                 if (j % 2 == 0) {
111                     username = username + boy.substring(j, j + 2);
112                 } else {
113                     username = username + boy.substring(j, j + 1);
114                 }
115             }
116             
117             /***
118              * 随机password3
119              */
120             String str = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*.";
121             int randomNum;
122             char passchar;
123             StringBuffer passwordbuffer = new StringBuffer();
124              
125                 for (int j = 0; j < 9; j++) {
126                     // 可生成[0,n)之间的整数,获得随机位置
127                     randomNum = random.nextInt(str.length());
128                     // 获得随机位置对应的字符
129                     passchar = str.charAt(randomNum);
130                     // 组成一个随机字符串
131                     passwordbuffer.append(passchar);
132                 }
133             String oldpassword=passwordbuffer.toString();
134             String password = MD5Utils.toMd5(bpassword);//密码采用MD5加密,此类方法在最下面
135             /***
136              * 随机性别4
137              */
138             String[] sex = { "男", "女" };
139             int sexIndex = 0;
140             sexIndex = random.nextInt(2);
141             String gender = sex[sexIndex];
142             
143             /***
144              * 随机年龄5
145              */
146             int age = 0;
147             age = random.nextInt(60) + 1;
148             
149             /***
150              * 随机个性签名6
151              */
152             String description = "开眼是最棒的";
153             
154             /***
155              * 随机邮箱7
156              */
157             String nemail = "0123456789";
158             StringBuffer sb = new StringBuffer();
159             for (int l = 0; l < 9; l++) {
160                 int number = (int) (Math.random() * nemail.length());
161                 sb.append(nemail.charAt(number));
162             }
163             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"};
164             int sindex = 0;
165             sindex = random.nextInt(11);
166             String ssemail = stremail[sindex];
167             
168             String email = sb.toString()+ssemail;
169             
170             /***
171              * 随机身份8
172              */
173             int[] ident = {0, 1};
174             int a = 0;
175             a = random.nextInt(2);
176             int identify = ident[a];
177             
178             /***
179              * 随机粉丝数9
180              */
181             int fsNum = 0;
182             fsNum = random.nextInt(9999) + 1;
183             
184             /***
185              * 随机关注数10
186              */
187             int gzNum = 0;
188             gzNum = random.nextInt(9999) + 1;
189             
190             test.insertUser(userid, username, password, gender, age, description, email, identify, fsNum, gzNum);
191         }
192         System.out.println("成功插入100条数据!");
193     }
194 
195 }

永不重复的UUID,类如下:

 

 1 package com.jery.javaee.utils;
 2 
 3 import java.util.UUID;
 4 
 5 public class UUIDUtils {
 6 
 7     public static void main(String[] args) {
 8         UUID uuid = UUID.randomUUID();
 9         String id = uuid.toString();
10         System.out.println(id.hashCode());
11     }
12     public static String getUUID() {
13         UUID uuid = UUID.randomUUID();
14         return uuid.toString().replace("-", "");
15     }
16     public static int getNumUUID() {
17         UUID uuid = UUID.randomUUID();
18         int num = uuid.toString().replace("-", "").hashCode();
19         return num > 0 ? num : -num;
20     }
21 }

 

 

 

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

 1 package com.jery.javaee.utils;
 2 
 3 import org.apache.commons.codec.digest.DigestUtils;
 4 
 5 public class MD5Utils {
 6 
 7     public static void main(String[] args) {
 8         String pass = DigestUtils.md5Hex("");
 9         System.out.println(pass);
10     }
11     
12     public static String toMd5(String pass) {
13         return DigestUtils.md5Hex(pass);
14     }
15 }

 

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

posted @ 2018-12-19 15:30  铃铃铃  阅读(251)  评论(0编辑  收藏  举报