標籤:
Windows環境下測試代碼:
import java.io.File;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.Scanner;public class testTimer { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // ./szrpp_files/clPath //寫控制檔案.ctl String fileRoute = "D:\\files\\";//檔案地址路徑 String fileName = "db.txt";//資料檔案名 String tableName = "testtable";//表名 String fieldName = "(Source,File,Date,FileNum,RechargeSerial,Status)";//要寫入表的欄位 String ctlfileName = "ctlName.ctl";//控制檔案名稱 stlFileWriter(fileRoute,fileName,tableName,fieldName,ctlfileName); //要執行的DOS命令 String user = "SCOTT"; String psw = "123456"; String Database = "ORCL"; String logfileName = "logName.log"; Executive(user,psw,Database,fileRoute,ctlfileName,logfileName); } /** * * 寫控制檔案.ctl * @param fileRoute 資料檔案地址路徑 * @param fileName 資料檔案名 * @param tableName 表名 * @param fieldName 要寫入表的欄位 * @param ctlfileName 控制檔案名稱 */ public static void stlFileWriter(String fileRoute,String fileName,String tableName,String fieldName,String ctlfileName) { FileWriter fw = null; String strctl = "OPTIONS (skip=0)" + " LOAD DATA INFILE ‘"+fileRoute+""+fileName+"‘" + " APPEND INTO TABLE "+tableName+"" + " FIELDS TERMINATED BY ‘,‘" + " OPTIONALLY ENCLOSED BY \"‘\"" + " TRAILING NULLCOLS "+fieldName+""; try { fw = new FileWriter(fileRoute+""+ctlfileName); fw.write(strctl); } catch (IOException e) { e.printStackTrace(); } finally { try { fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 調用系統DOS命令 * @param user * @param psw * @param Database * @param fileRoute 檔案路徑 * @param ctlfileName 控制檔案名稱 * @param logfileName 記錄檔名 */ public static void Executive(String user,String psw,String Database,String fileRoute,String ctlfileName,String logfileName) { InputStream ins = null; //要執行的DOS命令 String dos="sqlldr "+user+"/"+psw+"@"+Database+" control="+fileRoute+""+ctlfileName+" log="+fileRoute+""+logfileName; String[] cmd = new String[] { "cmd.exe", "/C", dos }; // 命令 try { Process process = Runtime.getRuntime().exec(cmd); ins = process.getInputStream(); // 擷取執行cmd命令後的資訊// BufferedReader reader = new BufferedReader(new InputStreamReader(ins));// String line = null;// while ((line = reader.readLine()) != null)// {// String msg = new String(line.getBytes("ISO-8859-1"), "UTF-8");// System.out.println(msg); // 輸出// } int exitValue = process.waitFor(); if(exitValue==0) { System.out.println("傳回值:" + exitValue+"\n資料匯入成功"); }else { System.out.println("傳回值:" + exitValue+"\n資料匯入失敗"); } process.getOutputStream().close(); // 關閉 } catch (Exception e) { e.printStackTrace(); } }}
View Code
Linux環境下的測試代碼:
import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;public class TestLinux { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // ./szrpp_files/clPath //寫控制檔案.ctl String fileRoute = "/home/test/files/clPath/";//檔案地址路徑 String fileName = "db.txt";//資料檔案名 String tableName = "testtable";//表名 String fieldName = "(Source,File,Date,FileNum,Status)";//要寫入表的欄位 String ctlfileName = "ctltest.ctl";//控制檔案名稱 stlFileWriter(fileRoute,fileName,tableName,fieldName,ctlfileName); //要執行的DOS命令 String user = "scott"; String psw = "123456"; String Database = "ORCL"; String logfileName = "logtext.log"; Executive(user,psw,Database,fileRoute,ctlfileName,logfileName); } /** * * 寫控制檔案.ctl * @param fileRoute 資料檔案地址路徑 * @param fileName 資料檔案名 * @param tableName 表名 * @param fieldName 要寫入表的欄位 * @param ctlfileName 控制檔案名稱 */ public static void stlFileWriter(String fileRoute,String fileName,String tableName,String fieldName,String ctlfileName) { FileWriter fw = null; String strctl = "OPTIONS (skip=0)" + " LOAD DATA INFILE ‘"+fileRoute+""+fileName+"‘" + " APPEND INTO TABLE "+tableName+"" + " FIELDS TERMINATED BY ‘,‘" + " OPTIONALLY ENCLOSED BY \"‘\"" + " TRAILING NULLCOLS "+fieldName+""; try { fw = new FileWriter(fileRoute+""+ctlfileName); fw.write(strctl); } catch (IOException e) { e.printStackTrace(); } finally { try { fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 調用系統DOS命令 * @param user * @param psw * @param Database * @param fileRoute 檔案路徑 * @param ctlfileName 控制檔案名稱 * @param logfileName 記錄檔名 */ public static void Executive(String user,String psw,String Database,String fileRoute,String ctlfileName,String logfileName) { InputStream ins = null; //要執行的DOS命令 String dos="sqlldr "+user+"/"+psw+"@"+Database+" control="+fileRoute+""+ctlfileName+" log="+fileRoute+""+logfileName; //Linux環境下注釋掉 //String[] cmd = new String[] //{ "cmd.exe", "/C", dos }; // Windows環境 命令 try { Process process = Runtime.getRuntime().exec(dos); ins = process.getInputStream(); // 擷取執行cmd命令後的資訊// BufferedReader reader = new BufferedReader(new InputStreamReader(ins));// String line = null;// while ((line = reader.readLine()) != null)// {// String msg = new String(line.getBytes("ISO-8859-1"), "UTF-8");// System.out.println(msg); // 輸出// } int exitValue = process.waitFor(); if(exitValue==0) { System.out.println("傳回值:" + exitValue+"\n資料匯入成功"); }else { System.out.println("傳回值:" + exitValue+"\n資料匯入失敗"); } process.getOutputStream().close(); // 關閉 } catch (Exception e) { e.printStackTrace(); } }}
View Code
特別注意:
1.你的程式部署在什麼伺服器下面 是在Oracle資料庫伺服器下 資料庫連接配置地址可以不指明,如果部署在其他伺服器下不在資料庫伺服器下資料庫名的串連地址要指向明確的 所在資料庫伺服器IP地址,這個自己可以多次實驗測試就曉得了。
2.測試程式只是實現了功能,但是放到你的項目程式中一定要多多測試自然會發現問題的,這個測試代碼放到你的項目中時候一定要注意一些代碼的改造,具體自己多測試就發現了。可以簡單說明下 就在Executive()這個方法裡面的代碼只要稍加改造就好了,自己摸索下就明白了。
3.控制檔案路徑最好這樣使用在引用到項目中時候:control="+clPath+File.separator+ctlfileName+" log="+clPath+File.separator+logfileName
4.針對這段代碼具體描述下
String strctl = "OPTIONS (skip=0)" + // 0是從第一行開始 1是 從第二行 " LOAD DATA INFILE ‘"+fileRoute+""+fileName+"‘" + " APPEND INTO TABLE "+tableName+"" + //覆蓋寫入 " FIELDS TERMINATED BY ‘,‘ " + //--資料中每行記錄用","分隔 ,TERMINATED用於控制欄位的分隔字元,可以為多個字元。 " OPTIONALLY ENCLOSED BY \"‘\"" + //源檔案有引號 ‘‘,這裡去掉 ‘‘‘‘" " TRAILING NULLCOLS "+fieldName+"";//--表的欄位沒有對應的值時允許為空白 來源資料沒有對應,寫入null
Java中使用Oracle的用戶端 load data和sqlldr命令執行資料匯入到資料庫中