简体   繁体   English

将Derby db导出为sql文件

[英]Export Derby db as a sql file

I have a derby db in "D:/Dev/workspaces/workspacePro/School-FinMa/school-finma". 我在“D:/ Dev / workspaces / workspacePro / School-FinMa / school-finma”中有一个derby db。 I want to export it to a this folder "E:/". 我想将其导出到此文件夹“E:/”。 So I run this: 所以我运行这个:

Runtime.getRuntime().exec("java org.apache.derby.tools.dblook -d 'jdbc:derby:D:/Dev/workspaces/workspacePro/School-FinMa/school-finma' -o 'E:/myDB_DDL.sql'");

But nothing happens, it's like if this code does not exists. 但没有任何反应,就像这段代码不存在一样。

I want a way to export the db to single .sql file, that contains all db generation code + data. 我想要一种方法将db导出到单个.sql文件,该文件包含所有数据库生成代码+数据。 How Can I do that from inside Java ?? 我怎么能从Java里面做到这一点?

The project (in eclipse) have derbytools.jar imported. 该项目(在eclipse中)已导入derbytools.jar。

When you start a new sub process and it doesn't work, you should output the output and error streams. 当您启动新的子流程并且它不起作用时,您应该输出输出和错误流。 You said there is no output, but I think you didn't get the output stream of the process. 你说没有输出,但我认为你没有得到过程的输出流。

When running a Java sub process you should add the classpath as you would in the command line. 运行Java子进程时,应该像在命令行中一样添加类路径。 In this case you should add derby.jar and derbytools.jar . 在这种情况下,您应该添加derby.jarderbytools.jar For a network connection you need derbyclient.jar , too. 对于网络连接,您也需要derbyclient.jar

You are using Windows. 您正在使用Windows。 Then don't use single quotation marks. 然后不要使用单引号。

Here is a simple running example, which prints the output and error streams to the console. 这是一个简单的运行示例,它将输出和错误流打印到控制台。 Without an error there is no output. 没有错误就没有输出。 But try it with single quotation marks you will see an error message. 但尝试使用单引号,您将看到一条错误消息。

In this example the Derby JARs are located at: e:\\derby\\lib . 在此示例中,Derby JAR位于: e:\\derby\\lib

public static void copyStream(InputStream in, OutputStream out) throws IOException {
  final byte[] buffer = new byte[1024];
  int bytesRead = 0;
  while((bytesRead = in.read(buffer)) != -1) {
    out.write(buffer, 0, bytesRead);
  }
}

public static void main(String[] args) throws IOException, InterruptedException {
  Process process = Runtime.getRuntime().exec( 
    "java -cp e:\\derby\\lib\\derby.jar;e:\\derby\\lib\\derbyclient.jar;e:\\derby\\lib\\derbytools.jar org.apache.derby.tools.dblook -d jdbc:derby:D:/Dev/workspaces/workspacePro/School-FinMa/school-finma -o E:/myDB_DDL.sql");
  final int exitValue = process.waitFor();
  System.out.println("Exit value: " + exitValue);

  System.out.println("Outputstream: ");
  copyStream(process.getInputStream(), System.out);

  System.out.println("Errorstream: ");
  copyStream(process.getErrorStream(), System.out);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM