JavaコードプログラミングMySQLデータベースのバックアップとインポートを実現
4575 ワード
/**
* @author Zengyoujie
* @date 2011-10-23 09:50:02
* Copyright 2011
*/
package com.fjsdfx.sqlBack;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Beifen {
/**
* @param args
*/
public static void main(String[] args) {
/*
* 。
* : mysql , , .sql ;
* : mysql , .sql
* : ">" "<"
*/
//backup();
load();
}
/**
* sql : sql ultra
* edit , , ( sql , db )
*/
public static void backup() {
try {
Runtime rt = Runtime.getRuntime();
// mysql cmd:
//-uroot -u+
//-p123456 -p+
//--set-charset=utf8 , utf8。 utf8
//scmsys
Process child = rt.exec("mysqldump -uroot -p123456 --set-charset=utf8 scmsys");
// .sql , 。 : ,
InputStream in = child.getInputStream();//
InputStreamReader isr = new InputStreamReader(in, "utf8");// utf8。 utf8,
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
//
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r
");
}
outStr = sb.toString();
// sql :
//"e:/abc.sql" sql
FileOutputStream fout = new FileOutputStream(
"e:/abc.sql");
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
writer.write(outStr);
// : , , flush()
writer.flush();
//
in.close();
isr.close();
br.close();
writer.close();
fout.close();
System.out.println("/* Output OK! */");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
*
*/
public static void load() {
try {
String fPath = "e:/sp.sql";//"e:/sp.sql" sql
Runtime rt = Runtime.getRuntime();
// mysql cmd:
Process child = rt.exec("mysql -uroot -p123456 abc");
OutputStream out = (OutputStream) child.getOutputStream();//
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fPath), "utf8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r
");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
// : , , flush()
writer.flush();
//
out.close();
br.close();
writer.close();
System.out.println("/* Load OK! */");
} catch (Exception e) {
e.printStackTrace();
}
}
}