0

Java プログラムを使用して、バックアップした .sql ファイルを復元しようとしています。以下にその方法を掲載します。しかし、これを実行すると、プログラムが長時間停止します。次に、コマンドライン(Windows)で同じmysqlコマンドを実行しましたが、魅力的に機能します。

見逃したところに戸惑いました。どう思いますか ?

            File file;
         final JFileChooser fc = new JFileChooser();
         int returnVal = fc.showOpenDialog(this);

   if (returnVal == JFileChooser.APPROVE_OPTION) {
                 file = fc.getSelectedFile();

         try {
             System.out.println(file.getCanonicalPath());

        String executeCmd = "mysql -u " + username + " -p" + password +" " + dbName+" < "+" \" "+file.getCanonicalPath()+"\" " ;
         Process runtimeProcess;
           runtimeProcess = Runtime.getRuntime().exec(executeCmd);
         int processComplete = runtimeProcess.waitFor();
         if (processComplete == 0) {

            JOptionPane.showMessageDialog(Interface.mainFrame, "Database Backup restored successfully.", "Netmetering", 1);     
        } else {
            System.out.println("Could not restore the backup");
        }
    } catch (IOException | InterruptedException ex) {}

...

4

2 に答える 2

1

一般に、外部プログラムを実行する正しい方法は次のとおりです。

  • 外部プログラムのコマンドラインを構築する
  • ProcessBuilderProcessをビルドする
  • 独自の StreamRender を構築する
  • 外部プログラムを実行する
  • 外部プログラムの STDOUT と STDERR を確認してください
  • 外部プログラムの終了ステータスを確認します

以下に説明するように、このシーケンスを実現できます。

String command = "mysql -u... -p... dbname < file.sql";
ProcessBuilder pb = new ProcessBuilder(command);
Process pr;

try {
  pr = pb.start();
  StreamRender outputStdout = new StreamRender(pr.getInputStream(), StreamRender.STDOUT);
  // your STDOUT output here
  outputStdout.start();

  StreamRender outputStderr = new StreamRender(pr.getErrorStream(), StreamRender.STDERR);
  // your STDERR outpu here
  outputStderr.start();

  pr.waitFor();
  if (pr.exitValue() != 0) {
    // your external program fails
  } else {
    // ok, your external program was executed correctly
  }

} catch (Exception e) {
  // ...
}  finally {
  // ...
}

StreamRender.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class StreamRender extends Thread {

public static final String STDOUT = "STDOUT";
public static final String STDERR = "STDERR";

private InputStream inputStream;
private String inputType;

public StreamRender(InputStream inputStream, String inputType) {
  this.inputStream = inputStream;
  this.inputType = inputType;
}

public void run() {
  try {
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader br = new BufferedReader(inputStreamReader);
    String line = null;
    while ((line = br.readLine()) != null) {
      System.out.println(this.inputType + " > " + line);
    }
  } catch (IOException ioe) {
    ioe.printStackTrace();
  }
}
于 2013-09-11T05:49:05.380 に答える
1
String executeCmd = "mysql -u " + username + " -p" + password +" " + dbName+" < "+" \" "+file.getCanonicalPath()+"\" " ;
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
InputStream is = runtimeProcess.getInputStream();

// Do one OR the other, but not both ;)

// If you don't care about the output, but I think it's a bit of waste personally...
while (is.read() != -1) {}

// I'd at least dump the output to the console...
int byteRead = -1;
while ((byteRead = is.read()) != -1) {
    System.out.print((char)byteRead );
}

int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {...}

ProcessBuilderまた、このように手動で作成するよりも使用することをお勧めしProcessます。パラメーターをより適切に処理します-IMHO

于 2013-09-11T05:49:50.497 に答える