zoukankan      html  css  js  c++  java
  • Java 读取txt文件,读取结果保存到数据库

    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(10);
                }
            } 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)                ");
                sqlBf.append("          VALUES(?                                                    ");
                sqlBf.append("               , ?                                                    ");
                sqlBf.append("               , ?)                                                   ");
                
                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");
        }
    }

  • 相关阅读:
    linux JAVA JDK环境配置
    逍遥语录
    php常用函数集
    使用uGUI系统玩转标准俄罗斯方块
    Unity3D中uGUI事件系统简述及使用方法总结
    JAVA笔记-如何将百万级数据高效的导出到Excel表单
    简单的异步Socket实现——SimpleSocket_V1.1
    Netty4.x中文教程系列(六) 从头开始Bootstrap
    Unity3D中简单的C#异步Socket实现
    基于Spring框架的简单多数据源切换解决办法
  • 原文地址:https://www.cnblogs.com/likeju/p/4882886.html
Copyright © 2011-2022 走看看