java实现oracle数据库的dmp导入出

  最近有个需求java实现oracle部分表的dmp文件导入/出,废话不多直接上代码,properties文件存储数据库信息

 /**
     * 导出dmp
     * @return
     */
    public boolean generateDmp() {
        Process process = null;
        String[] cmds = new String[3];
        try {
            StringBuffer sql = new StringBuffer("exp ");
            sql.append(PropertyUtil.getProperty("username") + "/");
            sql.append(PropertyUtil.getProperty("password") + "@");
            sql.append(PropertyUtil.getProperty("url") + "/");
            sql.append(PropertyUtil.getProperty("servername"));
            sql.append(" tables=scjhmy.jhuser,scjhmy.jhrole,scjhmy.jhmenu");
            sql.append(" file=E:\\war\\scjhmy_tables.dmp log=E:\\war\\scjhmy_tables_out.log");
            System.out.println("dos命令:" +sql.toString());
            cmds[0] = "cmd";
            cmds[1] = "/c";
            cmds[2] = sql.toString();
            process = Runtime.getRuntime().exec(cmds);
        } catch (IOException e) {
            System.out.println("导出dmp失败,失败原因:" + e.getMessage());
        }
        boolean shouldClose = false;
        try {
            InputStreamReader isr = new InputStreamReader(process.getErrorStream(),  Charset.forName("GBK"));
            BufferedReader br = new BufferedReader(isr);
            String line;
            // 打印导出日志
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                if (line.indexOf("????") != -1) {
                    shouldClose = true;
                    break;
                }
            }
        } catch (IOException i) {
            shouldClose = true;
            System.out.println("导出失败,失败原因:" + i.getMessage());
        }
        if (shouldClose) {
            // 杀死进程
            process.destroy();
        }
        int exitVal;
        try {
            exitVal = process.waitFor();
            System.out.println(exitVal);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
            System.out.println("导出失败,失败原因:" + ie.getMessage());
        }
        return true;
    }

    /**
     * 导入dmp
     * @return
     */
    public boolean impDmp() {
        Process process = null;
        String[] cmds = new String[3];
        try {
            StringBuffer sql = new StringBuffer("imp ");
            sql.append(PropertyUtil.getProperty("username") + "/");
            sql.append(PropertyUtil.getProperty("password") + "@");
            sql.append(PropertyUtil.getProperty("url") + "/");
            sql.append(PropertyUtil.getProperty("servername"));
            sql.append(" file=E:\\war\\scjhmy_tables.dmp log=E:\\war\\scjhmy_tables_imp.log");
            sql.append(" full=y ignore=y");
            cmds[0] = "cmd";
            cmds[1] = "/c";
            cmds[2] = sql.toString();
            process = Runtime.getRuntime().exec(cmds);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("导出失败,失败原因:" + e.getMessage());
        }
        boolean shouldClose = false;
        try {
            InputStreamReader isr = new InputStreamReader(process.getErrorStream(),  Charset.forName("GBK"));
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                if (line.indexOf("????") != -1) {
                    shouldClose = true;
                    break;
                }
            }
        } catch (IOException e) {
            shouldClose = true;
            System.out.println("导入失败,失败原因:" + e.getMessage());
        }
        if (shouldClose) {
            process.destroy();
        }
        int exitVal;
        try {
            exitVal = process.waitFor();
            System.out.println(exitVal);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("导入失败,失败原因:" + e.getMessage());
        }
        return true;
    }

    @Test
    public void outTest() {
        OracleDmpUtil oracleDmpUtil = new OracleDmpUtil();
        boolean outFlag = oracleDmpUtil.generateDmp();
        System.out.println(outFlag);
    }

    @Test
    public void impTest() {
        OracleDmpUtil oracleDmpUtil = new OracleDmpUtil();
        boolean impFlag = oracleDmpUtil.impDmp();
        System.out.println(impFlag);
    }

读取properties文件工具类

public class PropertyUtil {

    private static Properties props;
    static {
        loadProps();
    }

    synchronized static private void loadProps(){
        System.out.println("开始加载properties文件内容.......");
        props = new Properties();
        InputStream in = null;
        try {
            // 第一种,通过类加载器进行获取properties文件流-->
            in = PropertyUtil.class.getClassLoader().getResourceAsStream("/jdbc.properties");
            // 第二种,通过类进行获取properties文件流-->
            //in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
            props.load(in);
        } catch (FileNotFoundException e) {
            System.out.println("jdbc.properties文件未找到");
        } catch (IOException e) {
            System.out.println("出现IOException");
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
                System.out.println("jdbc.properties文件流关闭出现异常");
            }
        }
        System.out.println("加载properties文件内容完成...........");
        //System.out.println("properties文件内容:" + props);
    }

    /**
     * 根据key获取配置文件中的属性
     */
    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    /**
     * 根据key获取配置文件中的属性,当为null时返回指定的默认值
     */
    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值