我想将数据从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中加载数据。我必须在查询中做出什么改变。请帮我。提前谢谢你......
答案 0 :(得分:1)
首先,您需要编写一个控制文件。
控制文件示例FYI:
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.exec 或 ProcessImpl.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());
}
}
答案 1 :(得分:0)
尝试制作external table。您可以使用ORACLE_LOADER驱动程序在CSV文件上创建外部表,然后使用DML(例如MERGE)使用外部表中的数据更新现有表。
答案 2 :(得分:-1)
我认为以下查询应该有效。
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