简体   繁体   English

如何使用Java还原数据库备份文件

[英]how to restore a database backup file using java

i am backup-ed a database in java using mysqldump command and now i want to restore this file back here is my code but it creates a mysqlid and do not responding anything and also it doesn't restore the file back. 我使用mysqldump命令备份了java中的数据库,现在我想恢复此文件,这是我的代码,但是它创建了mysqlid,不响应任何内容,也没有恢复文件。

restore.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
//                  JFileChooser  fc  =  new  JFileChooser();
//                  
//                  fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
//                  int result = fc.showOpenDialog(frame);
//                  if (result == JFileChooser.APPROVE_OPTION) {
//                      File file = fc.getSelectedFile();
//                      pathTobeSaved = file.getAbsolutePath();

                    //}
//                  JOptionPane.showMessageDialog(null, "Starting");
//                  String cmd = "mysql -u root -h localhost mysqlsarafi < C:\\xampp\\htdocs\\backup.sql";
//                  JOptionPane.showMessageDialog(null, "Waiting");
                    Process runtime = Runtime.getRuntime().exec("mysql -u root mysqlsarafi < C:\\xampp\\htdocs\\backup.sql");
                    JOptionPane.showMessageDialog(null, "Done");
                    int complete = runtime.waitFor();
                    JOptionPane.showMessageDialog(null, complete);
                    if(complete ==0){
                        JOptionPane.showMessageDialog(null, "Succed");
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "not succed");
                    }

                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }



            }
        });

Possibly Duplicate Thread, but for now take a look into these similar android issue - 可能是重复线程,但现在看看这些类似的android问题-

Restoring SQLite DB file 恢复SQLite数据库文件

is it posible backup and RESTORE a database file in android? 它可以备份并在android中还原数据库文件吗? non root devices 非根设备

I have grilled my head trying to solve this problem for a week and I have finally figured it out. 我为解决这个问题花了一个星期的时间,终于找到了答案。

The problem is at this line: 问题在这一行:

Process runtime =
Runtime.getRuntime().exec("mysql -u root mysqlsarafi < C:\\xampp\\htdocs\\backup.sql");

The < in above code is called as stream redirection. 上面的代码中的<称为流重定向。 You can read about it here . 你可以在这里阅读。

Unfortunately, you cannot use stream redirection when executing a process when using java.lang.Runtime (it simply does not work). 不幸的是,在使用java.lang.Runtime情况下执行进程时不能使用流重定向(它根本不起作用)。

.

So, you have to do it without using stream redirection. 因此,您必须在不使用流重定向的情况下执行此操作。 One way of doing it is: 一种方法是:

mysql -u root --execute "SOURCE C:\\path\\to\\backup.sql"

.

To make your work easier, the solution for you is: 为了使您的工作更轻松,适合您的解决方案是:

String cmd = "mysql -u root mysqlsarafi --execute \"SOURCE C:\\xampp\\htdocs\\backup.sql\"";
Process runtime = Runtime.getRuntime.exec(cmd);

Post a comment if you found my answer useful. 如果您发现我的答案有用,请发表评论。

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

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