package com.sync.util; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.metinform.util.StringUtil; import com.sync.hbm.SyncOrigtab; import com.sync.hbm.SyncTargtab; import com.sync.init.ServiceDao; import com.sync.init.SyncThreadDao; public class SyncExpImpUtil { private static Log log = LogFactory.getLog(SyncExpImpUtil.class); /** * 导出 * @param userName 数据库用户名 * @param passWord 数据库密码 * @param dataBaseName 数据库名 * @param tabName 表名 * @param address 导出地址 * @param where 数据过滤条件 */ public static boolean exp(String userName,String passWord,String dataBaseName,String tabName,String address,String where){ boolean bool = false; // 拼装DOS命令进行数据库备份 StringBuffer exp = new StringBuffer("exp "); exp.append(userName); exp.append("/"); exp.append(passWord); exp.append("@"); exp.append(dataBaseName); exp.append(" TRIGGERS=N "); if(StringUtil.isNotEmpty(tabName)){ exp.append(" tables="); exp.append(tabName); } exp.append(" file="); exp.append(address); if(StringUtil.isNotEmpty(where)){ exp.append(" query=\\\"where "); exp.append(where); exp.append(" \\\" "); } try { log.info(exp.toString()); //System.out.println(exp.toString()); Process p = Runtime.getRuntime().exec(exp.toString()); InputStreamReader isr = new InputStreamReader(p.getErrorStream()); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null){ if(line.indexOf("错误")!=-1){ break; } } p.destroy(); p.waitFor(); bool = true; } catch (Exception e) { log.error("导出文件"+tabName+"失败"); e.printStackTrace(); } return bool; } /** * 导入 * @param userName 数据库用户名 * @param passWord 数据库密码 * @param dataBaseName 数据库名 * @param tabName 表名 * @param address 导出地址 * @param where 数据过滤条件 * @param ignore true追加(数据导出模式) */ public static boolean imp(String userName,String dataBaseName,String tabName,String address,boolean ignore){ boolean bool = false; // 拼装DOS命令进行数据库备份 StringBuffer imp = new StringBuffer("imp "); imp.append(userName); imp.append("/"); imp.append(passWord); imp.append("@"); imp.append(dataBaseName); passWord,String if(ignore){ imp.append(" ignore=y"); } if(StringUtil.isNotEmpty(tabName)){ imp.append(" tables="); imp.append(tabName); } imp.append(" file="); /* * 得到存储地址的最后一个字符 如果有\就直接拼装地址 如果没有\就加上/然后拼装数据库名称 */ imp.append(address); try { log.info(imp.toString()); //System.out.println(imp.toString()); Process p = Runtime.getRuntime().exec(imp.toString()); InputStreamReader isr = new InputStreamReader(p.getErrorStream()); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null){ if(line.indexOf("错误")!=-1){ break; } } p.destroy(); p.waitFor(); bool = true; } catch (Exception e) { log.error("导入文件"+tabName+"失败"); e.printStackTrace(); } return bool; } 本文来源:https://www.wddqw.com/doc/a3676da5f524ccbff1218486.html