日期:2014-05-16  浏览次数:20486 次

addBatch()和executeBatch()实现数据的批处理插入
import java.io.FileReader;   
import java.io.BufferedReader;   
import java.io.FileNotFoundException;   
import java.io.IOException;   
import java.sql.Connection;   
import java.sql.DriverManager;   
import java.sql.SQLException;   
import java.sql.Statement;   
  
import javax.naming.InitialContext;   
import javax.naming.NamingException;   
import javax.sql.DataSource;   
  
public class InertDB_batch {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        // TODO Auto-generated method stub   
        System.out.println("***************");   
        String buf = null;   
        BufferedReader in = null;   
        Statement stmt =null;   
        Connection con =null;   
        try{   
                Class.forName("com.mysql.jdbc.Driver");   
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=gb2312", "root", "root");   
                stmt = con.createStatement();   
        }catch(Exception e){   
            e.printStackTrace();   
        }   
        try {   
            in = new BufferedReader(new FileReader("insert.sql"));   
            buf = in.readLine();   
            System.out.println(buf);   
        } catch (FileNotFoundException e) {   
            new Exception("insert.sql File not exists!");   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
           
        while (buf != null) {   
            System.out.println(buf);   
            try {   
                  
                //将给定的 SQL 命令添加到此 Statement 对象的当前命令列