【问题标题】:How to a csv file in oracle using sql loader in java如何使用java中的sql loader在oracle中创建csv文件
【发布时间】:2014-11-02 11:30:36
【问题描述】:

我想将数据从 csv 文件加载到 oracle 数据库。这是我的代码-

void importData(Connection conn) {
    Statement stmt;
    String query;
    String filename = "C:/CSVData/Student.csv";

    try {
        stmt = conn.createStatement(
                ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        query = "LOAD DATA INFILE '" + filename + "' INTO TABLE Student FIELDS terminated by ',' ;";
        System.out.println(query);
        stmt.executeQuery(query);

    } catch (Exception e) {
        e.printStackTrace();
        stmt = null;
    }
}

此代码运行完美,并在 mysql 中加载数据。但现在我想在 oracle 中加载数据。我必须在查询中做出什么改变。请帮我。提前谢谢你...

【问题讨论】:

  • 下周要加载到postgresql怎么办?还是德比?还是h2?或者...请使用标准 SQL 来处理此类琐碎的事情。

标签: java mysql oracle csv sql-loader


【解决方案1】:

首先,你需要编写一个控制文件。

控制文件示例仅供参考:

Load data                   
infile "D:/Viki/test.CSV"                                       --the input file(s) you need to import
truncate                                                                        --the option you need do. (truncate, append, insert, replace. insert by default)
into table vk_recon_China_201409_i                  --table need insert to
fields terminated by ","                                        --
trailing nullcols
(
    col_a         filler
  , col_b          "Trim(:col_b)"
  , col_c          "To_Date(:col_c,'yyyy/mm/dd hh24:mi:ss')"
  , seqno                sequence(Max,1)
)

然后,通过Runtime.execProcessImpl.start调用sqlldr命令,

public  void startUp() {
    StringBuffer sb = new StringBuffer();
    String path = "sqlldr user/password@sid readsize=10485760 bindsize=10485760 rows=1000 control=controlFileName.ctl log=controlFileName.log direct=true \n pause";   
    try {
        Process pro = Runtime.getRuntime().exec(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()), 4096);
        String line = null;
        int i = 0;
        while ((line = br.readLine()) != null) {
            if (0 != i)
                sb.append("\r\n");
            i++;
            sb.append(line);
        }
    } catch (Exception e) {
        sb.append(e.getMessage());
    }
}

【讨论】:

    【解决方案2】:

    尝试创建external table。您可以使用 ORACLE_LOADER 驱动程序在 CSV 文件上创建一个外部表,然后使用 DML(例如 MERGE)使用外部表中的数据更新现有表。

    【讨论】:

      【解决方案3】:

      我认为下面的查询应该可以工作。

      query = "LOAD DATA INFILE '" + filename + "' APPEND INTO TABLE Student FIELDS terminated by ',' ;";
      

      更多信息:-

      http://docs.oracle.com/cd/E11882_01/server.112/e16536/ldr_control_file.htm#SUTIL005

      【讨论】:

      • 你使用的是哪个oracle版本?
      • 我正在使用 oracle 10g
      • 这是 MySQL;不是甲骨文
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2015-10-19
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      相关资源
      最近更新 更多