-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertDataDemo.java
More file actions
50 lines (46 loc) · 1.64 KB
/
Copy pathInsertDataDemo.java
File metadata and controls
50 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import static java.util.Calendar.MINUTE;
public class InsertDataDemo {
public static void main(String[] args) {
Connection connection = null;
Statement stmt = null;
try
{
Class.forName("org.mariadb.jdbc.Driver");
connection = DriverManager
.getConnection("jdbc:mariadb://localhost:3306/test1", "root", "");
stmt = connection.createStatement();
SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
stmt.execute("delete from impressions");
int n = 1;
Random rand = new Random();
Calendar calendar = Calendar.getInstance();
calendar.set(2019,6,27,0,0,0);
for(int i=1; i<=1000; i++) {
Date time = calendar.getTime();
int offerId = Math.abs(rand.nextInt(2));
stmt.execute("INSERT INTO impressions (created_at,request_id, offer_id, creative_id ) "
+ "VALUES ('" + sdf.format(time) + "',"+n+","+ offerId+",1)");
calendar.add(MINUTE,Math.abs(rand.nextInt(6)));
n++;
}
}
catch (Exception e) {
e.printStackTrace();
}finally {
try {
stmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}