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
阅读全文
0 0
- **.sql文件导入mysql数据库
- sql文件导入mysql数据库
- MySQL数据库导入.sql文件
- SQL文件导入MySQL数据库
- mysql数据库sql文件导入导出命令
- MYSQL 导入sql文件到数据库
- mysql数据库的导入导出.sql文件
- .sql文件导入mysql数据库中有图有真相
- mysql数据库导入sql文件不成功
- 将.sql文件导入mysql数据库
- MYSQL数据库导入SQL文件出现乱码
- mysql导入sql数据库
- sql 导入 mysql数据库
- PowerDesigner设计建造MySQL数据库(mysql 导入sql文件)
- PowerDesigner设计建造MySQL数据库(mysql 导入sql文件)
- 使用mysql命令行导入sql文件到mysql数据库
- mysql ---bat文件方式创建数据库、导入sql文件
- mysql导入.sql文件
- 为RecyclerView添加分隔线
- redis 数据类型
- C++:cin\cin.getline()\getline()用法:
- 矩阵的螺旋排列 Python实现
- Numpy-shape函数
- SQL文件导入MySQL数据库
- VAE背后的哲学思想及数学原理
- 牛客练习赛7 B
- Python入门之一:list列表
- 【转载】SciPy for .NET & IronPython
- ssm项目一直报错,陷入死循环,解决办法
- 论文阅读笔记-Siamese instance search for tracking(转)
- 基于Python的nessus API简析——监听&获取扫描结果
- spring动态加载时间表达式并生成定时任务