Java中怎么备份MySQL数据库
本篇文章为大家展示了Java中怎么备份MySQL数据库,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
importjava.io.File;importjava.io.IOException;/***MySQL数据库备份**@authorGaoHuanjie*/publicclassMySQLDatabaseBackup{/***Java代码实现MySQL数据库导出**@authorGaoHuanjie*@paramhostIPMySQL数据库所在服务器地址IP*@paramuserName进入数据库所需要的用户名*@parampassword进入数据库所需要的密码*@paramsavePath数据库导出文件保存路径*@paramfileName数据库导出文件文件名*@paramdatabaseName要导出的数据库名*@return返回true表示导出成功,否则返回false。*/publicstaticbooleanexportDatabaseTool(StringhostIP,StringuserName,Stringpassword,StringsavePath,StringfileName,StringdatabaseName){FilesaveFile=newFile(savePath);if(!saveFile.exists()){//如果目录不存在saveFile.mkdirs();//创建文件夹}if(!savePath.endsWith(File.separator)){savePath=savePath+File.separator;}StringBuilderstringBuilder=newStringBuilder();stringBuilder.append("mysqldump").append("--opt").append("-h").append(hostIP);stringBuilder.append("--user=").append(userName).append("--password=").append(password).append("--lock-all-tables=true");stringBuilder.append("--result-file=").append(savePath+fileName).append("--default-character-set=utf8").append(databaseName);try{Processprocess=Runtime.getRuntime().exec(stringBuilder.toString());if(process.waitFor()==0){//0表示线程正常终止。returntrue;}}catch(IOExceptione){e.printStackTrace();}catch(InterruptedExceptione){e.printStackTrace();}returnfalse;}publicstaticvoidmain(String[]args)throwsInterruptedException{if(exportDatabaseTool("172.16.0.127","root","123456","D:/backupDatabase","2014-10-14.sql","test")){System.out.println("数据库备份成功!!!");}else{System.out.println("数据库备份失败!!!");}}}
上述内容就是Java中怎么备份MySQL数据库,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。