Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main.java.fa.exceltolist;
- import java.awt.HeadlessException;
- import java.io.File;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.security.CodeSource;
- import javax.swing.JOptionPane;
- public class DemoRestore {
- public static void restoreDbFromSQL(String s) {
- try {
- /*NOTE: String s is the mysql file name including the .sql in its name*/
- /*NOTE: Getting path to the Jar file being executed*/
- /*NOTE: YourImplementingClass-> replace with the class executing the code*/
- CodeSource codeSource = DemoRestore.class.getProtectionDomain().getCodeSource();
- File jarFile = new File(codeSource.getLocation().toURI().getPath());
- String jarDir = jarFile.getParentFile().getPath();
- /*NOTE: Creating Database Constraints*/
- String dbName = "easypoe";
- String dbUser = "root";
- String dbPass = "root";
- String dirPath= "D:/script.sql";
- /*NOTE: Creating Path Constraints for restoring*/
- String restorePath = jarDir + "\\backup\\" + s;
- /*NOTE: Used to create a cmd command*/
- /*NOTE: Do not create a single large string, this will cause buffer locking, use string array*/
- // String[] executeCmd = new String[]{"mysql", dbName, "-u" + dbUser, "-p" + dbPass, "-e",
- // " source " + restorePath};
- // String executeCmd = "mysqldump --user=" + dbUser + " --password=" + dbPass + " "
- // + dbName + " < " + "D:\backup" + "\backuppro.sql";
- String executeCmd = "mysql -u " + dbUser + " -p" + dbPass + "-s"+ "<" +dirPath;
- /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
- Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
- int processComplete = runtimeProcess.waitFor();
- /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
- if (processComplete == 0) {
- JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);
- } else {
- JOptionPane.showMessageDialog(null, "Error at restoring");
- }
- } catch (URISyntaxException | IOException | InterruptedException | HeadlessException ex) {
- JOptionPane.showMessageDialog(null, "Error at restoreDbFromSQL! " + ex.getMessage());
- }
- }
- /*public boolean restoreDatabase(String dbUserName, String dbName, String dbPassword, String source) {
- String[] executeCmd = new String[] { "mysql -u " + dbUserName + "-p" + dbName + "<" + source, dbPassword };
- Process runtimeProcess;
- try {
- runtimeProcess = Runtime.getRuntime().exec(executeCmd);
- int processComplete = runtimeProcess.waitFor();
- if (processComplete == 0) {
- System.out.println("Backup restored successfully with " + source);
- return true;
- } else {
- System.out.println("Could not restore the backup " + source);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return false;
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement