- Public static void Insert () {
- //Open time
- Long begin = new Date (). GetTime ();
- //SQL prefix
- String prefix = "INSERT into Tb_big_data (count, Create_time, Random) VALUES";
- try {
- //Save SQL suffix
- StringBuffer suffix = new StringBuffer ();
- //SET transaction as non-auto-commit
- Conn.setautocommit (false);
- //Statement st = Conn.createstatement ();
- //Better than St,pst
- PreparedStatement PST = conn.preparestatement ("");
- //Outer loop, total number of commit transactions
- For (int i = 1; I <= ; i++) {
- //Time -to-commit step
- For (int j = 1; j <= 10000; j + +) {
- //Build SQL suffix
- Suffix.append ("(" + j * i + ", sysdate ()," + I * J
- * Math.random () + "),");
- }
- //Build Full SQL
- String sql = prefix + suffix.substring (0, Suffix.length ()- 1);
- //Add Execute SQL
- Pst.addbatch (SQL);
- //Perform actions
- Pst.executebatch ();
- //Commit a transaction
- Conn.commit ();
- //Clear the last added data
- suffix = new StringBuffer ();
- }
- //First-class connection
- Pst.close ();
- Conn.close ();
- } catch (SQLException e) {
- E.printstacktrace ();
- }
- //End time
- Long end = new Date (). GetTime ();
- //Time-consuming
- System.out.println ("Cast:" + (End-begin)/ + + "MS");
- }
Efficient forwarding of bulk data inserts