距离上班还有一段时间。现在总结一下如何使用Java语言实现MySQL数据库导入:
首先新建名为test的数据库;
其次执行下面Java代码:
- import java.io.File;
- import java.io.IOException;
-
- public class MySQLDatabaseImport {
-
-
- 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());
- if (process.waitFor() == 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("数据库导入失败!!!");
- }
- }
- }
Java实现MySQL数据库导入
原文:http://www.cnblogs.com/love540376/p/6255102.html