1、直接導出數據庫中的全部表的數據
java
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import com.util.ExportSQLUtil; /** * 測試類導出SQL到本地 * @author willdas * */ public class ExportSQL { public static void main(String args[]) { FileInputStream in = null; try { in = new FileInputStream("src\\db.properties"); // 讀取數據庫配置文件 Properties properties = new Properties(); properties.load(in); // 導出路徑 String exportPath = "H:\\test.sql"; String stringSql = ExportSQLUtil.getExportSQL(properties,exportPath); // 運行導出命令 String os = System.getProperty("os.name"); // 判斷系統 if(os.toLowerCase().startsWith("win")){ Process process = Runtime.getRuntime().exec(new String[]{"cmd","/c",stringSql}); // windows process.waitFor(); }else if(os.toLowerCase().startsWith("linux")){ Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",stringSql}); //linux process.waitFor(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * @Description: TODO(導出SQL工具包) * @author willdas */ public class ExportSQLUtil { public static String getExportSQL(Properties properties,String exportPath){ StringBuffer sbf = new StringBuffer(); 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 dataBaseName = properties.getProperty("jdbc.dataBaseName"); sbf.append("mysqldump"); sbf.append(" -h").append(host); // 主機 sbf.append(" -P").append(port); // 端口號*大寫P sbf.append(" -u").append(username); // 用戶名 sbf.append(" -p").append(password); // 密碼 sbf.append(" ").append(dataBaseName); // 數據庫名 sbf.append(" > "); sbf.append(exportPath); // 導出路徑 return sbf.toString(); } } 配置文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/show jdbc.username=root jdbc.password=root jdbc.host=127.0.0.1 jdbc.port=3366 jdbc.dataBaseName=mytest
2、按查詢條件導出多張表的數據mysql
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import com.util.ExportSQLUtil; /** * 測試類導出SQL到本地 * 根據關聯id 導出數據 * @author willdas * */ public class ExportSQL { public static void main(String args[]) { FileInputStream in = null; try { in = new FileInputStream("src\\db.properties"); // 讀取數據庫配置文件 Properties properties = new Properties(); properties.load(in); // 導出路徑 String exportPath = "H:\\test.sql"; //查詢條件 String classId = "102"; String stringSql = ExportSQLUtil.getExportSQL(properties,exportPath,classId); // 運行導出命令 String os = System.getProperty("os.name"); // 判斷系統名稱 if(os.toLowerCase().startsWith("win")){ Process process = Runtime.getRuntime().exec(new String[]{"cmd","/c",stringSql}); // windows process.waitFor(); }else if(os.toLowerCase().startsWith("linux")){ Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",stringSql}); //linux process.waitFor(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * @Description: TODO(導出SQL工具包) * @author willdas */ public class ExportSQLUtil { public static String getExportSQL(Properties properties,String exportPath,String classId){ 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 dataBaseName = properties.getProperty("jdbc.dataBaseName"); StringBuffer sbf = new StringBuffer(); sbf.append("mysqldump -h"+host+" -P"+port+" -u"+username+" -p"+password+" "+dataBaseName); sbf.append(" class "); // 班級表 sbf.append(" teacher "); // 教師表 sbf.append(" student "); // 學生表 sbf.append("--where=\"class_id="+classId+"\""); // 查詢條件 sbf.append(" >> "); sbf.append(exportPath); // 導出路徑 return sbf.toString(); } } 配置問件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/show jdbc.username=root jdbc.password=root jdbc.host=127.0.0.1 jdbc.port=3366 jdbc.dataBaseName=mytest
數據庫表結構linux