zoukankan      html  css  js  c++  java
  • Java实现MySQL数据库导入

      距离上班还有一段时间。现在总结一下如何使用Java语言实现MySQL数据库导入:

            首先新建名为test的数据库;

            其次执行下面Java代码:

    [java] view plain copy
     
    1. import java.io.File;  
    2. import java.io.IOException;  
    3.   
    4. /** 
    5.  * MySQL数据库导入 
    6.  *  
    7.  * @author GaoHuanjie 
    8.  */  
    9. public class MySQLDatabaseImport {  
    10.   
    11.     /** 
    12.      * Java实现MySQL数据库导入 
    13.      *  
    14.      * @author GaoHuanjie 
    15.      * @param hostIP MySQL数据库所在服务器地址IP 
    16.      * @param userName 数据库用户名 
    17.      * @param password 进入数据库所需要的密码 
    18.      * @param importFilePath 数据库文件路径 
    19.      * @param sqlFileName 数据库文件名 
    20.      * @param databaseName 要导入的数据库名 
    21.      * @return 返回true表示导入成功,否则返回false。 
    22.      */  
    23.     public static boolean importDatabase(String hostIP, String userName, String password, String importFilePath, String sqlFileName, String databaseName) {  
    24.         File saveFile = new File(importFilePath);  
    25.         if (!saveFile.exists()) {// 如果目录不存在  
    26.             saveFile.mkdirs();// 创建文件夹  
    27.         }  
    28.         if (!importFilePath.endsWith(File.separator)) {  
    29.             importFilePath = importFilePath + File.separator;  
    30.         }         
    31.   
    32.         StringBuilder stringBuilder=new StringBuilder();  
    33.         stringBuilder.append("mysql").append(" -h").append(hostIP);  
    34.         stringBuilder.append(" -u").append(userName).append(" -p").append(password);  
    35.         stringBuilder.append(" ").append(databaseName);  
    36.         stringBuilder.append(" <").append(importFilePath).append(sqlFileName);  
    37.         try {  
    38.             Process process = Runtime.getRuntime().exec("cmd /c "+stringBuilder.toString());//必须要有“cmd /c ”  
    39.             if (process.waitFor() == 0) {// 0 表示线程正常终止。  
    40.                 return true;  
    41.             }  
    42.         } catch (IOException e) {  
    43.             e.printStackTrace();  
    44.         } catch (InterruptedException e) {  
    45.             e.printStackTrace();  
    46.         }  
    47.         return false;  
    48.     }  
    49.   
    50.     public static void main(String[] args) throws InterruptedException {  
    51.         if (importDatabase("172.16.0.127", "root", "123456", "D:\backupDatabase", "2014-10-14.sql", "GHJ")) {  
    52.             System.out.println("数据库导入成功!!!");  
    53.         } else {  
    54.             System.out.println("数据库导入失败!!!");  
    55.         }  
    56.     }  
    57. }  
  • 相关阅读:
    Spring.NET学习笔记(4)对象作用域和类型转换
    spring 依赖注入有什么好处
    加入收藏和设为主页js代码
    hdoj_1027_code
    【C#.NET】ASP.NET状态管理之一:Cookie
    【C#.NET】自定义“验证码”控件(转)
    【C#.NET】C#六种集合性能比较
    【C#.NET】特性和属性
    【C#.NET】ASP.NET 2.0 数据绑定概述(转)
    【C#.NET】ASP.NET状态管理之三:Application
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6255102.html
Copyright © 2011-2022 走看看