This article describes the method of Java implementing batch writing data to mysql. Share it for your reference, as follows:
private static String user = "root";private static String pass = "123456";private static String URL = "jdbc:mysql://192.168.1.116:3306/test";public static void main(String args[]) throws SQLException, ClassNotFoundException{ //Get time information SimpleDateFormat sfmt = new SimpleDateFormat("yyyyMMddHHmmss"); Date lastHour = new Date(new Date().getTime() - 3600000);//1 hour ago String hourMeta = sfmt.format(lastHour).substring(0, 10);//The timestamp of the hour is used to narrow the search range of big data int day = Integer.parseInt(sfmt.format(lastHour).substring(0, 8)); int hour = Integer.parseInt(String.valueOf(lastHour.getHours())); //Connect mysql database Connection conn = DriverManager.getConnection(URL, user, pass); conn.setAutoCommit(false); //Insert data into mysql String sql = "insert into test1 values(?,?)"; PreparedStatement ps = conn.prepareStatement(sql);//The object to execute the sql statement ps.setInt(1, day); ps.setInt(2, hour); //ps.setString(1, "aa"); //ps.setString(2, "bb"); ps.addBatch();//Add predefined parameters ps.executeBatch();//Execute batch execution conn.commit(); if (ps != null) { ps.close(); } if (conn != null) { conn.close(); }}For more information about Java-related content, please check out the topics of this site: "Java+MySQL database programming summary", "Java data structure and algorithm tutorial", "Java file and directory operation skills summary", "Java operation DOM node skills summary" and "Java cache operation skills summary"
I hope this article will be helpful to everyone's Java programming.