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

            距离上班另一段时间。如今总结一下怎样使用Java语言实现MySQL数据库导入:

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

            其次运行以下Java代码:

    import java.io.File;
    import java.io.IOException;
    
    /**
     * MySQL数据库导入
     * 
     * @author GaoHuanjie
     */
    public class MySQLDatabaseImport {
    
    	/**
    	 * Java实现MySQL数据库导入
    	 * 
    	 * @author GaoHuanjie
    	 * @param hostIP MySQL数据库所在server地址IP
    	 * @param userName 数据库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("数据库导入失败!!

    。"); } } }

  • 相关阅读:
    NBOJv2 1022 短信篮球(种类并查集)
    POJ 1182 食物链(种类并查集)
    虚拟机下samba简单安装配置
    gcc5.1.0升级指南
    廖雪峰Java16函数式编程-2Stream-7其他操作
    廖雪峰Java16函数式编程-2Stream-6reduce
    廖雪峰Java16函数式编程-2Stream-5filter
    廖雪峰Java16函数式编程-2Stream-4map
    廖雪峰Java16函数式编程-2Stream-2创建Stream
    廖雪峰Java16函数式编程-2Stream-1Stream简介
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6738054.html
Copyright © 2011-2022 走看看