sqldr批量导入oracle数据库

1.创建指定用户下的表
表结构:
create table scott.demo
(
id varchar2(40),
name varchar2(20),
description varchar2(40)

)

select * from demo

2.添加到demo20170101.csv 文件中内容,并放到指定目录下

3.创建控制文件democontrol.ctl
控制文件中的内容:
load data                                
infile 'E:\oracle\data\demo20170101.csv'      
append into table scott.demo       
fields terminated by ','                   
optionally enclosed by '"'                 
(id,name,description)

4.当前文件存放路径执行:>sqlldruserid=用户名/密码[@数据库字符串]control=控制文件
运行cmd--->
sqlldr scott/orcl@orcl control=e:\oracle\data\democontrol.ctl


cmd 运行  sqlldr userid=用户名/密码@数据库 control='ctl文件路径' log='log文件路径' 

用java 调用cmd 执行循环调用控制文件

package cn.ss.csv.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 第二种方式实现csv文件数据导入到oracle数据库中
 * 秒传
 * @author Administrator
 *
 */
public class Test1 {
	private final static String LINE_SEPARATOR = System.getProperty("line.separator");//行分割符
	public static void main(String[] args) throws IOException {
		
		runtest();
//		String control =createCtl("D:\\demo1\\demo20170101.csv","demo20170101");
	}

	public static void runtest() throws IOException {
		File dir = new File("D:\\demo1");//目录是否存在 用绝对路径
		if(!dir.exists()){
			dir.mkdir();
		}
		File[] listFiles = dir.listFiles(new FilenameFilter() {
			String suffix = ".csv";
			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return name.endsWith(suffix);
			}
		});
		StringBuffer infiles = new StringBuffer();
		for (File file : listFiles) {
			String absolutePath = file.getAbsolutePath();
			String name = file.getName();
//			System.out.println(name+":"+absolutePath);
			absolutePath.replace('\\', '/');
			System.out.println(absolutePath);
			infiles.append("infile '"+absolutePath+"'").append(LINE_SEPARATOR);
			
			//执行控制文件
//		Runtime.getRuntime().exec("cmd /c sqlldr scott/orcl@orcl control=e:\\oracle\\data\\"+control);
		}
		createCtl(infiles.toString()); //创建控制文件
		Runtime.getRuntime().exec("cmd /c sqlldr scott/orcl@orcl control=e:\\oracle\\data\\control.ctl");
	}

	/**
	 * 创建控制文件 并返回控制文件名
	 * @param absolutePath
	 * @return
	 * @throws FileNotFoundException 
	 */
	private static void createCtl(String infiles) throws FileNotFoundException {
		File control  = new File("e:\\oracle\\data\\control.ctl"); //存放控制文件目录
		
		StringBuffer content = new StringBuffer();
		content.append("load data").append(LINE_SEPARATOR);
		content.append(infiles).append(LINE_SEPARATOR);
		content.append("append into table scott.demo").append(LINE_SEPARATOR);
		content.append("fields terminated by ','").append(LINE_SEPARATOR);
		content.append("optionally enclosed by '\"' ").append(LINE_SEPARATOR);
		content.append("(id,name,description)");
		
		PrintWriter out = new PrintWriter(control);
		out.write(content.toString());
		out.close();//关闭资源
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值