java中使用oracle的exp/imp导出、导入数据

java中使用oracle的exp/imp导出、导入数据

          今天在学习的过程中遇到了一篇关于java中使用oracle导入导出的文章,感觉还不错,为了学习和以后工作的需要,我整理如下:
           String[] cmds 
=   new  String[ 3 ];
        cmds[
0 =   " cmd " ;
        cmds[
1 =   " /C " ;
        cmds[
2 ] = commandBuf.toString();
        Process process
= Runtime.getRuntime().exec(cmds);
        
boolean  shouldClose = false ;
        
try   {
            InputStreamReader isr 
= new InputStreamReader(process.getErrorStream());
            BufferedReader br 
= new BufferedReader(isr);
            String line 
= null;
            
while ((line = br.readLine()) != null){
                
if(line.indexOf("错误")!=-1){
                    shouldClose
=true;
                    
break;
                }

            }

        }
 
        
catch  (IOException ioe)  {
            shouldClose
=true;
        }

        
if (shouldClose)
            process.destroy();
        
int  exitVal  =  process.waitFor();

        下面还有一种形式:
exp和imp的输出是要从ErrorStream中获取,这是我以前写的
Process proc = null;
try
{
proc = Runtime.getRuntime().exec(cmd.toString());
InputStream istr = proc.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
String str;
while ((str=br.readLine()) != null)
{
errorInfo.append(str + "\n");
}
proc.waitFor();
}
catch (Exception e)
{
...
}
if (proc.exitValue() == 0)
{
proc.destroy();
return true;
}
else
{
if(logger.isDebugEnabled())
logger.debug(errorInfo);
proc.destroy();
return false;

两者可以比较的看看
注意:在执行oracle的exp时,出现了一个很怪的现象,就是exp在console输出的信息没有被放入InputStream,反而是放到了ErrorStream中(即使正确的情况也是),这就导致了按照正常的情况去写这段代码的话反而会出问题。---这是在jdk1.4环境下实现的。



还有中建议是在jdk1.5环境下:可以如下实现
1,把对InputStream的处理放到一个单独Thread里面。
2,用ProcessBuilder的redirectErrorStream来合并OutputStream和ErrorStream。注意子进程的InputStream对应父进程的OutStream。如果不合并这两个流的话则必须并行排空它们,顺序的排空会导致思索。


你可能感兴趣的:(java中使用oracle的exp/imp导出、导入数据)