• 读取csv文件,写入oracle数据库


    /*
    * @(#)DataParse.java 2014年4月28日
    */

    package com.yihaodian.sa.doData;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.StringTokenizer;

    /**
    * <pre>
    * @author mashunran
    *
    * 功能:
    * 创建日期: 2014年4月28日
    * 修改人:
    * 修改说明:
    * 评审人:
    * </pre>
    */
    public class DataParse {
    public static void main(String[] args) throws SQLException {
    DataParse dc = new DataParse();
    Connection con = null;
    PreparedStatement pst = null;
    // Statement sm = null;
    String ip = "192.168.20.56";// db ip
    String port = "1521";//
    String sid = "bidev";
    String userName = "edw1_user";
    String passWord = "edw1_user";
    // sm = con.createStatement();
    DateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //str时间转化为yyyy-MM-dd HH:mm:ss格式date
    String sql = "insert into INF_PMS_OOS_DAILY(MRCHNT_ID,PROD_CODE,PM_INFO_ID,DATE_ID,IS_OOS,ETL_BATCH_ID,UPDT_TIME) values(?,?,?,?,?,?,?)";
    try {
    con = dc.getConnection(ip, port, sid, userName, passWord);

    File file = new File("E://data"); // CSV文件
    File[] files = file.listFiles();
    if (files != null) {
    pst = con.prepareStatement(sql);//连接数据库(上线299次)
    for (File f : files) {
    BufferedReader br = new BufferedReader(new FileReader(f));
    // 读取直到最后一行
    String line = "";
    int start = 0;
    while ((line = br.readLine()) != null) {
    start++;
    if (start == 1)
    continue; //从第二行开始读
    // 把一行数据分割成多个字段
    StringTokenizer st = new StringTokenizer(line, "");
    while (st.hasMoreTokens()) {
    // 每一行的多个字段用,隔开表示
    String a[] = st.nextToken().split(",");

    //给sql参数???赋值,?的序号从1开始,除此之外java的序号都从0开始
    pst.setLong(3, Long.parseLong(a[2].substring(1, a[2].length()-1)));//csv文件内容都是逗号分隔的带有双引号的字符串,需要先去掉引号,substring
    pst.setLong(1, Long.parseLong(a[0].substring(1, a[0].length()-1)));
    pst.setString(2, a[1].substring(1, a[1].length()-1));
    try {
    pst.setDate(4, new java.sql.Date(format.parse(a[3].substring(1, a[3].length()-1)).getTime()));//java.sql.Date与java.util.date互转
    } catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    pst.setLong(5, Long.parseLong(a[4].substring(1, a[4].length()-1)));
    pst.setLong(6, Long.parseLong(a[5].substring(1, a[5].length()-1)));
    try {
    pst.setDate(7, new java.sql.Date(format.parse(a[6].substring(1, a[6].length()-1)).getTime()));
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    pst.execute();
    System.out.println("读完一行");
    }
    }
    br.close();
    System.out.println("-------------------------读完一个文件---------------------------");
    }
    System.out.println("****************结束结束结束****************");
    }

    } catch (FileNotFoundException e) {
    // 捕获File对象生成时的异常
    e.printStackTrace();
    } catch (IOException e) {
    // 捕获BufferedReader对象关闭时的异常
    e.printStackTrace();
    } finally {
    if (pst != null) {
    pst.close();
    pst = null;
    }
    if (con != null) {
    con.close();
    con = null;
    }
    }
    }
    //连接oracle数据库
    public Connection getConnection(String ip, String port, String sid, String userName, String passWord) {
    String driverName = "oracle.jdbc.driver.OracleDriver";// 连接oracle驱动包,需要把驱动jar包 add to buildPath
    String dbUrl = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + sid + "";
    Connection conn = null;
    try {
    Class.forName(driverName);
    conn = DriverManager.getConnection(dbUrl, userName, passWord);
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    // Connection conn;
    return conn;
    }
    }

    格式太凌乱。。。

  • 相关阅读:
    git使用
    Git常用命令梳理
    git fetch 更新远程代码到本地仓库
    理解RESTful架构
    漫谈五种IO模型(主讲IO多路复用)
    python 单下划线/双下划线使用总结
    闰秒导致MySQL服务器的CPU sys过高
    闰秒问题
    Java线上应用故障排查之一:高CPU占用
    ZooKeeper安装与配置
  • 原文地址:https://www.cnblogs.com/msr1019jingzi/p/3699775.html
Copyright © 2020-2023  润新知