星空网 > 软件开发 > Java

Java 读取txt文件,读取结果保存到数据库

需求:有一个很大的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(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)        \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文件,读取结果保存到数据库




原标题:Java 读取txt文件,读取结果保存到数据库

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

产品销往美国?以下是破解世界最大经济体的四种方法:https://www.kjdsnews.com/a/564709.html
颜值经济! 从美妆Top 3子类目看东南亚和拉美购物偏好+热销元素:https://www.kjdsnews.com/a/564710.html
封掉180多个店铺!冻结8520万!这家大卖只剩1成多收入?:https://www.kjdsnews.com/a/564711.html
中东五大热门电商平台:SHEIN荣登榜首!:https://www.kjdsnews.com/a/564712.html
亚马逊运营QA集锦(2031-2040):https://www.kjdsnews.com/a/564713.html
为什么说做东南亚电商离不开社交媒体:https://www.kjdsnews.com/a/564714.html
雅鹿与时代赛跑52年,奔向品牌生态化丨亿邦智库 :https://www.kjdsnews.com/a/1836560.html
淘宝给商家发了个“大红包” :https://www.kjdsnews.com/a/1836561.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流