简体   繁体   English

在 java 中备份 mysql [xampp] 数据库

[英]Backup a mysql [xampp] database in java

So I am still learning programming, I am creating a simple application that can backup a database but the problem is when I click the button for backup, nothing happens, it does not even display the "can't create backup".所以我仍在学习编程,我正在创建一个可以备份数据库的简单应用程序,但问题是当我单击备份按钮时,没有任何反应,甚至没有显示“无法创建备份”。 I am using xampp, in case that is relevant.我正在使用 xampp,以防万一。 I have zero idea as to why is it is not working, and I am really curios what is the reason behind it, any help will be greatly appreciated.我不知道为什么它不起作用,我真的很好奇它背后的原因是什么,任何帮助将不胜感激。

...
String path = null;
String filename;

//choose where to backup

 private void jButtonLocationActionPerformed(java.awt.event.ActionEvent evt)   {                                         
    JFileChooser fc = new JFileChooser();
    fc.showOpenDialog(this);
    String date = new SimpleDateFormat("MM-dd-yyy").format(new Date());

    try {
        File f = fc.getSelectedFile();
        path = f.getAbsolutePath();
        path = path.replace('\\', '/');
        path = path+"_"+date+".sql";
        jTextField1.setText(path);

    } catch (Exception e) {
        e.printStackTrace();
    }
} 

//backup
private void jButtonBackUpActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Process p = null;


    try{
        Runtime runtime = Runtime.getRuntime();

        p=runtime.exec("C:/xampp/mysq/bin/mysqldump -u root --add-drop-database -B capstone -r "+path);

        int processComplete = p.waitFor();
        if (processComplete==0) {
            jLabel1.setText("Backup Created Success!");
        } else {
            jLabel1.setText("Can't create backup.");
        }
    } catch (Exception e) {

    }


} 

You use a try-catch block in the jButtonBackUpActionPerformed , but the catch statement is empty.您在jButtonBackUpActionPerformed使用了try-catch块,但 catch 语句为空。 Therefore, if an exception is raised for whatever reason, no file would be written and you would get no output.因此,无论出于何种原因引发异常,都不会写入任何文件,也不会得到任何输出。 You can try to use e.printStackTrace() like in the catch statement of the other button for debugging.您可以尝试在其他按钮的catch语句中使用e.printStackTrace()进行调试。

I found the underlying problem, thanks to stan.感谢 stan,我找到了根本问题。 It was a typo problem, instead of "mysql", I have put "mysq" thank you guys!这是一个错字问题,我输入了“mysq”而不是“mysql”,谢谢你们!

  java.io.IOException: Cannot run program "C:/xampp/mysq/bin/mysqldump.exe": CreateProcess error=2, The system cannot find the file specified

this will run any shell script on Linux server.这将在 Linux 服务器上运行任何 shell 脚本。 Test it on windows ... shoud work too在 Windows 上测试它......也应该工作

 public static int executeExternalScript(String path) throws InterruptedException, IOException {

  ProcessBuilder procBuilder = new ProcessBuilder(path); 
  procBuilder.redirectErrorStream(true);
  Process process = procBuilder.start();
  BufferedReader brStdout = new BufferedReader(new InputStreamReader(process.getInputStream()));

  String line = null;
  while((line = brStdout.readLine()) != null) {   logger.info(line);   }
      int exitVal = process.waitFor();
      brStdout.close();
      return exitVal;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM