温馨提示×

javaweb怎么导入mysql

小新
188
2021-03-23 09:02:23
栏目: 云计算
亿速云mysql数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

javaweb怎么导入mysql

javaweb导入mysql的示例代码:

import java.io.File;

import java.io.IOException;

/**

MySQL数据库导入

*

* @author GaoHuanjie

*/

public class MySQLDatabaseImport {

/**

* Java实现MySQL数据库导入

*

* @author GaoHuanjie

* @param hostIP MySQL数据库所在服务器地址IP

* @param userName 数据库用户名

* @param password 进入数据库所需要的密码

* @param importFilePath 数据库文件路径

* @param sqlFileName 数据库文件名

* @param databaseName 要导入的数据库名

* @return 返回true表示导入成功,否则返回false

*/

public static boolean importDatabase(String hostIP, String userName, String password, String importFilePath, String sqlFileName, String databaseName) {

File saveFile = new File(importFilePath);

if (!saveFile.exists()) {// 如果目录不存在

saveFile.mkdirs();// 创建文件夹

}

if (!importFilePath.endsWith(File.separator)) {

importFilePath = importFilePath + File.separator;

}

StringBuilder stringBuilder=new StringBuilder();

stringBuilder.append("mysql").append(" -h").append(hostIP);

stringBuilder.append(" -u").append(userName).append(" -p").append(password);

stringBuilder.append(" ").append(databaseName);

stringBuilder.append(" <").append(importFilePath).append(sqlFileName);

try {

Process process = Runtime.getRuntime().exec("cmd /c "+stringBuilder.toString());//必须要有“cmd /c ”

if (process.waitFor() == 0) {// 0 表示线程正常终止。

return true;

}

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

return false;

}

public static void main(String[] args) throws InterruptedException {

if (importDatabase("172.16.0.127""root""123456""D:\\backupDatabase""2014-10-14.sql""GHJ")) {

System.out.println("数据库导入成功!!!");

else {

System.out.println("数据库导入失败!!!");

}

}

}

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

推荐阅读:eclipse如何导入javaweb项目

0