最近有个需求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);
}