JDBC批量上传简单demo

JAVA修炼塔,技术世界的探知与交流,欢迎你的加入-----群号:535296702

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class batchInsert {
public Connection getConnection(){
    Connection conn=null;
    /**mysql*/
    String driver="com.mysql.jdbc.Driver";
    String connectdb="数据库url连接地址(输入你自己的替换掉这些文字)";
    String user="用户名";
    String password="密码";
    try {
        Class.forName(driver);
        try {
            conn=DriverManager.getConnection(connectdb,user,password);
            System.out.println("数据库驱动成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return conn;
}
public static void main(String[] args) {
    batchInsert batchin=new batchInsert();
    //rt.getConnection();
    batchin.AhPhoneInsert("这里是要解析的txt文件的地址");
}
//数据记录导入 
    public boolean AhPhoneInsert(String file){ 
        try { 
            Connection con = getConnection(); 
            PreparedStatement ps = null,ps1=null,ps2=null;  
            ResultSet rs = null;   
            BufferedReader br = new BufferedReader(   
                    (new InputStreamReader(new FileInputStream(new File(file)),"utf-8")));// 编码方式为utf-8,txt保存时编码方式也要选择为utf-8  
            String line;   
            try {   
                String  sql ="insert into user(name) values(?)";   
                try { 
                    con.setAutoCommit(false);   
                ps = con.prepareStatement(sql);    
                int n=0;     
                while ((line = br.readLine()) != null) {   
                    String phone = line.trim();    
                    ps.setString(1, phone);    
                    ps.addBatch();  
                    n++;     
                    if(n>1000){ 
                        ps.executeBatch(); 
                        n=0;  
                        }   
                    }
                ps.executeBatch();  
                con.commit();    
                } catch (SQLException e) {  
                    e.printStackTrace();
                    }finally{  
                        if(null!=rs){  
                            try {  
                                rs.close();
                                } catch(SQLException e){ 
                                    e.printStackTrace();   
                                    }   
                            }    
                        if(null!=ps){   
                            try {
                                ps.close();   
                                } catch (SQLException e) {   
                                    e.printStackTrace();    
                                    } 
                            }   
                        if(null!=ps2){     
                            try {   
                                ps2.close();  
                                } catch (SQLException e) {  
                                    e.printStackTrace();  
                                    }  
                            }  
                        if(null!=ps1){ 
                            try {     
                                ps1.close();  
                                } catch (SQLException e) {    
                                    e.printStackTrace();    
                                    }    
                            }    
                        if(null!=con){  
                            try {      
                                con.close(); 
                                } catch (SQLException e) {    
                                    e.printStackTrace();    
                                    }    
                            }  
                        }   
                } catch (IOException e) { 
                    e.printStackTrace();  
                    }  
            } catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {  
      e.printStackTrace(); 
      }      
        return true;
        } 
}

 

posted @ 2016-12-31 15:15  烟雨观春柳  阅读(145)  评论(0)    收藏  举报