java备份mysql数据库

package com.zjx.dbbackup;

/**
 * JAVA 对MYSQL数据库的备份
 *       mysql 5.0
 *       JDK 1.6
 * @author Zhou Jingxian
 *
 */
public class DB_deal {

	public static void main(String[] args) {
		DB_deal.backup();
//		DB_deal.load();
	}
	
	private static String mysqlcmd = null;
	
	/**
	 * 备份数据库
	 */
	public static void backup(){
		//使用mysqldump来备份数据库,格式:"mysqldump -u username -ppassword --opt database_name > direction/backup_name.sql"
		//Example:mysqldump -u root -prootroot --opt maritime > D:/backup_maritime.sql
		mysqlcmd = "mysqldump -u root -prootroot --opt maritime > D:/backup_maritime_0.sql";
		
		try {
			Runtime runtime = Runtime.getRuntime(); 
			
			//调用 mysql的cmd:			
			runtime.exec("cmd /c" + mysqlcmd);
			
			System.out.println("....success....");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("....error..."+e.getMessage());
			
		}
	}
	
	/**
	 * 还原数据库
	 */
	public static void load(){
		//使用mysqldump来备份数据库,格式:"mysql -u username -ppassword  database_name < direction/backup_name.sql"
		mysqlcmd = "mysql -u root -prootroot  maritime < D:/backup_maritime_0.sql";
		
		try {
			Runtime runtime = Runtime.getRuntime(); 
			
			//调用 mysql的cmd:			
			runtime.exec("cmd /c" + mysqlcmd);
			
			System.out.println("....success....");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("....error..."+e.getMessage());
			
		}
	}
	
}

 

你可能感兴趣的:(java,jdk,sql,c,mysql)