Advertisement
Ahmad_faisal

Restore data

Jul 7th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. public static void Restoredbfromsql() {
  2. try {
  3. /*NOTE: String s is the mysql file name including the .sql in its name*/
  4. /*NOTE: Getting path to the Jar file being executed*/
  5. /*NOTE: YourImplementingClass-> replace with the class executing the code*/
  6. CodeSource codeSource = ?Main_Screen?.class.getProtectionDomain().getCodeSource();
  7. File jarFile = new File(codeSource.getLocation().toURI().getPath());
  8. String jarDir = "E:\\";
  9.  
  10.  
  11. /*NOTE: Creating Database Constraints*/
  12. String dbName = ?"electrical"?;
  13. String dbUser = ?"root"?;
  14. String dbPass = ?"root"?;
  15.  
  16. /*NOTE: Creating Path Constraints for restoring*/
  17. JFileChooser jfc = new JFileChooser();
  18. jfc.showOpenDialog(null);
  19. File f = jfc.getSelectedFile();
  20.  
  21. // String restorePath = jarDir + "\\backup" + "\\" + s;
  22. String restorePath = jfc.getSelectedFile().getAbsolutePath();
  23.  
  24. /*NOTE: Used to create a cmd command*/
  25. /*NOTE: Do not create a single large string, this will cause buffer locking, use string array*/
  26. String[] executeCmd = new String[]{"mysql", dbName, "-u" + dbUser, "-p" + dbPass, "-e", " source " + restorePath};
  27.  
  28. /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  29. Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  30. int processComplete = runtimeProcess.waitFor();
  31.  
  32. /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  33. if (processComplete == 0) {
  34. JOptionPane.showMessageDialog(null, "تم استعادة معلوماتك بنجاح");
  35. } else {
  36. JOptionPane.showMessageDialog(null, "لم يتم استعادة المعلومات");
  37. }
  38.  
  39. } catch (URISyntaxException | IOException | InterruptedException | HeadlessException ex) {
  40. JOptionPane.showMessageDialog(null, "Error at Restoredbfromsql" + ex.getMessage());
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement