Gapchoos
Gapchoos

Reputation: 1422

SQL import using Java

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?

Upvotes: 0

Views: 1269

Answers (4)

hakanpekcan
hakanpekcan

Reputation: 1

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 = ""

Upvotes: 0

Dipul Patel
Dipul Patel

Reputation: 187

first set System Variable like

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

then

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

Upvotes: 1

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

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

Upvotes: 2

Ketan Bhavsar
Ketan Bhavsar

Reputation: 5396

Try this....

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

Upvotes: 0

Related Questions