-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodLoader.java
More file actions
147 lines (126 loc) · 4.3 KB
/
GoodLoader.java
File metadata and controls
147 lines (126 loc) · 4.3 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.Properties;
import java.sql.*;
import java.net.URL;
public class GoodLoader {
private static final int BATCH_SIZE = 500;//用于批处理
private static URL propertyURL = GoodLoader.class
.getResource("/loader.cnf");
private static Connection con = null;
private static PreparedStatement stmt = null;
private static boolean verbose = false;
private static void openDB(String host, String dbname,
String user, String pwd) {
try {
//
Class.forName("org.postgresql.Driver");
} catch (Exception e) {
System.err.println("Cannot find the Postgres driver. Check CLASSPATH.");
System.exit(1);
}
String url = "jdbc:postgresql://" + host + "/" + dbname;
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", pwd);
try {
con = DriverManager.getConnection(url, props);
if (verbose) {
System.out.println("Successfully connected to the database "
+ dbname + " as " + user);
}
con.setAutoCommit(false);
} catch (SQLException e) {
System.err.println("Database connection failed");
System.err.println(e.getMessage());
System.exit(1);
}
try {
/**
* here need reconsideration
*/
stmt = con.prepareStatement("insert into users(login,id, name)"
+ " values(?,?,?)");
} catch (SQLException e) {
System.err.println("Insert statement failed");
System.err.println(e.getMessage());
closeDB();
System.exit(1);
}
}
private static void closeDB() {
if (con != null) {
try {
if (stmt != null) {
stmt.close();
}
con.close();
con = null;
} catch (Exception e) {
// Forget about it
}
}
}
private static void loadData(String login, Long id, String name)
throws SQLException {
if (con != null) {
stmt.setString(1, login);
stmt.setLong(2, id);
stmt.setString(2, name);
stmt.addBatch();
}
}
public static void ImportUsersInfo(List<User> users) throws SQLException {
if (propertyURL == null) {
System.err.println("No configuration file (loader.cnf) found");
System.exit(1);
}
Properties defprop = new Properties();
defprop.put("host", "localhost");
defprop.put("user", "postgres");
defprop.put("password", "123456");
defprop.put("database", "postgres");
Properties prop = new Properties(defprop);
long start;
long end;
int cnt = 0;
// Empty target table
openDB(prop.getProperty("host"), prop.getProperty("database"),
prop.getProperty("user"), prop.getProperty("password"));
Statement stmt0;
if (con != null) {
stmt0 = con.createStatement();
stmt0.execute("truncate table students");
stmt0.close();
}
closeDB();
//
start = System.currentTimeMillis();
openDB(prop.getProperty("host"), prop.getProperty("database"),
prop.getProperty("user"), prop.getProperty("password"));
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
loadData(user.login, user.id, user.name);
cnt++;
}
if (cnt % BATCH_SIZE == 0) {
stmt.executeBatch();
stmt.clearBatch();
}
if (cnt % BATCH_SIZE != 0) {
stmt.executeBatch();
}
con.commit();
stmt.close();
closeDB();
end = System.currentTimeMillis();
System.out.println(cnt + " records successfully loaded");
System.out.println("Loading speed : "
+ (cnt * 1000) / (end - start)
+ " records/s");
closeDB();
}
}