java造mysql假数据记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37786447/article/details/80707475

造假数据的方法:

尽量对数据库表每个字段写一个对应的方法,这样比较清晰。

方法一:获得车牌号,根据自己的需求控制字母和数字的比例。

	public static String getPlateNo(){
		String str1="ABCCCCCCCCCCCCCCCCCCCCCCCCDEFGHJKC";
        String str2="ABCDEFGHJKLMNPQRSTUVWXYZ01234567890123456789012345678901234567890123456789012345678901234567890123456789";
        Random random=new Random();
    
        StringBuffer sb=new StringBuffer();
        sb.append("闽");
        sb.append(str1.charAt(random.nextInt(33)));
        for(int i=0;i<5;i++){
          int number=random.nextInt(94);
          sb.append(str2.charAt(number));
        }
        return sb.toString();
    }

方法二:用数组存好自己的字段所要出现的数据,同一自己控制大致比例。

	public static String getCarBrand(){
		String brand="";
		Random rd = new Random();
		String[] strs = {"大众","别克","丰田","吉利","宝马","丰田","丰田","标志"};
		int num = rd.nextInt(strs.length);
		brand = strs[num];
		return brand;
	}

方法三:时间方法如下

	public static String getDatetime(){
		String datetime = null;
		Random rd = new Random();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long ts = new Date().getTime()+6000*rd.nextInt(60)+600000*rd.nextInt(35);
		return datetime = df.format(ts);
	}

大致可以控制时间为当前时间后几个小时的时间变化。这里具体控制为多少的区间,可以自己设置,还可以直接按天数直接增加,也可以减。


然后就是到数据库驱动包后:

public static Connection getConnection(){
         Connection conn = null;
         try {
         Class.forName("com.mysql.cj.jdbc.Driver");
         String url = "jdbc:mysql://192.168.17.213:3306/tc?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
         String user = "root";
         String pass = "root";
         conn = DriverManager.getConnection(url,user,pass);
         } catch (ClassNotFoundException e) {
         e.printStackTrace();
         } catch (SQLException e) {
         e.printStackTrace();
         }
         return conn;
         }
然后就是写main方法进行字符串的拼接和insert语句的提交。




猜你喜欢

转载自blog.csdn.net/m0_37786447/article/details/80707475
今日推荐