package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class TestInsert { public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db", "root", "123456"); // 关闭事务自动提交 con.setAutoCommit(false); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS"); TimeZone t = sdf.getTimeZone(); t.setRawOffset(0); sdf.setTimeZone(t); Long startTime = System.currentTimeMillis(); BufferedReader br = new BufferedReader(new FileReader("e://v100.ara")) ; List list = new ArrayList(); //放置读取的内容 String s; while( (s = br.readLine()) != null){ System.out.println(s.toString()); list.add(s); } PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into test values (?,?)"); for (int i = 0; i < list.size(); i++) { pst.setInt(1, i+1); pst.setString(2, list.get(i)); pst.addBatch(); } // 执行批量更新 pst.executeBatch(); // 语句执行完毕,提交本事务 con.commit(); Long endTime = System.currentTimeMillis(); System.out.println("用时:" + sdf.format(new Date(endTime - startTime))); pst.close(); con.close(); }}