import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertDataDemo {
static Connection conn = null;
public static void initConn() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/wyy_moxibustion_server?"
+ "user=root&password=HCiot151231&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC";
try {
// Dynamically load mysql driver
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Successfully loaded MySQL driver");
conn = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String randomStr(int size) {
// Define an empty string
String result = "";
for (int i = 0; i < size; ++i) {
// Generate an int type integer between 97 ~ 122
int intVal = (int) (Math.random() * 26 + 97);
// Force conversion (char) intVal Convert the corresponding value to the corresponding character, and splicing the characters
result = result + (char) intVal;
}
// Output string
return result;
}
public static void insert(int insertNum) {
// open time
Long begin = System.currentTimeMillis();
System.out.println("Start Inserting Data...");
// sql prefix
String prefix = "INSERT INTO device_connection_log (id, device_id, host, connection_time,is_certified,create_time,update_time) VALUES ";
try {
// save the sql suffix
StringBuffer suffix = new StringBuffer();
// Set the transaction to non-automatic commit
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement("");
for (int i = 1; i <= insertNum; i++) {
// Build sql suffix
suffix.append("(" + i + ", 1, '127.0.0.1', SYSDATE(), 1, SYSDATE(), SYSDATE()),");
}
// Build a complete sql
String sql = prefix + suffix.substring(0, suffix.length() - 1);
// Add execution sql
pst.addBatch(sql);
// perform the operation
pst.executeBatch();
// commit the transaction
conn.commit();
// close the connection
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// End Time
Long end = System.currentTimeMillis();
System.out.println("insert"+insertNum+" data data is completed!");
System.out.println("Time-consuming : " + (end - begin) / 1000 + "seconds");
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initConn();
insert(1000000);
}
}
Mysql使用java代码快速插入100W数据
最新推荐文章于 2024-07-03 14:57:11 发布