使用Java环境从CMD行还原MYSQL [英] Restore MYSQL from CMD line using Java Environment

查看:147
本文介绍了使用Java环境从CMD行还原MYSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java和Mysql作为程序,我使用脚本文件为了恢复Databsae。

I am using Java and Mysql for a Program, I am Using a Script File in order to restore a Databsae.

在Java下我执行命令:under bin:mysql -u root -proot test< c:\test.mysql

Under Java I am Executing a command:under bin: mysql -u root -proot test< c:\test.mysql

它不运行如果我在cmd行下运行它将正确执行并恢复数据库。

It is not running while If I run it under cmd line it will execute properly and restore the database.

是谁有谁知道..为什么会发生..
这是什么问题,为什么它不运行如果我在Java环境下运行。

Is anybody there who knows.. why it happens.. Whats the problem, why its not running if i run it under Java environment.

EXACT SYNTAX:
I m使用进程P = runtime.getRunTime()。exec(FilePath)

EXACT SYNTAX: I m Using Process P= runtime.getRunTime().exec(FilePath)

mysql -u root -proot test< c:\test.mysql

where FilePath Variable is having value: mysql -u root -proot test< c:\test.mysql

我使用Windiws环境。如果我在CmdLine中运行FilePath,它将给出完美的reesult。

I am Using Windiws environment. while if I run the FilePath in CmdLine, it will give the perfect reesult.

非常感谢或帮助。

推荐答案

我有同样的问题!

实际上,我唯一能做的工作正在使用批处理文件:

Actually the only thing that I could make work (on Windows, havent tested other platforms) is using batch files:

这里是代码:

public class MysqlDatabase {

private int BUFFER = 10485760;
private String host, port, user, password, db;

public MysqlDatabase(String host, String port, String user, String password, String db) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.password = password;
    this.db = db;
}

public boolean restoreDatabase(String filepath) throws Exception {
    String comando = "mysql " + db + " --host=" + host + " --port=" + port
            + " --user=" + user + " --password=" + password
            + " < " + filepath;
    File f = new File("restore.bat");
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(comando.getBytes());
    fos.close();
    Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");
    return true;

}

public String getFull() throws Exception {

    Process run = Runtime.getRuntime().exec(
            "mysqldump --host=" + host + " --port=" + port
            + " --user=" + user + " --password=" + password
            + " --opt "
            + "" + db);
    InputStream in = run.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    StringBuilder temp = new StringBuilder();

    int count;
    char[] cbuf = new char[BUFFER];

    while ((count = br.read(cbuf, 0, BUFFER)) != -1) {
        temp.append(cbuf, 0, count);
    }

    br.close();
    in.close();

    return temp.toString();
}}

这篇关于使用Java环境从CMD行还原MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆