0

I am going to take database backup by using java code.This code is excuting fine but I am getting int processComplete = runtimeProcess.waitFor(); This method calling is returning the integer as 1. So finally I am getting the message as could not create backup as sop.

public static void main(String[] args) {

        String path = "D:/databasebackup/databasbac.sql";
        String username = "root";
        String password = "";
        String dbname = "rac";
        String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + username + " -p" + password + " --add-drop-database -B " + dbname + " -r " + path;
        Process runtimeProcess;
        try {
//            System.out.println(executeCmd);//this out put works in mysql shell
            runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });
            System.out.println(executeCmd);
//            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();
            System.out.println("processComplete"+processComplete);
            if (processComplete == 0) {
                System.out.println("Backup created successfully");

            } else {
                System.out.println("Could not create the backup");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
6
  • try to read process's error stream for possible failures Commented Feb 14, 2014 at 9:04
  • What is the output of System.out.println(executeCmd);? Did you try to execute this exact command on the command line? Commented Feb 14, 2014 at 9:05
  • This what I am getting <Path to MySQL>/bin/mysqldump -u root -p --add-drop-database -B rac -r D:/databasebackup/databasbac.sql Commented Feb 14, 2014 at 9:08
  • What is this mean This code is excuting fine is this code is able to create a backup ? or Not ? If it is may your choice of checking is wrong Commented Feb 14, 2014 at 9:08
  • Its not creating backup file but i am not getting any exception. Commented Feb 14, 2014 at 9:11

3 Answers 3

2

I was facing similar problem in database backup from MYSQL through JDBC. So I ran the below code.

String path = C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump

String command= "cmd.exe /c "
                    + "\"\""+path+"\"  "
                    + " --user="+UserInputs.getDbUserName()
                    + " --password="+UserInputs.getDbPassword()
                    + " --host="+UserInputs.getDbConnectionIP()
                    + " --protocol=tcp "
                    + " --port="+UserInputs.getDbConnectionPort()
                    + " --default-character-set=utf8 "
                    + " --single-transaction=TRUE "
                    + " --routines "
                    + " --events "
                    + "\""+UserInputs.getDbName()
                    +"\" "
                    + ">"
                    + " \""
                    + "D:\\MY DATA\\DB_Backup.sql"
                    + "\""
                    + " \"";

Runtime runtime = Runtime.getRuntime(command);

In case password is blank remove the --password line. This will create your database backup.

In case you are running this on LINUX replace

cmd.exe /c

by

/bin/sh -c

Thanks!

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

Comments

1

Try out my code for backup the database using java. It works well for me.

try {

     String filename = null;

     FileChooser.setVisible(true);

     int result = FileChooser.showSaveDialog(null);

     if (result == JFileChooser.APPROVE_OPTION) {
            filename = FileChooser.getSelectedFile().toString().concat(".sql");

            File file = new File(filename);

            if (file.exists()) {
                    Object option[] = {"Sim", "Nao"};

                    int opcao = JOptionPane.showOptionDialog(null, "aaa", "bbbb", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option, option[0]);

                    if (opcao == JOptionPane.YES_OPTION) {
                             Runtime backup = Runtime.getRuntime();
                             backup.exec("C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -v -v -v --host=localhost --user=root --port=3306 --protocol=tcp --force --allow-keywords --compress --add-drop-table --result-file=" + filename + " --databases GIVE YOUR DATABSE NAME");
                             JOptionPane.showMessageDialog(null, "Backup succesfully");
                    } else {
                         FileChooserActionPerformed(evt);
                    }
           } else {
                 Runtime backup = Runtime.getRuntime();
                 backup.exec("C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -v -v -v --host=localhost --user=root --port=3306 --protocol=tcp --force --allow-keywords --compress --add-drop-table --result-file=" + filename + " --databases GIVE YOUR DATABASE NAME");
                 JOptionPane.showMessageDialog(null, "Backup succesfully");
           }
        }
    } catch (Exception e) {
          JOptionPane.showMessageDialog(null, e, "Error.!", 2);
}

And this is the DbOperation class That i have written.

import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbOperation {
       String url = "jdbc:mysql://localhost:3306/give your database name";
       String username = "your username";
       String password = "your password";
       Connection con = null;
       PreparedStatement pst = null;
       ResultSet rs = null;

       public Connection backupDB(){
              try{
                  con=DriverManager.getConnection(url, username, password);
              }catch(SQLException e){
                  System.out.println(e.getMessage());
              }
       return con;
      }
}

Comments

0

Try this:

int processComplete = runtimeProcess.exitValue();

runtime info

Hope this helps

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.