【问题标题】:how to restore a database backup file using java如何使用java恢复数据库备份文件
【发布时间】:2016-04-09 06:23:30
【问题描述】:

我正在使用 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();
                }



            }
        });

【问题讨论】:

  • 请帮我解决这个问题

标签: java database restore


【解决方案1】:

可能是重复的线程,但现在看看这些类似的 android 问题 -

Restoring SQLite DB file

is it posible backup and RESTORE a database file in android? non root devices

【讨论】:

    【解决方案2】:

    我绞尽脑汁想解决这个问题一个星期,终于弄明白了。

    问题出在这一行:

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

    上面代码中的&lt; 称为流重定向。你可以阅读它here

    不幸的是,在使用java.lang.Runtime 时执行进程时不能使用流重定向(它根本不起作用)。

    .

    因此,您必须在不使用流重定向的情况下执行此操作。一种方法是:

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

    .

    为了让您的工作更轻松,适合您的解决方案是:

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

    如果您觉得我的回答有用,请发表评论。

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 2023-04-01
      • 2012-06-14
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 2011-07-14
      相关资源
      最近更新 更多