SQL文件导入MySQL数据库

来源:互联网 发布:ubuntu看存储空间 编辑:程序博客网 时间:2024/05/02 10:58

SQL文件导入MySQL数据库

1、MySQL设置环境变量
MYSQL_HOME:C:\Program Files (x86)\MySQL\MySQL Server 5.5
path:%MYSQL_HOME%bin

这里写图片描述

这里写图片描述

环境变量设置后,打开cmd命令窗口,输入mysql -u用户名 -p密码
登陆mysql
这里写图片描述

2、MySQL常用命令

source命令:

#进入MySQL数据库控制台,如mysql -uroot -prootmysql>user 数据库#使用source命令后面加脚本文件mysql>source d:/test.sql

cmd窗口下操作命令:

mysql -u用户名 -p密码 -D数据库<sql文件[全路径全名]如mysql -uroot -proot -Dtest<D:\test\test.sql

3、实现代码

package com.example.service;import java.io.*;import java.util.Properties;public class ImportData {    private static boolean importSQL(Properties properties) {        String host = properties.getProperty("jdbc.host");        String port = properties.getProperty("jdbc.port");        String username = properties.getProperty("jdbc.username");        String password = properties.getProperty("jdbc.password");        String databasename = properties.getProperty("jdbc.databasename");        String file = properties.getProperty("jdbc.file");        StringBuilder stringBuilder = new StringBuilder();        stringBuilder.append("cmd /C ");        stringBuilder.append(" mysql ").append(" -h").append(host).append(" -p").append(port);        stringBuilder.append(" -u").append(username).append(" -p").append(password);        stringBuilder.append(" --default-character-set=utf8 ").append(databasename);        stringBuilder.append(" < ").append(file);        try {            Process process = Runtime.getRuntime().exec(stringBuilder.toString());            input(process.getInputStream());            if (process.waitFor() == 0) {                return true;            }        } catch (IOException | InterruptedException e) {            e.printStackTrace();        }        return false;    }    private static void input(final InputStream inputStream) {        new Thread(                new Runnable() {                    @Override                    public void run() {                        Reader reader = new InputStreamReader(inputStream);                        BufferedReader bf = new BufferedReader(reader);                        String line = null;                        try {                            while ((line = bf.readLine()) != null) {                                System.out.println(line);                            }                        } catch (IOException e) {                            e.printStackTrace();                        } finally {                            try {                                reader.close();                                bf.close();                            } catch (IOException e) {                                e.printStackTrace();                            }                        }                    }                }        ).start();    }    public static void main(String[] args) {        Properties properties = new Properties();        InputStream is = ImportData.class.getClassLoader().getResourceAsStream("jdbc.properties");        try {            properties.load(is);        } catch (IOException e) {            e.printStackTrace();        }        if (importSQL(properties)) {            System.out.println("导入成功");        } else {            System.out.println("导入失败");        }    }}

资源文件:

jdbc.host=127.0.0.1jdbc.port=3306jdbc.username=rootjdbc.password=rootjdbc.databasename=testjdbc.file=D\:\\Workspaces\\data.sql
原创粉丝点击