JAVA代码调用SQLLDR

 

java代码调用sqlldr

/**
 * <b>文件名:</b>TestSqlldrDemo.java<br>
 * <b>描述:</b><br>
 * <b>作者:</b>FishRoad<br>
 * <b>时间:</b>2019年12月5日 上午11:36:47<br>
 * Copyright(c) 2019 Joyin Tech,LTD. All Rights Reserved.
 */
package ZRQ;

/**
 * <b>类名:</b>TestSqlldrDemo<br>
 * <b>描述:</b><br>
 * <b>作者:</b>FishRoad<br>
 * <b>时间:</b>2019年12月5日 上午11:36:47<br>
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestSqlldrDemo {

	public static final String ctlFileDir = "F:\\test\\";
	public static final String logFileDir = "F:\\test\\";

	public static void main(String[] args) {

		String username = "FJHX";
		String password = "FJHX";
		String Database = "@ORCL";
		String ctlFileName = "11.ctl";
		String logFileName = "11.log";
		boolean isDBA = false;

		String command = buildCommand(username, password, Database, isDBA,
				TestSqlldrDemo.ctlFileDir, ctlFileName,
				TestSqlldrDemo.logFileDir, logFileName);

		Executive(command);
	}

	/**
	 * @param username
	 * @param password
	 * @param Database
	 * @param isDBA
	 * @param ctlFileDir
	 * @param ctlFileName
	 * @param logFileDir
	 * @param logFileName
	 * @return the sql loader command
	 */
	public static String buildCommand(String username, String password,
			String Database, boolean isDBA, String ctlFileDir,
			String ctlFileName, String logFileDir, String logFileName) {

		StringBuffer command = new StringBuffer();
		command.append("sqlldr ");
		command.append(isDBA ? "'" : "");
		command.append(username);
		command.append("/");
		command.append(password);
		command.append(Database);
		command.append(isDBA ? " as sysdba'" : "");
		command.append(" control=" + ctlFileDir + ctlFileName);
		command.append(" data=" + ctlFileDir + "11.txt");
		command.append(" log=" + logFileDir + logFileName);

		System.out.println("Command : " + command.toString());
		// command : sqlldr 'SYS/123@ORCL as sysdba' control=C:\test.ctl
		// log=C:\test_table.log
		return command.toString();

	}

	/**
	 * To call DOS command
	 * 
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void Executive(String command) {

		// 获取子进程的输入流
		InputStream ins = null;
		// 获取子进程的错误流
		InputStream inserr = null;

		String[] cmd = new String[] { "cmd.exe", "/C", command }; // 命令
		try {
			Process process = Runtime.getRuntime().exec(cmd);
			inserr = process.getErrorStream(); // 获取执行cmd命令后的错误信息

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inserr, "ISO-8859-1"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				String msg = new String(line.getBytes("ISO-8859-1"), "GBK");
				System.out.println("执行命令错误:" + msg); // 输出
			}

			ins = process.getInputStream(); // 获取执行cmd命令后的信息

			reader = new BufferedReader(
					new InputStreamReader(ins, "ISO-8859-1"));
			while ((line = reader.readLine()) != null) {
				String msg = new String(line.getBytes("ISO-8859-1"), "GBK");
				System.out.println(msg); // 输出
			}

			int exitValue = process.waitFor();

			System.out.println("Returned value was:" + exitValue);
			process.destroy();

			if (exitValue == 0) {
				System.out.println("导入成功!");
			} else {
				System.out.println("导入失败!");
			}

			process.getOutputStream().close(); // 关闭
			ins.close();
			inserr.close();
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
		}
	}

}

11.txt 的内容如下:

id|name|text|remark|
12|qqqq|hahahah|www|
13|www|hahahah|www|
14|eeee|hahahah|www|

11.ctl 的内容如下:

OPTIONS (skip=1)
load data
truncate into table ZRQ
fields terminated by '|'
TRAILING NULLCOLS
(id,
DATA_COL2 FILLER,
text,
remark
)

ZRQ建表语句

create table ZRQ
(
  id     VARCHAR2(20) not null,
  name   VARCHAR2(50),
  text   VARCHAR2(50),
  remark VARCHAR2(20)
)

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java代码调用执行shell脚本,sqlldr导数与使用sqlplus在shell调用执行存储过程。 linux环境中有2个dba的用户:oracle、erm 还有1个web用户:erm 在linux环境中,这三个用户都可以直接在任意目录下执行该shell脚本,可是在java代码调用shell脚本时,报了如下4个错误: 1、sqlldr: command not found 2、sqlplus: command not found 3、0750: You may need to set ORACLE_HOME to your Oracle software directory 4、Message 2100 not found; No message file for product=RDBMS, facility=ULMessage 2100 not found; No message file for product=RDBMS, facility=UL$ 检查了dba用户和web用户下关于oracle的所有环境变量发现没有问题 (/home/oracle/.profile 与 /home/erm/.profile 环境变量文件为隐藏文件需使用 ls -a 命令查看) 在网上查了一遍以后有如下几种处理办法: 1、没有oracle_home目录的执行权限,无法进入到bin目录,使用chmod 755 $oracle_home更改目录权限;使用chmod 755 $oracle_home/lib更改目录权限;对目录$oracle_home/rdbms/mesg目录赋予相应权限;当需要给非oracle用户配置使用sqlldr时,不单需要配置环境变量,还需要把相关目录的访问权限赋予该用户。【采用此法测试以后还是出现上面的错误】 2、用sh直接执行shell和在java直接执行shell是有区别的,要在shell文件内增加oracle的环境变量即. /home/oracle/.bash_profile 这句话,否则在执行sqlldr会报如下异常: Message 2100 not found; No message file for product=RDBMS...... facility=UL 【采用此法测试以后继续报错,但是这个方法有引导作用,继续往下看】 因为我在linux环境下执行shell脚本时sqlldr命令和sqlplus命令是正常执行的,没有任何问题,但是在java代码调用脚本时却报错,所有排除了其他原因,只可能是环境变量的问题, 于是我把oracle的所有环境变量直接复制到shell脚本文件中,在java调用了一下,然后所有问题迎刃而解! 具体代码参看文件内容

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值