
正文
java命令行导出、导入sql文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
@IocBean
public class SqlCommandModel{ //用户名
@Inject("java:$conf.get('jdbc.username')")
private String username;
//用户密码
@Inject("java:$conf.get('jdbc.password')")
private String password;
//从哪个主机导出数据库,如果没有指定这个值,则默认取localhost
@Inject("java:$conf.get('jdbc.host')")
private String host;
//使用的端口号
@Inject("java:$conf.get('jdbc.port')")
private String port;
// 路径是mysql中 bin 文件 的位置
@Inject("java:$conf.get('mysqlPath')")
private String mysqlPath; // 导出数据库名称
@Inject("java:$conf.get('jdbc.exportDatabaseName')")
private String exportMysqlDataBase; // 导入数据库名称
@Inject("java:$conf.get('jdbc.importDatabaseName')")
private String importDataBase; // # 用户名
// jdbc.username=root
// # 数据库密码
// jdbc.password=123456
// # localhost
// jdbc.host=127.0.0.1
// # 端口号
// jdbc.port=3306
// # mysql下的bin文件的路径 (linux)
// mysqlPath=usr/local/mysql/bin
//
// # 要导出的数据库名称
// jdbc.exportDatabaseName=bankmanaer
// # 要导入的目标数据库
// jdbc.importDatabaseName=bankmanaer /**
* 获取导出命令
* @param exportDatabaseName 表名称
* @param exportPath 导出路径
* @return
*/
public String getExportCommand(String exportDataTableName,String exportPath) { StringBuffer command = new StringBuffer();
//注意哪些地方要空格,哪些不要空格
command.append("mysqldump -u ").append(username).append(" -p").append(password)//密码是用的小p,而端口是用的大P。
.append(" -h").append(host).append(" -P").append(port).append(" ").append(exportMysqlDataBase + " ").append(exportDataTableName).append(" -r ").append(exportPath); // 如果是linux系统上则加上数据库路径
// command.append(mysqlPath).append("mysqldump -u ").append(username).append(" -p").append(password)//密码是用的小p,而端口是用的大P。
// .append(" -h").append(host).append(" -P").append(port).append(" ").append(exportMysqlDataBase + " ").append(exportDataTableName).append(" -r ").append(exportPath); return command.toString();
} // 得到 导入 数据库的命令
// 得到 导入数据 的 命令行语句
/**
*
* @param importPath 导入文件所在路径
* @return
*/
public String[] getImportCommand(String importPath) { //第一步,获取登录命令语句
String loginCommand = new StringBuffer().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(importDataBase).toString();
//第三步,获取导入的命令语句
String importCommand = new StringBuffer(" source ").append(importPath).toString();
//需要返回的命令语句数组 String[] commands = new String[] {loginCommand, switchCommand, importCommand}; return commands;
} public static void main(String[] args) throws InterruptedException, IOException { SqlCommandModel sqlCommandModel = new SqlCommandModel(); String[] tables = new String[5]; // 这里其实是在命令窗口中执行的 command 命令行
List<Process> list = new ArrayList<>();
for (int i = 0; i < tables.length; i++) {
String exportCommand = sqlCommandModel.getExportCommand(tables[i], "D:\\" + tables[i] + ".sql");
Runtime runtime = Runtime.getRuntime();
// 这里其实是在命令窗口中执行的 command 命令行
list.add(runtime.exec(exportCommand));
}
//等待所有子进程处理完毕
for (Process process : list) {
while(process.waitFor() != 0){
;
}
} } }





