生活随笔
收集整理的這篇文章主要介紹了
Java批量生成数据库测试数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
第一列id自增,日期為一年內(nèi)所有日期,還有一列是隨機數(shù)
一次生成365條數(shù)據(jù)
由于控制臺緩沖區(qū)太小放不下所有語句,已將輸出重定向到文件
import java
.io
.BufferedOutputStream
;
import java
.io
.FileNotFoundException
;
import java
.io
.FileOutputStream
;
import java
.io
.PrintStream
;
import java
.text
.SimpleDateFormat
;
import java
.util
.ArrayList
;
import java
.util
.Calendar
;
import java
.util
.List
;public class Main {private static SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");public static List
<String> list
;public static void main(String
[] args
) throws FileNotFoundException
{list
= new ArrayList<>();int year
= 2021;int m
= 1;while (m
< 13) {int month
= m
;Calendar cal
= Calendar
.getInstance();cal
.clear();cal
.set(Calendar
.YEAR
, year
);cal
.set(Calendar
.MONTH
, month
- 1);cal
.set(Calendar
.DAY_OF_MONTH
, 1);list
.add(sdf
.format(cal
.getTime()));int count
= cal
.getActualMaximum(Calendar
.DAY_OF_MONTH
);for (int j
= 0; j
<= (count
- 2);) {cal
.add(Calendar
.DAY_OF_MONTH
, +1);j
++;
list
.add(sdf
.format(cal
.getTime()));}m
++;}PrintStream ps
= System
.out
;ps
= new PrintStream(new BufferedOutputStream(new FileOutputStream("print.txt")), true);System
.setOut(ps
);for (int i
= 0; i
< list
.size(); i
++) {System
.out
.println("INSERT INTO。。。。。`) VALUES ('"+ (i
+ 466)+ "', 'HC.SHF', '2019-10-09ceshi', '1', '2019-10-09,測試用例', '-1', '', '1', '', '1', '', '1', '', '1', ' ', '1', ' ', '2', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '1', '', '', '"+ (int) (10 + (Math
.random() * 30)) + "', '22', '-1', 'username', '" + list
.get(i
) + "');");}}
}
總結(jié)
以上是生活随笔為你收集整理的Java批量生成数据库测试数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。