码迷,mamicode.com
首页 > 数据库 > 详细

Java 读取txt文件后把数据保存到数据库中

时间:2015-10-14 11:54:29      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

需求:有一个很大的txt文件(1,000,000条数据),从txt中读取有用数据库后保存到Oracle数据库中

 

利用Java实现:

1、读取文件

2、数据库连接后插入到数据库

package com.test.IF.service;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

public class ReadCustomerFile {
    
    int idx;
    Connection conn = null;
    PreparedStatement pstmt = null;    
    
    // 使用commons-io.jar包的FileUtils的类进行读取
    public void readTxtFileByFileUtils(String fileName) {
        File file = new File(fileName);
        
        dbConnection();
        
        try {
            LineIterator lineIterator = FileUtils.lineIterator(file, "GB2312");
            while (lineIterator.hasNext()) {
                String line = lineIterator.nextLine();
                
                // 行数据转换成数组
                String[] custArray = line.split("\\|");
                insertCustInfo(custArray);
                Thread.sleep(100);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            dbDisConnection();
        }
    }
    
    // 插入到数据库中
    public void insertCustInfo(String[] strArray) {         
        try {
            StringBuffer sqlBf = new StringBuffer();
            sqlBf.setLength(0);
            
            sqlBf.append("INSERT INTO TEMP_CUST_INFO(CUST_NO, CUST_NM, MOB_NO1)                \n");
            sqlBf.append("          VALUES(?                                                    \n");
            sqlBf.append("               , ?                                                    \n");
            sqlBf.append("               , ?)                                                   \n");
            
            pstmt = conn.prepareStatement(sqlBf.toString());
            idx = 1;
            pstmt.clearParameters();
            pstmt.setInt(idx++, Integer.parseInt(strArray[0]));
            pstmt.setString(idx++, strArray[1]);
            pstmt.setString(idx++, strArray[2]);
            
            pstmt.executeUpdate();            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    // 连接数据库
    public Connection dbConnection() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            
            String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
            String user = "scott";  
            String password = "goodluck"; 
            
            conn = DriverManager.getConnection(url, user, password);    
            System.out.println("Connection 开启!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    
    // 关闭数据库
    public void dbDisConnection() {
        if (conn != null) {
            try {
                conn.close();
                System.out.println("Connection 关闭!");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        ReadCustomerFile rcf = new ReadCustomerFile();
        rcf.readTxtFileByFileUtils("D:\\test\\customer_info.txt");
    }
}

 

确认数据库数据:

技术分享

Java 读取txt文件后把数据保存到数据库中

标签:

原文地址:http://www.cnblogs.com/seabird1979/p/4876682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!