Backup a mysql [xampp] database in java

泪湿孤枕 提交于 2020-01-05 07:05:28

问题


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. 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) {

    }


} 

回答1:


You use a try-catch block in the jButtonBackUpActionPerformed, but the catch statement is empty. 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.




回答2:


I found the underlying problem, thanks to stan. It was a typo problem, instead of "mysql", I have put "mysq" thank you guys!

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


来源:https://stackoverflow.com/questions/47699781/backup-a-mysql-xampp-database-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!