Get the App
SLTechnology News&Howtos  ›  Database  › 

How to back up MySQL database in Java

Shulou Source: shulou.com Published: 2022-05-31 20:45:44 10月15日 Update

This article shows you how to back up the MySQL database in Java, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Import java.io.File;import java.io.IOException / * MySQL database backup * * @ author GaoHuanjie * / public class MySQLDatabaseBackup {/ * Java code to export MySQL database * * @ author GaoHuanjie * @ param hostIP MySQL database server address IP * @ param userName user name * @ param password entry required to enter the database According to the password required by the library * @ param savePath database export file save path * @ param fileName database export file name * @ param databaseName database name to be exported * @ return returns true to indicate that the export is successful Otherwise, false is returned. * / public static boolean exportDatabaseTool (String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {File saveFile = new File (savePath); if (! saveFile.exists ()) {/ / if saveFile.mkdirs () does not exist in the directory / / create a folder} if (! savePath.endsWith (File.separator)) {savePath = savePath + File.separator;} StringBuilder stringBuilder = new StringBuilder (); 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 {Process process = Runtime.getRuntime () .exec (stringBuilder.toString ()); if (process.waitFor () = = 0) {/ / 0 indicates that the thread terminates normally. Return true;}} catch (IOException e) {e.printStackTrace ();} catch (InterruptedException e) {e.printStackTrace ();} return false } public static void main (String [] args) throws InterruptedException {if (exportDatabaseTool ("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14.sql", "test") {System.out.println ("Database backup successful!") ;} else {System.out.println ("Database backup failed!") The above content is how to back up the MySQL database in Java. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Data database backup file success content skills knowledge concise concise code address password is location file name folder article more server Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MariaDB Linux macOS Xiaomi