简单备份并从Java为MySQL数据库还原
如何从Java代码备份MySQL数据库,以便通过
- 动态分配保存路径。
- 路径中的空间不会产生问题。
- 使用执行JAR文件生成路径。
- DBNAME,DBUSERNAME或DBPASS被动态分配。
- 创建一个专用文件夹以保存备份文件。
How to backup a mysql database from a java code such that:
- It's saving path is dynamically allocated.
- Spaces in Path do not create problems.
- Path is generated using the executing jar file.
- DBname,DBusername or DBpass are dynamically allotted.
- Creating a specialized folder to save the backup file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注意:下面给出的代码是解决问题的一种方法,可能不是最好的方法。代码中的一切都可以改变。如果您在环境变量中没有mySQL,请在mysqldump和mysql之前添加路径(例如xampp,c:\ xampp \ mysql \ mysql \ bin \ mysqldump)
(希望,这会解决您的问题。完全找出所有内容并正确地实现)
备份方法:
还原方法:
Note: The codes given below are one way of solving the problem and probably not the best method. Everything is changeable inside the code. If you do not have mysql in environment variables, add the path before mysqldump and mysql (e.g. For XAMPP, C:\xampp\mysql\bin\mysqldump)
(Hope, this will solve your problems. Took me a day to completely figure out everything and implement them properly)
Method for Backup:
Method for Restore:
如果 hibernate 配置正确,这是蛋糕:
If Hibernate is configured properly, this is cake:
除了Chettyharish的答案之外,如果您的服务器OS是Ubuntu,则路径应该具有前斜线'/'而不是BackSlash'\',例如
/path/to/to/to/your/your/file
:例如:
String savepath =“ \”“ + jardir +” \\ backup \\“ +” backup.sql \“”;
将是:
string savepath =“/”/“/” + jardir +“/backup/backup。 SQL“
In addition to chettyharish's answer, if your server os is ubuntu the path should have front slash '/' instead of backslash '\' such as
/path/to/your/file
For example:
String savePath = "\"" + jarDir + "\\backup\\" + "backup.sql\"";
Will be :
String savePath="/"+jarDir+"/backup/backup.sql"