75.? ? ? ? int i = 0;?
76.? ? ? ? while (i < MAX) {?
77.? ? ? ? ? ? pt.setLong(1, 1 + (int) (Math.random() * 100000000));?
78.? ? ? ? ? ? pt.setLong(2, 1 + (int) (Math.random() * 100000000));?
79.? ? ? ? ? ? pt.setString(3, RandomStringUtils.randomAscii(200));?
80.? ? ? ? ? ? pt.setInt(4, 1);?
81.? ? ? ? ? ? pt.setInt(5, 1);?
82.? ? ? ? ? ? pt.setInt(6, 1);?
83.? ? ? ? ? ? pt.setInt(7, 1);?
84.? ? ? ? ? ? pt.addBatch();?
85.? ? ? ? ? ? if (i % batchSize == 1) {?
86.? ? ? ? ? ? ? ? pt.executeBatch();?
87.? ? ? ? ? ? ? ? con.commit();?
88.? ? ? ? ? ? }?
89.? ? ? ? ? ? i++;?
90.? ? ? ? }?
91.? ? ? ? pt.executeBatch();?
92.? ? ? ? con.commit();?
93.? ? ? ? con.close();?
94.? ? }?
95.? ?
96.? ? private static void testLoadFile(int batchSize)?
97.? ? ? ? ? ? throws ClassNotFoundException, SQLException, UnsupportedEncodingException {?
98.? ? ? ? String fieldsterminated = "\t\t";?
99.? ? ? ? String linesterminated = "\t\r\n";?
100.? ? ? ? String loadDataSql = "LOAD DATA LOCAL INFILE 'sql.csv' INTO TABLE chat_message FIELDS TERMINATED BY '"?
101.? ? ? ? ? ? ? ? + fieldsterminated + "'? LINES TERMINATED BY '" + linesterminated?
102.? ? ? ? ? ? ? ? + "' (src_userid,target_userid,message,s1,s2,s3,s4) ";?
103.? ? ? ? Connection con = getConnection();?
104.? ? ? ? con.setAutoCommit(false);?
105.? ? ? ? PreparedStatement pt = con.prepareStatement(loadDataSql);?
106.? ? ? ? com.mysql.jdbc.PreparedStatement mysqlStatement = null;?
107.? ? ? ? if (pt.isWrapperFor(com.mysql.jdbc.Statement.class)) {?
108.? ? ? ? ? ? mysqlStatement = pt.unwrap(com.mysql.jdbc.PreparedStatement.class);?
109.? ? ? ? }?
110.? ?
111.? ? ? ? int i = 0;?
112.? ? ? ? StringBuilder sb = new StringBuilder(10000);?
114.? ? ? ? ? ? sb.append(1 + (int) (Math.random() * 100000000));?
115.? ? ? ? ? ? sb.append(fieldsterminated);?
116.? ? ? ? ? ? sb.append(1 + (int) (Math.random() * 100000000));?
117.? ? ? ? ? ? sb.append(fieldsterminated);?
118.? ? ? ? ? ? sb.append(RandomStringUtils.randomAscii(200).replaceAll("\\\\", " "));?
119.? ? ? ? ? ? sb.append(fieldsterminated);?
120.? ? ? ? ? ? sb.append(1);?
121.? ? ? ? ? ? sb.append(fieldsterminated);?
122.? ? ? ? ? ? sb.append(1);?
123.? ? ? ? ? ? sb.append(fieldsterminated);?
124.? ? ? ? ? ? sb.append(1);?
125.? ? ? ? ? ? sb.append(fieldsterminated);?
126.? ? ? ? ? ? sb.append(1);?
127.? ? ? ? ? ? sb.append(linesterminated);?
128.? ? ? ? ? ? if (i % batchSize == 1) {?
129.? ? ? ? ? ? ? ? byte[] bytes = sb.toString().getBytes();?
130.? ? ? ? ? ? ? ? InputStream in = new ByteArrayInputStream(bytes);?
131.? ? ? ? ? ? ? ? mysqlStatement.setLocalInfileInputStream(in);?
132.? ? ? ? ? ? ? ? mysqlStatement.executeUpdate();?
133.? ? ? ? ? ? ? ? con.commit();?
134.? ? ? ? ? ? ? ? sb = new StringBuilder(10000);?
135.? ? ? ? ? ? }?
136.? ?
137.? ? ? ? ? ? i++;?
138.? ? ? ? }?
139.? ? ? ? byte[] bytes = sb.toString().getBytes();?
140.? ? ? ? InputStream in = new ByteArrayInputStream(bytes);?
141.? ? ? ? mysqlStatement.setLocalInfileInputStream(in);?
142.? ? ? ? mysqlStatement.executeUpdate();?
143.? ? ? ? con.commit();?
144.? ?
145.? ? ? ? con.close();?
146.? ? }?
147. }?
测试结果:
?