Windows環境下測試代碼:
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; 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:\\test\\";//文件地址路徑 String fileName = "test2015.txt";//數據文件名 String tableName = "test_table";//表名 String fieldName = "(name,userpwd,sex)";//要寫入表的字段 String ctlfileName = "test_table.ctl";//控制文件名 stlFileWriter(fileRoute,fileName,tableName,fieldName,ctlfileName); //要執行的DOS命令 String user = "SCOTT"; String psw = "123"; String Database = "//127.0.1.1:1521/ORCL"; IP要指向數據庫服務器的地址 String logfileName = "test_table.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命令 --數據庫 用戶名 密碼 user/password@database 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(); } } }
Linux環境下的測試代碼:
import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class TestLinux { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // ./szrpp_files/clPath //寫控制文件.ctl String fileRoute = "/home/test/";//文件地址路徑 String fileName = "test2015.txt";//數據文件名 String tableName = "test_table";//表名 String fieldName = "(name,userpwd,sex)";//要寫入表的字段 String ctlfileName = "ctltest.ctl";//控制文件名 stlFileWriter(fileRoute,fileName,tableName,fieldName,ctlfileName); //要執行的DOS命令 String user = "scott"; String psw = "123"; String Database = "//127.0.0.1:1521/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環境下注釋掉不需要CMD 直接執行DOS就可以 //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(); } } }
特別注意:
注意cmd命令執行的密碼字符不能過於復雜 不能帶有特殊符號 以免執行不通過 譬如有!@#¥%……&*之類的
所以在Oracle數據庫設置密碼是不要太復雜 同時在數據庫集群環境下的任何一IP地址都可以訪問數據庫的
1.你的程序部署在什么服務器下面 是在Oracle數據庫服務器下 數據庫連接IP地址配置可以不指明,如果部署在其他服務器下不在數據庫服務器下數據庫名的連接地址要指向明確的 所在數據庫服務器IP地址,這個自己可以多次試驗測試就曉得了。
程序部署所在的服務器一定要安裝oracle客戶端才能執行導入數據命令
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