0

我试图在我的java项目中导出和导入数据库。但是执行命令不起作用。以下是我完成的代码。

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

}

这显示了一个错误

“无法运行程序“CD”:CreateProcess 错误=2,系统找不到指定的文件”

我该如何解决这个问题?

4

4 回答 4

2

如果您正在运行命令,则需要预先"cmd"添加到 your_command_to_be_run。

使其为:

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

另外我不知道要导出的命令,但您的命令似乎是错误的。您应该直接指定目录。你cd在你的命令中使用。考虑以下示例:

C:\mydir1\mydir2\some.exe   // this is correct
cmd cd C:\mydir1\mydir2 some.exe   // this is wrong
于 2012-09-06T09:55:19.430 回答
1

首先设置系统变量,如

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

然后

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

于 2012-09-06T10:06:10.967 回答
0

你可以这样尝试:

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 = ""
于 2012-09-06T09:59:37.150 回答
0

试试这个....

字符串 executeCmd ="FilePath+" mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ ">" +FileName;

于 2012-09-06T09:59:55.527 回答