如果您使用mySQl,您可以执行类似的操作
static int BUFFER = 10485760;
public static String getData(String host, String port, String user, String password, String db) throws Exception {
Process run = Runtime.getRuntime().exec(
"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump --host=" + host + " --port=" + port +
" --user=" + user + " --password=" + password +
" --compact --databases --add-drop-table --complete-insert --extended-insert " +
"--skip-comments --skip-triggers "+ db);
InputStream in = run.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer temp = new StringBuffer();
int count;
char[] cbuf = new char[BUFFER];
while ((count = br.read(cbuf, 0, BUFFER)) != -1)
temp.append(cbuf, 0, count);
br.close();
in.close();
return temp.toString();
}
public static void main(String[] args) {
byte[] data = BackupRestore.getData("localhost", "3306",
"root", "", "test").getBytes();
File filedst = new File(path);
FileOutputStream dest = new FileOutputStream(filedst);
dest.write(data);
}
但我相信,除非你没有任何令人信服的/花哨的理由去做这个更好的用户命令行工具
您可以使用MySQL附带的GUI工具,更经过测试,更易于使用.
String path="C:/datadump/db_backup.sql"
确保路径C:/ datadump应该存在,或者您可以提供您选择的任何其他路径.