SqlLoad package com.sun.file;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;public class S
package com.sun.file; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class SqlLoad { //数据库配置信息 private static String dbconnStr="ACTIVITY1/ACTIVITY1@//192.168.40.30:1521/orcl"; /** java运行dos命令将txt文件下的数据导入到oracle * 没有ctlfile * @param args */ public void sqlldr (String infile,String tablename,String split_flag,String ctlfile){ Runtime rt=Runtime.getRuntime(); Process proc; String ctl_path=createloadctl(infile,tablename,split_flag,ctlfile); String log_path=ctl_path.substring(0,ctl_path.indexOf(".")+1)+"log"; String cmdstr="sqlldr "+dbconnStr+" control="+ctl_path+" log="+log_path+" skip=1"; System.out.println("cmd命令:"+cmdstr); try{ proc = rt.exec(cmdstr); }catch (Exception e) { System.out.println(" error while running sqlldr!"); } } /** * 有ctlfile * @param ctl_path */ public void sqlldr (String ctl_path){ Runtime rt=Runtime.getRuntime(); Process proc; String log_path=ctl_path.substring(0,ctl_path.indexOf(".")+1)+"log"; String cmdstr="sqlldr "+dbconnStr+" control="+ctl_path+" log="+log_path+" skip=1"; System.out.println("cmd命令:"+cmdstr); try{ proc = rt.exec(cmdstr); }catch (Exception e) { System.out.println(" error while running sqlldr!"); } } /** 创建ctl文件 * @param infile:有数据的txt文档 * @param tablename:要导入的oracle数据表 * @param split_flag:导入的数据以何种标志分割字段 * @param ctl文件的最终存放路径 */ public String createloadctl(String infile,String tablename,String split_flag,String ctlfile){ if (tablename=="" || tablename==null){ System.out.println("数据库名称不能为空!!"); } if (ctlfile=="" || ctlfile==null){ String path="F:\\sqlloadTest\\"; ctlfile=path+"lc_sqlldr.ctl"; System.out.println("路径:"+ctlfile); } File out_file=new File(ctlfile); try { if (out_file.exists()){ out_file.delete(); }else{ out_file.createNewFile(); } }catch(Exception ex){ ex.printStackTrace(); } /* * 编写ctlfile的格式 */ StringBuffer sb = new StringBuffer(); sb.append("load data"); sb.append("\n"); sb.append("infile "); sb.append("'"); sb.append(infile); sb.append("'"); sb.append("\n"); sb.append("append into table "); sb.append(tablename); sb.append("\n"); sb.append("fields terminated by "); sb.append("'"); sb.append(split_flag); sb.append("'"); sb.append("\n"); sb.append("(id,name,price,author,pubishdate DATE\"YYYY-MM-DD HH24:MI:SS\")"); String sbstr=sb.toString(); String ctlstr="load data"+"\n"+ "infile "+"'"+infile+"'"+"\n"+ "append into table "+tablename+"\n"+ "fields terminated by "+"'"+split_flag+"'"+"\n"+ "(id,name,price,author,pubishdate DATE\"YYYY-MM-DD HH24:MI:SS\")"; System.out.println(ctlstr); try{ //将数据库数据写入指定的txt文件 File file = new File(ctlfile); //自定义文件路径 OutputStreamWriter write; //write = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); write = new OutputStreamWriter(new FileOutputStream(file)); BufferedWriter writer = new java.io.BufferedWriter(write); writer.write(ctlstr); // 输出流写入到文件中 writer.close(); write.close(); }catch(UnsupportedEncodingException e){ e.printStackTrace(); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(Exception ex){ ex.printStackTrace(); } return ctlfile; } public static void main(String[] args) { String infile="F:\\sqlloadTest\\data.txt"; String tablename="book"; String split_flag="|"; String ctlfile="F:\\sqlloadTest\\lc_loader.ctl"; SqlLoad loader=new SqlLoad(); loader.sqlldr(infile, tablename, split_flag, ctlfile); //loader.sqlldr(ctlfile); } }