I know this question is a bit old, but I think people are still looking for an answer like me.
Basically, I wanted to make two buttons in my GUI (one to import, and the other to export) and generate an SQL file, I tested the chosen solution to start a process from java and execute it with Runtime but it didn't work, I had an Access Denied error eventhought I am the only user in my computer. After some researches, I found this library (mysql-backup4j) and did this code :
EXPORT FUNCTION :
Properties properties = new Properties();
properties.setProperty(MysqlExportService.DB_NAME, "DATABASE_NAME");
properties.setProperty(MysqlExportService.DB_USERNAME, "DATABASE_USERNAME");
properties.setProperty(MysqlExportService.DB_PASSWORD, "DATABASE_PWD");
properties.setProperty(MysqlExportService.TEMP_DIR, new File(System.getProperty("user.dir") + "\\database_dump").getAbsolutePath());
properties.setProperty(MysqlExportService.PRESERVE_GENERATED_ZIP, "true");
MysqlExportService mysqlExportService = new MysqlExportService(properties); mysqlExportService.export();
IMPORT FUNCTION (the user chooses the SQL file) :
FileChooser fc = new FileChooser();
List<String> sqlExtensions = new ArrayList<>(List.of("*.sql", "*.SQL"));
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("Fichier SQL", sqlExtensions));
File f = fc.showOpenDialog(null);
if (f != null) {
System.out.println("database path : " + f.getAbsolutePath());
String sql = new String(Files.readAllBytes(Paths.get(f.getAbsolutePath())));
boolean res = MysqlImportService.builder()
.setDatabase("DATABASE_NAME").setSqlString(sql)
.setUsername("DATABASE_USERNAME").setPassword("DATABASE_PWD").setDeleteExisting(true)
.setDropExisting(true)
.importDatabase();
}
P.S.: I am using JavaFX for the GUI with JDK11.
Reference : How to backup your MySQL database programmatically using mysql-backup4j