gpt4 book ai didi

java - 在 MySQL 中写入时间

转载 作者:行者123 更新时间:2023-11-29 02:20:35 25 4
gpt4 key购买 nike

我正在使用 Java 代码为 6000 个条目随机创建图钉。目前写入数据库需要 3 分 27 秒。我想知道是否可以减少写入数据库的时间

数据库有一个 auto_increment 字段和一个 pin 字段,它是一个字符串。

public class DonkeyInsert {

/**
* @param args the command line arguments
* @throws java.sql.SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO code application logic here
Connection conn = new DBConnection().connect();
for (int i = 1; i <= 6000; i++) {
Random rand = new Random();
// create a Statement from the connection
Statement statement = conn.createStatement();
// insert the data
int k = rand.nextInt(Integer.SIZE - 1);
k = (k + 1) * 9999;
String pin = Integer.toString(k);
try {
String sql = "INSERT INTO login (login_id,pin) VALUES (" + i + "," + pin + ");";
statement.executeUpdate(sql);
} catch (SQLException se) {
System.out.println(se);
}

}
}

}

最佳答案

试试这个,看看它是否更快。我所做的更改应该有所帮助:

package persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

/**
* DonkeyInsert changes
* @author Michael
* @link https://stackoverflow.com/questions/32551895/write-time-in-mysql?noredirect=1#comment52959887_32551895
* @since 9/13/2015 12:49 PM
*/
public class DonkeyInsert {

public static final int DEFAULT__RECORD_COUNT = 6000;

private Random random;

public DonkeyInsert() {
this.random = new Random();
}

public DonkeyInsert(long seed) {
this.random = new Random(seed);
}

public static void main(String[] args) {
// I'd externalize these so I could change the database without recompiling.
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://hostname:3306/dbname";
String username = "username";
String password = "password";
Connection connection = null;
try {
DonkeyInsert donkeyInsert = new DonkeyInsert();
connection = createConnection(driver, url, username, password);
int numRowsInserted = donkeyInsert.insertPins(connection, DEFAULT__RECORD_COUNT);
System.out.println(String.format("# pins inserted: %d", numRowsInserted));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(connection);
}
}

private static final String INSERT_SQL = "INSERT INTO login(pin) VALUES(?) ";

public int insertPins(Connection connection, int count) {
int numInserted = 0;
if (count > 0) {
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(INSERT_SQL);
for (int i = 0; i < count; ++i) {
ps.setString(1, this.createRandomPin());
ps.addBatch();
}
int [] counts = ps.executeBatch();
for (int rowCount : counts) {
numInserted += rowCount;
}
} catch (SQLException e) {
throw new RuntimeException("SQL exception caught while inserting pins", e);
} finally {
close(ps);
}
}
return numInserted;
}

public String createRandomPin() {
// Changed it a bit. Didn't understand your requirement.
int k = this.random.nextInt(Integer.MAX_VALUE);
return Integer.toString(k);
}



public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
Class.forName(driver);
if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0)) {
return DriverManager.getConnection(url);
} else {
return DriverManager.getConnection(url, username, password);
}
}


public static void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}


public static void close(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

关于java - 在 MySQL 中写入时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com