java调用mysql命令导入脚本方案
发布日期:2021-11-16 18:49:56
浏览次数:1
分类:技术文章
本文共 5691 字,大约阅读时间需要 18 分钟。
背景:
使用java调用ibatis-common-2.jar中的ScriptRunner类中的runScript方法执行mysql脚本时,表能创建成功,但是FUNCTION没有成功,/!50003…/中注释的内容只有mysql本身的解析器可以执行,其他的程序会忽略掉,所以没有成功。既然只有mysql才能完整的解析脚本,那用mysql本身的命令去执行就好了,本方案才应运而生。以下为范例:
/*Table structure for table `school` */DROP TABLE IF EXISTS `school`;CREATE TABLE `school` ( `schoolId` int(11) NOT NULL AUTO_INCREMENT COMMENT '校区编号', `schoolName` varchar(50) NOT NULL COMMENT '校区名称', `schoolShortName` varchar(20) DEFAULT NULL COMMENT '校区名称简写' PRIMARY KEY (`schoolId`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;/* Function structure for function `f_getSchoolShortName` *//*!50003 DROP FUNCTION IF EXISTS `f_getSchoolShortName` */;DELIMITER $$/*!50003 CREATE FUNCTION `f_getSchoolShortName`(schId int) RETURNS varchar(50) CHARSET utf8BEGIN return(select schoolShortName from school where schoolId=schId); END */$$DELIMITER ;
1.导出
使用命令导出脚本
数据+结构+(函数+存储过程) mysqldump -hip -uroot -pxxxx –opt -R 数据库名> c:/script.sql2.合并
package com.thatluck.test.sql;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileReaderUtil { public static void main(String[] args) { try { mergeFile(new File("C:/test"), new File("c:/one/all003.sql")); // copyFiles(new File("G:/学习资料/笔记"),new File("G:/Test")); } catch (IOException e) { e.printStackTrace(); } } /** * 拷贝某个文件目录下面的所有文件, * * @param sourcePath * 原文件目录 * @param desPath * 目的文件目录 */ public static void copyFiles(File sourceFile, File desFile) throws IOException { if (sourceFile.isFile()) { File file = new File(desFile.getPath() + "/" + sourceFile.getName()); FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(file); int len = 0; byte[] buf = new byte[1024]; while ((len = fis.read(buf)) != -1) fos.write(buf, 0, len); } else { File dir = new File(desFile.getPath() + "/" + sourceFile.getName()); if (!dir.exists()) dir.mkdir(); String[] names = sourceFile.list(); for (int i = 0; i < names.length; i++) { copyFiles(new File(sourceFile.getPath() + "/" + names[i]), dir); } } } /** * 将一个文件目录下面的所有文件独到一个文件中的方法(主要用于将很多文本文件合并到一起) * * @param sourceFile * @param decFile * @return * @throws IOException */ public static File mergeFile(File sourceFile, File decFile) throws IOException { String[] fileList = sourceFile.list(); FileInputStream fis = null; FileOutputStream fos = null; File file=null; try{ for (String string : fileList) { file = new File(sourceFile.getPath() + "/" + string); if (!file.isDirectory()) { fis = new FileInputStream(file); fos = new FileOutputStream(decFile, true); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) != -1) fos.write(buffer, 0, len); } else { decFile = mergeFile(file, decFile); } } }finally{ fis.close(); fos.close(); } return decFile; }}
3.导入
/** * Administrator */package com.thatluck.test;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import com.thatluck.test.sql.FileReaderUtil;/** * *@ClassName:DBTest *@author: thatluck *@date:2016年8月5日 上午8:38:16 *@version: V2.0 */public class DBTest { /** * * @author thatluck * @date 2016年8月5日 上午8:38:16 * @param args */ public static void main(String[] args) throws Exception{ load(); } public static void load() throws Exception{ FileReaderUtil.mergeFile(new File("C:/test"), new File("c:/one/all.sql")); OutputStream out = null; OutputStreamWriter writer = null; BufferedReader br = null; String fPath = "c:/one/all.sql"; try { Runtime rt = Runtime.getRuntime(); // 调用 mysql 的 cmd: Process child = rt.exec(" mysql -hxx.xx.xx.xx -uroot -pxxx 数据苦命"); String inStr; StringBuffer sb = new StringBuffer(""); String outStr; br = new BufferedReader(new InputStreamReader( new FileInputStream(fPath), "utf8")); while ((inStr = br.readLine()) != null) { sb.append(inStr + "\r\n"); System.out.println(inStr); } outStr = sb.toString(); out = child.getOutputStream();//控制台的输入信息作为输出流 writer = new OutputStreamWriter(out, "utf8"); writer.write(outStr); // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免 writer.flush(); // 别忘记关闭输入输出流 System.out.println("/* Load OK! */"); } catch (Exception e) { e.printStackTrace(); }finally{ try { out.close(); br.close(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
参考:
1.
2. 3.转载地址:https://blog.csdn.net/thatluck/article/details/52128121 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月20日 16时41分37秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
千人千面Elasticsearch实战学习笔记
2021-07-02
最大子数组问题(递归)(java)
2021-07-02
2021年第十二届蓝桥杯软件赛省赛第二场 C/C++ 大学 A 组
2021-07-02
2020年哨兵数据批量下载(USGS)
2021-07-02
简单3步快速生成千万级别mysql测试数据库,模拟电商数据
2021-07-02
EasyDSS平台接入设备量过多的情况下如何进行批量推流测试?
2021-07-02
mysql数据库操作基础
2021-07-02
Mariadb基础管理
2021-07-02
awk 的内置变量 NF、NR、FNR、FS、OFS、RS、ORS
2019-04-29
CentOS系统内核升级攻略
2019-04-29
linux系统时区修改(Debian的主机和docker)
2019-04-29
docker-compose 安装
2019-04-29
crontab 定时任务
2019-04-29
查看docker veth pair与宿主机上网卡的对应关系
2019-04-29
使用 GitLab CI 进行持续集成的一些踩坑
2019-04-29
企业云盘给贸易业带来新的效益
2019-04-29