Advertisement
Guest User

Untitled

a guest
Feb 28th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package main.java.fa.exceltolist;
  2.  
  3. import java.awt.HeadlessException;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.net.URISyntaxException;
  7. import java.security.CodeSource;
  8.  
  9. import javax.swing.JOptionPane;
  10.  
  11. public class DemoRestore {
  12.    
  13.     public static void restoreDbFromSQL(String s) {
  14.         try {
  15.             /*NOTE: String s is the mysql file name including the .sql in its name*/
  16.             /*NOTE: Getting path to the Jar file being executed*/
  17.             /*NOTE: YourImplementingClass-> replace with the class executing the code*/
  18.             CodeSource codeSource = DemoRestore.class.getProtectionDomain().getCodeSource();
  19.             File jarFile = new File(codeSource.getLocation().toURI().getPath());
  20.             String jarDir = jarFile.getParentFile().getPath();
  21.  
  22.             /*NOTE: Creating Database Constraints*/
  23.              String dbName = "easypoe";
  24.              String dbUser = "root";
  25.              String dbPass = "root";
  26.              String dirPath= "D:/script.sql";
  27.  
  28.             /*NOTE: Creating Path Constraints for restoring*/
  29.             String restorePath = jarDir + "\\backup\\" + s;
  30.  
  31.             /*NOTE: Used to create a cmd command*/
  32.             /*NOTE: Do not create a single large string, this will cause buffer locking, use string array*/
  33. //            String[] executeCmd = new String[]{"mysql", dbName, "-u" + dbUser, "-p" + dbPass, "-e",
  34. //                              " source " + restorePath};
  35. //            String executeCmd = "mysqldump --user=" + dbUser + " --password=" + dbPass + " "
  36. //                    + dbName + " < " + "D:\backup" + "\backuppro.sql";
  37.  
  38.             String executeCmd = "mysql -u " + dbUser + " -p" + dbPass + "-s"+ "<" +dirPath;
  39.             /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  40.            
  41.             Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  42.             int processComplete = runtimeProcess.waitFor();
  43.  
  44.             /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  45.            
  46.             if (processComplete == 0) {
  47.                 JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);
  48.             } else {
  49.                 JOptionPane.showMessageDialog(null, "Error at restoring");
  50.             }
  51.  
  52.  
  53.         } catch (URISyntaxException | IOException | InterruptedException | HeadlessException ex) {
  54.             JOptionPane.showMessageDialog(null, "Error at restoreDbFromSQL! " + ex.getMessage());
  55.         }
  56.  
  57.     }
  58.  
  59.     /*public boolean restoreDatabase(String dbUserName, String dbName, String dbPassword, String source) {
  60.  
  61.         String[] executeCmd = new String[] { "mysql -u " + dbUserName + "-p" + dbName + "<" + source, dbPassword };
  62.  
  63.         Process runtimeProcess;
  64.         try {
  65.             runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  66.             int processComplete = runtimeProcess.waitFor();
  67.  
  68.             if (processComplete == 0) {
  69.                 System.out.println("Backup restored successfully with " + source);
  70.                 return true;
  71.             } else {
  72.                 System.out.println("Could not restore the backup " + source);
  73.             }
  74.         } catch (Exception ex) {
  75.             ex.printStackTrace();
  76.         }
  77.         return false;
  78.     }*/
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement