0

我正在尝试使用 java 恢复一个 mysql sql 文件,但我不知道它为什么不起作用。代码如下。

/*NOTE: Getting path to the Jar file being executed*/
            CodeSource codeSource = DBmanager.class.getProtectionDomain().getCodeSource();
            File jarFile = new File(codeSource.getLocation().toURI().getPath());
            String jarDir = jarFile.getParentFile().getPath();

            /*NOTE: Creating Database Constraints*/
            String dbName = "dth";
            String dbUser = "root";
            String dbPass = "root";

            String restorePath ="\""+ jarDir + "\\backup" + "\\" + s+"\"";


            /*NOTE: Used to create a DOS command*/
            String executeCmd = "";
            executeCmd = "C:\\xampp\\mysql\\bin\\mysql -u" + dbUser + " -p" + dbPass + " --database " + dbName + " < " + restorePath;


            System.out.println(executeCmd);


            Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);
            } else {
                JOptionPane.showMessageDialog(null, "Error at restoring");
            }

代码执行,但 java swing 卡在运行时命令上。System.out.println 输出的行是这样的。

C:\xampp\mysql\bin\mysql -uroot -proot --database dth < "F:\Final Year Project\Final\build\backup\0_Harish_2013-02-17-20-05-12.sql"

如果我将其复制并粘贴到命令行中,该行将完美运行。不知道为什么 java swing 接口卡在等待状态。(相同的查询在 cmd 上需要 2 秒,在 java 上我已经等了 5 分钟)。

编辑: 我运行了 streamgobbler 仍然没有任何好处,它仍然给出 Exit Value: 1 这显然是问题 IllegalThreadStateException 我该如何解决这个问题?

编辑2:

狼吞虎咽无济于事,因为挂起仍然存在,这是狼吞虎咽的输出

OUTPUT>C:\xampp\mysql\bin\mysql  Ver 14.14 Distrib 5.5.27, for Win32 (x86)
OUTPUT>Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
OUTPUT>
OUTPUT>Oracle is a registered trademark of Oracle Corporation and/or its
OUTPUT>affiliates. Other names may be trademarks of their respective
OUTPUT>owners.
OUTPUT>
OUTPUT>Usage: C:\xampp\mysql\bin\mysql [OPTIONS] [database]
OUTPUT>  -?, --help          Display this help and exit.
OUTPUT>  -I, --help          Synonym for -?
OUTPUT>  --auto-rehash       Enable automatic rehashing. One doesn't need to use
OUTPUT>                      'rehash' to get table and field completion, but startup
OUTPUT>       and reconnecting may take a longer time. Disable with
OUTPUT>         --disable-auto-rehash.
OUTPUT>                      (Defaults to on; use --skip-auto-rehash to disable.)
OUTPUT>  -A, --no-auto-rehash 
OUTPUT>                      No automatic rehashing. One has to use 'rehash' to get
OUTPUT>                      table and field completion. This gives a quicker start of
OUTPUT>                      mysql and disables rehashing on reconnect.
OUTPUT> --auto-vertical-output 
OUTPUT>                      Automatically switch to vertical output mode if the
OUTPUT>                      result is wider than the terminal width.
OUTPUT>  -B, --batch         Don't use history file. Disable interactive behavior.
OUTPUT>         (Enables --silent.)
OUTPUT>  --character-sets-dir=name 
OUTPUT>       Directory for character set files.
OUTPUT>  --column-type-info  Display column type information.
OUTPUT>  -c, --comments      Preserve comments. Send comments to the server. The
OUTPUT>                      default is --skip-comments (discard comments), enable
OUTPUT>         

(其余使用信息省略)

4

2 回答 2

0

我能够通过将字符串命令转换为字符串数组命令来解决问题。这使得 Gobbler 的使用变得多余。

于 2013-02-17T19:37:16.123 回答
-1

这很可能是由于您没有从输出流中读取 - 当它们的缓冲区已满时它们会阻塞。请阅读
您可能想要使用Commons Exec 之类的东西。
您还有一个问题是您不想使用管道(<),这是一个 bash 构造,将无法正常工作
您需要FileInputStream在 java 中从您的文件创建一个并将其设置为InputStream您的代码中的。看看这个例子。

于 2013-02-17T15:07:51.507 回答