`

Invoked Sqlload in Java

阅读更多
http://www.javabulls.com/node/1
public class CSVLoader {

public void execute(LoadableCSV csv) throws Exception{
try {
String comand = init(csv);
service(comand);
} catch (Throwable e) {
throw new Exception(e);
} finally {
shutdown();
}
}

protected String init(LoadableCSV csv) {
StringBuffer command = new StringBuffer();
command.append("sqlldr").append(" ");
command.append(csv.getLoaderCommand());

System.out.println("CSVLoader initialized.");

return command.toString();
}

protected void service(String command) throws Exception {

Runtime r = Runtime.getRuntime();
long startTime = System.currentTimeMillis();

try {
Process process = r.exec(command);
if (process == null) {
System.out.println("Process NOT Initialized for command [" + command + "]");
} else {
// wait for the process to exit
process.waitFor();
System.out.println("Loading completed. Exit Value [" + process.exitValue() + "] Time taken ["
+ (System.currentTimeMillis() - startTime) / 1000 + "] sec");

StringBuffer strBuffer = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null) {
strBuffer.append(line).append("\n");
}
reader.close();

if (process.exitValue() != 0) {
System.out.println("ERROR:" + strBuffer.toString());
throw new Exception("Sql Loading Exited with [" + process.exitValue() + "]");
} else {
System.out.println("Loader completed execution. \n" + strBuffer.toString());
}
}
} catch (InterruptedException e) {
System.out.println("Exception while waiting for process [" + command + "] to complete");
throw e;
} catch (IOException e) {
System.out.println("Exception executing process [" + command + "]");
}

System.out.println("CSVLoader service done.");
}

protected void shutdown() {
System.out.println("CSVLoader shutdown.");
}

}

The Loader class is invoked from any Java process using a LoadableCSV instance.
LoadableCSV is an interface which can be implemented for different flat files
as per the requirement.

public interface LoadableCSV {

public int getRecordsToSkip();

public int getRecordsToLoad();

public int getErrorsAllowed();

public String getSeparator();

public String getLoaderCommand();

}

The CSVLoader class just uses the getLoaderCommand() in the example above,
but can be enhanced to use other methods mentioned above or more methods
can be added to the interface and each implementing csv can provide it's
own implementation. This simple design above helps load different types
of csv files from a single loader class.

**Regular caveats apply, the code may have some bugs or may just not be
the ideal choice for your implementation. Please feel free to use the above
as a reference for your implementation.

Happy Loading.
分享到:
评论

相关推荐

    java 项目中遇到的错误

    Unknown column 'fillMen' in 'field list' java.sql.SQLException: Operation not ...java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in

    Java Excel API

    In addition, it contains a mechanism which allows java applications to read in a spreadsheet, modify some cells and write out the new spreadsheet. This API allows non Windows operating systems to ...

    java英语练习题

    ##### 题目4: ________ consists of a set of separate programs for developing and testing Java programs, each of which is invoked from a command line. - **选项**: - 1. Java Language Specification - 2....

    Java期末考试模拟习题 .doc

    17. 类C的运行结果是"The default constructor of A is invoked",因为每个类的构造器都会调用其父类的构造器。 以上就是Java期末考试模拟习题的部分解析,涵盖了变量命名、关键字使用、循环控制、数组创建、继承和...

    WebSphere.MQ.Solutions.in.a.Microsoft.NET.Environment

    CHM格式文档,包含配套源代码。 WebSphere MQ, IBM messaging service, can be incorporated into the ...invoked by .NET or Java applications and also J2EE applications invoked by .NET applications or Java.

    java中常见的错误.docx

    11. QueryRunner使用错误:`java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way`提示在调用QueryRunner时没有提供DataSource或Connection,需要确保正确配置。 12. executeQuery...

    ant-invoked.jar.zip

    标签:ant-invoked.jar.zip,ant,invoked,jar.zip包下载,依赖包

    2010-09-27 MVC2.0项目模板.rar

    修正“the invoked member is not supported in a dynamic assembly.”错误 修改: efproviderwrappertoolkit\entityconnectionwrapperutils.cs (184 ) parseresources 添加: if (asm.gettype().fullname != ...

    java定时器timer制作

    Console.WriteLine("TimerCallbackMethod invoked"); } ``` #### 3. System.Timers.Timer - **特点**: - 适用于桌面应用程序和服务器端。 - 提供更多配置选项,如精度调整。 - 更高级的API,支持事件处理。 ...

    JVM内核架构--JVM规范

    Each entry in the Java stack is called “stack frames“. Whenever a method is invoked a new stack frame is added to the stack and corresponding frame is removed when its execution is completed. ...

    ANSI/ISO/IEC International Standard(IS) Database Language SQL Part 5 Host Language Bindings (SQL Bindings)

    该标准主要关注如何将SQL与高级编程语言(如C、Java等)进行集成,以便程序员能够在这些语言中使用SQL来访问数据库。 #### 规范参考 在该标准的第二章中,列出了所有必需遵循的规范性引用文件,这些文件对于理解...

    java全英文试题,侧重基础知识

    - **选项:** a) Superclass object b) The object invoked the method c) Any object used in the application d) Ignore - **答案与解析:** 正确答案是b) The object invoked the method。“this”关键字主要用于...

    Game Theory

    he discipline of game theory was pioneered ...on the context in which the game theoretic language is invoked: in evolutionary biology (see, for example, John Maynard Smith, 1982) players are Chapter 0 T

    Research on dynamic update transaction for Java classes

    In addition, the Java language is gaining increasing popularity in developing distributed systems. Most previous works on updating are concerned with safely updating one class every time. It has many...

    android lint performance probe帮助.zip

    expression to the @MethodPattern annotation in LintDetectorStats.java, you might see a line like this. Conversely, it's a good sign if you see a line like this. If you make code changes to the tool, ...

    Visual Assist X 2107官方原版 带破解补丁

    When Goto is invoked on an external type in XAML, VA prompts to open the Object Browser (instead of searching it without prompting). (case=96541) Find Symbol in Solution dialog lists symbols defined ...

    ios demo,UIAlertController_supportedInterfaceOrientations,警告框,操作表的实例

    let okAction = UIAlertAction(title: "确定", style: .default) { _ in // 用户点击“确定”时执行的代码 } alertController.addAction(okAction) present(alertController, animated: true, completion: nil) ```...

    plsqldev12.0.4.1826x64主程序+ v12中文包+keygen

    A Standard Test can be invoked from the popup menu when right-clicking on the function or procedure in the Object Browser or in a PL/SQL source: The Test Window now supports Oracle12c implicit ...

    servlet2.4doc

    Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked. doFilter(ServletRequest, ...

    Android代码-jclasslib

    jclasslib bytecode viewer Purpose jclasslib bytecode viewer is a tool that visualizes all aspects ...can be invoked when a Java, Groovy or Kotlin file is open in the editor. The bytecode will be shown i

Global site tag (gtag.js) - Google Analytics