Javaの下でMysqlをエクスポートする.sqlファイル
5248 ワード
Java呼び出しコマンドウィンドウを使用してコマンドを実行し、MySqlをデータベースにインポートするには、一般的に3つのステップに分けられます.
ステップ1:Mysqlデータベースにログインし、データベースにログインするときにどのデータベースにログインするかを指定し、指定した場合はステップ2をスキップすることができます.
ステップ2:インポートするターゲット・データベースへのデータベースの切り替え
ステップ3:コマンドによるインポートの開始
エクスポートを行う場合は、mysqlインストールパスの下のbinが
システムのpath変数では、エクスポート時にコマンド文を直接使用できます.そうしないと、コマンド文を実行する必要があります.
コマンドが存在する場所のパス、すなわちmysqlインストールパスが考えられるbinの下のmysqldumpコマンドを追加します.
以下はコード
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Properties;
/***エクスポートを行う場合、コマンド文の実行環境に注意し、mysqlインストールパスの下のbinを*システムのpath変数に追加した場合、エクスポート時にコマンド文を直接使用できます.そうしないと、コマンド文の*を実行する際にコマンドの位置のパスを追加する必要があります.すなわちmysqlインストールパス希望bin下のmysqldumpコマンド*@author andy*/public class test 5{
//MySqlImportAndExport.export(properties);//ここで簡単に異常を起こしたら、私は直接上へ投げます//test 5.importSql(properties); test5.export(properties); }
}
原文住所:http://elim.iteye.com/blog/1413830
ステップ1:Mysqlデータベースにログインし、データベースにログインするときにどのデータベースにログインするかを指定し、指定した場合はステップ2をスキップすることができます.
ステップ2:インポートするターゲット・データベースへのデータベースの切り替え
ステップ3:コマンドによるインポートの開始
エクスポートを行う場合は、mysqlインストールパスの下のbinが
システムのpath変数では、エクスポート時にコマンド文を直接使用できます.そうしないと、コマンド文を実行する必要があります.
コマンドが存在する場所のパス、すなわちmysqlインストールパスが考えられるbinの下のmysqldumpコマンドを追加します.
以下はコード
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Properties;
/***エクスポートを行う場合、コマンド文の実行環境に注意し、mysqlインストールパスの下のbinを*システムのpath変数に追加した場合、エクスポート時にコマンド文を直接使用できます.そうしないと、コマンド文の*を実行する際にコマンドの位置のパスを追加する必要があります.すなわちmysqlインストールパス希望bin下のmysqldumpコマンド*@author andy*/public class test 5{
public static void main(String args[]) throws IOException {
InputStream is = test5.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
//MySqlImportAndExport.export(properties);//ここで簡単に異常を起こしたら、私は直接上へ投げます//test 5.importSql(properties); test5.export(properties); }
/**
*
* @param properties
* @throws IOException
*/
public static void export(Properties properties) throws IOException {
Runtime runtime = Runtime.getRuntime();
String command = getExportCommand(properties);
runtime.exec(command);//
}
/**
*
* mysql :
* mysql; mysql -uusername -ppassword -hhost -Pport -DdatabaseName;
* , , ;
* ;use importDatabaseName;
* ;source importPath;
* @param properties
* @throws IOException
*/
public static void importSql(Properties properties) throws IOException {
Runtime runtime = Runtime.getRuntime();
// mysql ,
String cmdarray[] = getImportCommand(properties);// ,
//runtime.exec(cmdarray);//
Process process = runtime.exec(cmdarray[0]);
// mysql , mysql
//
OutputStream os = process.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(os);
// 1 2
writer.write(cmdarray[1] + "\r
" + cmdarray[2]);
writer.flush();
writer.close();
os.close();
}
/**
*
* :
* “>” , mysqldump -uusername -ppassword databaseName > exportPath,
* Java , -r , :
* mysqldump -uusername -ppassword databaseName -r exportPath。
* @param properties
* @return
*/
private static String getExportCommand(Properties properties) {
StringBuffer command = new StringBuffer();
String username = properties.getProperty("jdbc.username");//
String password = properties.getProperty("jdbc.password");//
String exportDatabaseName = properties.getProperty("jdbc.exportDatabaseName");//
String host = properties.getProperty("jdbc.host");// , , localhost
String port = properties.getProperty("jdbc.port");//
String exportPath = properties.getProperty("jdbc.exportPath");//
// ,
command.append("mysqldump -u").append(username).append(" -p").append(password)// p, P。
.append(" -h").append(host).append(" -P").append(port).append(" ").append(exportDatabaseName).append(" -r ").append(exportPath);
return command.toString();
}
/**
* ,
*
* , , ;
* @param properties
* @return
*/
private static String[] getImportCommand(Properties properties) {
String username = properties.getProperty("jdbc.username");//
String password = properties.getProperty("jdbc.password");//
String host = properties.getProperty("jdbc.host");//
String port = properties.getProperty("jdbc.port");//
String importDatabaseName = properties.getProperty("jdbc.importDatabaseName");//
String importPath = properties.getProperty("jdbc.importPath");//
// ,
String loginCommand = new StringBuffer().append("mysql -u").append(username).append(" -p").append(password).append(" -h").append(host)
.append(" -P").append(port).toString();
// ,
String switchCommand = new StringBuffer("use ").append(importDatabaseName).toString();
// ,
String importCommand = new StringBuffer("source ").append(importPath).toString();
//
String[] commands = new String[] {loginCommand, switchCommand, importCommand};
return commands;
}
}
原文住所:http://elim.iteye.com/blog/1413830