java调用mysql命令 导入导出数据库



package com.db.tools;
/**
 * 下面的代码,本人在实际的工程中使用都OK,
 * 如果由bug,请发信[email protected] 谢谢!
 */
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();
		}

	}
}




你可能感兴趣的:(Java,MySQL,SQL,C,C++)