0

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.

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();
                }



            }
        });
1
  • please help me out of this problem Commented Apr 9, 2016 at 6:25

2 Answers 2

1

Possibly Duplicate Thread, but for now take a look into these similar android issue -

Restoring SQLite DB file

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.