自动生成数据库测试数据
package com.sql;
import java.sql.*;
import java.util.*;
public class Sql {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
try {
Scanner Scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = Scanner.nextInt();
Connection connection = getConnection();
StringBuffer sql = new StringBuffer();
sql.append("insert into ").append(" ").append("paymenttwo");
StringBuffer lie = new StringBuffer();
lie.append("(");
StringBuffer values = new StringBuffer();
values.append("values");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("DESC paymenttwo");
Map map = new LinkedHashMap();
while (resultSet.next()) {
System.out.println(resultSet.getObject(1));
System.out.println(resultSet.getObject(2));
map.put(resultSet.getObject(1), resultSet.getObject(2));
}
lie = getKey(lie, map);
boolean execute = true;
if (num > 99999) {
int a = num / 100;
for (int i = 0; i < 100; i++) {
values = getType(values, map, a * i, a);
sql.append(lie).append(" ").append(values);
execute = statement.execute(sql.toString());
sql = new StringBuffer();
sql.append("insert into ").append(" ").append("paymenttwo");
lie = new StringBuffer();
lie.append("(");
lie = getKey(lie, map);
values = new StringBuffer();
values.append("values");
}
}
System.out.println(execute);
System.out.println(sql.toString());
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception {
String driverClassName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/db2019?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false";
String user = "";
String password = "";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(url, user, password);
return conn;
}
public static StringBuffer getType(StringBuffer str, Map map, int num, int a) {
Set<String> set = map.keySet();
for (int i = num+1; i <=num + a; i++) {
str.append("(");
for (String o : set) {
String s = (String) (map.get(o));
if (s.contains("varchar")) {
str.append("'").append("字符串" + i).append("'");
} else if (s.contains("bigint")) {
str.append(i);
}
str.append(",");
}
str = str.deleteCharAt(str.length() - 1);
str.append(")");
str.append(",");
}
str = str.deleteCharAt(str.length() - 1);
return str;
}
public static StringBuffer getKey(StringBuffer str, Map map) {
Set<String> set = map.keySet();
for (String o : set) {
str.append(o).append(",");
}
str = str.deleteCharAt(str.length() - 1);
str.append(")");
return str;
}
}