日期:2014-05-16 浏览次数:20474 次
package com.db.tools; /** * 下面的代码,本人在实际的工程中使用都OK, * 如果由bug,请发信xf.zhouwenjun@163.com 谢谢! */ public class SqlControl { // 备份数据库 public static void backup(String dbName, String filePath) { try { @SuppressWarnings("unused") Process process = Runtime.getRuntime().exec( "cmd /c mysqldump -uroot -psa " + dbName + " > " + filePath + "/" + new java.util.Date().getTime() + ".sql"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("备份数据库"); e.printStackTrace(); } } @SuppressWarnings("unused") // 恢复数据库 public static void load(String dbName, String filePath) { try { @SuppressWarnings("unused") Process process = Runtime.getRuntime().exec( "cmd /c mysql -uroot -psa " + dbName + " < " + filePath); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("恢复数据库"); e.printStackTrace(); } } public static void main(String[] args) { try { // backup("mysql2009","d:/"); load("test", "d:/1259138711453.sql"); System.out.println("ok"); } catch (Exception e) { // TODO: handle exception e.getMessage(); } } }