我想将数据从 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中加载数据。我必须在查询中进行哪些更改。请帮我。预先感谢您...
最佳答案
首先,您需要编写一个控制文件。
控制文件示例仅供引用:
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());
}
}
关于java - 如何在java中使用sql加载器在oracle中读取csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25737927/