java操作:mysql資料庫匯入、匯出
阿新 • • 發佈:2019-02-02
首先在這之前,我將mysql資料庫的密碼、使用者名稱等一些資訊儲存在對應的工程的屬性檔案中,以便在之後呼叫。
還有就是匯出的一個數據庫中包含的是多個表格,以資料庫為單位匯出,
這裡記錄一下屬性檔案的建立過程
在工程的 src 檔案下右擊選擇 new ->file -> 檔名以 .properties 結尾就可以了
屬性檔案的內容
jdbc.host=127.0.0.1 // localhost
jdbc.exportDatabaseName=students // 要匯出的資料庫名稱
jdbc.password=123456 // 資料庫密碼
jdbc.importPath=/Users/ sz/Desktop/students.sql // 要匯入的資料庫所在路徑
jdbc.port=3306 // 埠號
MysqlPath=/usr/local/mysql/bin/ //mysql下的bin檔案的路徑
jdbc.exportPath=/Users/sz/Desktop/students.sql // 匯出的資料庫存放路徑
jdbc.username=root // 使用者名稱
jdbc.importDatabaseName=DesignModel // 要匯入的目標資料庫
程式碼如下:
package dataInput;
import java.io.IOException;
import java.io.*;
import java.util.Properties;
public class ImportAndExport {
// 實現資料庫的匯出
public static void exportSql() throws IOException{
Properties properties = new Properties();
// 讀取屬性檔案
properties.load(Import.class.getClassLoader().getResourceAsStream("jdbc.properties"));
Runtime runtime = Runtime.getRuntime();
String command = getExportCommand(properties);
// 這裡其實是在命令視窗中執行的 command 命令列
runtime.exec(command);
}
// 實現資料庫的匯入
public static void importSql() throws IOException {
Properties properties = new Properties();
// 讀取屬性檔案
properties.load(Import.class.getClassLoader().getResourceAsStream("jdbc.properties"));
Runtime runtime = Runtime.getRuntime();
//把所執行的命令將以字串陣列的形式出現
String cmdarray[] = getImportCommand(properties);//根據屬性檔案的配置獲取資料庫匯入所需的命令,組成一個數組
Process process = runtime.exec(cmdarray[0]);
//執行了第一條命令以後已經登入到mysql了,所以之後就是利用mysql的命令視窗
java.io.OutputStream os = process.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(os);
//命令1和命令2要放在一起執行
// 這裡會執行後面的程式碼, 將命令輸出到mysql的命令視窗,進行執行
writer.write(cmdarray[1] + "\r\n" + cmdarray[2]);
writer.flush();
writer.close();
os.close();
}
// 得到 匯入 資料庫的命令
// 得到 匯入資料 的 命令列語句
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 MysqlPath = properties.getProperty("MysqlPath"); // 路徑是mysql中 bin 檔案 的位置
//第一步,獲取登入命令語句
String loginCommand = new StringBuffer().append(MysqlPath).append("mysql -h").append(host).append(" -u").append(username).append(" -p").append(password)
.append(" -P").append(port).toString();
//第二步,獲取切換資料庫到目標資料庫的命令語句
String switchCommand = new StringBuffer().append("use ").append(importDatabaseName).toString();
//第三步,獲取匯入的命令語句
String importCommand = new StringBuffer(" source ").append(importPath).toString();
//需要返回的命令語句陣列
String[] commands = new String[] {loginCommand, switchCommand, importCommand};
return commands;
}
// 得到 匯出資料 的 命令列語句
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");//匯出路徑
String MysqlPath = properties.getProperty("MysqlPath"); // 路徑是mysql中 bin 檔案 的位置
//注意哪些地方要空格,哪些不要空格
command.append(MysqlPath).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();
}
}
有錯誤地方希望指出。