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



            }
        });
Noorullah
  • 11
  • 3

2 Answers2

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

Community
  • 1
  • 1
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.

Adwait Patil
  • 171
  • 1
  • 10