0

I have tried to export and import the data base in my java project.But the execute command doesnt work. Following is the code I've done.

    public boolean exportDatabase(String fromServer, String FileName, String FilePath, int ExportOpions)
    {

        try {
            Class.forName(Driver).newInstance();
        }
        catch (final InstantiationException e) {
            NLogger.writeDebugLog(e);
        }
        catch (final IllegalAccessException e) {
            NLogger.writeDebugLog(e);
        }
        catch (final java.lang.ClassNotFoundException e) {
        NLogger.writeDebugLog(e);
    }
    try {

        String dbName ="DBsample";
        String dbUser = "root";
        String dbPass ="root";


        String executeCmd ="CD "+FilePath+" mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ ">" +FileName;

        Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();
        if(processComplete == 0){
        System.out.println("Backup taken successfully");
        } 
        else {
        System.out.println("Could not take mysql backup");
        }
    return true;
    }
    catch (final Exception ex) {
        NLogger.writeErrorLog("Database Connection Failed ", ex.toString());
        NLogger.writeDebugLog(ex);
        return false;
    }

}

This shows an error as

"Cannot run program "CD": CreateProcess error=2, The system cannot find the file specified"

How can I solve this issue?

1
  • mysqldump is for exporting, am i right? Commented Apr 2, 2019 at 10:55

4 Answers 4

2

If you are running command then, you need to prepend "cmd" to your_command_to_be_run.

Make it as :

String executeCmd ="cmd CD "+FilePath+" mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ ">" +FileName;

Also I do not know command to export, but it seems your command is wrong. You should directly specify directory. You are using cd in your command. Consider following example :

C:\mydir1\mydir2\some.exe   // this is correct
cmd cd C:\mydir1\mydir2 some.exe   // this is wrong
Sign up to request clarification or add additional context in comments.

1 Comment

@Gapchoos this address? can you be more clear what you want to do?
1

first set System Variable like

C:\Program Files\MySQL\MySQL Server 5.1\bin;

then

String executeCmd = mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ ">" +FileName;

Comments

0

Try this....

String executeCmd ="FilePath+" mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ ">" +FileName;

Comments

0

you can try this way:

try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    conn = DriverManager.getConnection(DbConnectionAdress, DbName, DbSifre);
    statement = conn.createStatement();
} catch (Exception e) {
    throw new RuntimeException(e);
}

String DbConnectionAdress = " "

DbName = ""

DbSifre = ""

1 Comment

hakanpekcan: never ever have an 'catch-all' exception handler without any exception handling. Thats very bad style and can (and will) cause great confusion! I will edit in a fix for that.

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.