java - 在 MySQL 中写入时间

标签 java mysql sql

我正在使用 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/

相关文章:

mysql - 在 SQL 中将百分比添加到数字

具有 Entity Framework 的 SQL Server xml 列 - 如何保留无关紧要的空格

Java FX 橡皮筋调整大小错误

java - 计算两个 UML 模型之间的差异

mysql - 如果第一个查询返回 NULL,则从第二个查询返回默认数据

mysql - 将带有子查询的 mySQL 查询转换为带有子查询的 DB2 SQL 查询

sql - 如何使用列中的不同值作为新表的列名创建表?

c# - 确保仅 "Reasonable"查询

java - 通过递归调用确定泛型类型

Java - 从 "http://www.example.com/something.php?id=1111"获取文件名