[英]Start Java DB server without NetBeans
I am creating a Java APP that manages a database. 我正在创建一个管理数据库的Java APP。 I've been starting the JAVA DB server manually by right clicking - start server.
我一直通过右键单击-启动服务器来手动启动JAVA DB服务器。 with NetBeans but since I am not going to be the one that runs the application that can't be done anymore.
使用NetBeans,但由于我将不再是无法运行该应用程序的人。 I need a way to start it without NetBeans.
我需要一种无需NetBeans即可启动的方法。 Embedded or server mode, I don't really mind.
嵌入式或服务器模式,我并不介意。
I've searched a lot but nothing seems to work. 我进行了很多搜索,但似乎没有任何效果。 I tried this: Java DB - Starting Server within application
我试过了: Java DB-在应用程序内启动服务器
NetworkServerControl server = new NetworkServerControl(InetAddress.getLocalHost(),1527);
server.start(null)
I got java.net.ConnectException: Error al conectarse al servidor localhost en el puerto 1527 con el mensaje Connection refused: connect. 我得到了java.net.ConnectException:错误连接器服务端本地主机localhost en puerto 1527 con el mensaje连接被拒绝:连接。
I tried also starting it with the command line 我也尝试通过命令行启动它
String[] command =
{
"cmd",
};
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
try (PrintWriter stdin = new PrintWriter(p.getOutputStream())) {
stdin.println("cd C:\\Program Files\\Java\\jdk1.8.0_31\\db\\lib");
stdin.println("java -jar derbyrun.jar server start");
}
int returnCode = p.waitFor();
Also got connection refused, (database Citas not found) so the only way it could work is this: 也被拒绝连接(未找到数据库Citas),因此它唯一可行的方法是:
String host = "jdbc:derby://localhost:1527/Citas";
con = DriverManager.getConnection(host, username, password);
But it works only if I start the server by clicking in Java DB -> start. 但是仅当我通过单击Java DB-> start启动服务器时,它才有效。 Any help would be higly appreciated
任何帮助将不胜感激
Can you try starting it using Runtime
? 您可以尝试使用
Runtime
启动它吗?
Runtime runtime = Runtime.getRuntime();
runtime.exec(new String[]{ "java", "-jar", "/path/to/derbyrun.jar", "server", "start");
If you were to do this in its own thread, you could also append .waitFor()
to the .exec
command which will hang until the process finishes. 如果要在自己的线程中执行此操作,则还可以将
.waitFor()
附加到.exec
命令,该命令将挂起,直到进程完成。 This would be good to determine if the db closed unexpectedly. 这将是确定数据库是否意外关闭的好方法。
Also using runtime, you can get read the stdout / stderr of the process, and kill it if need be. 同样使用运行时,您可以读取进程的stdout / stderr,并在需要时将其杀死。 Possibilities are endless.
可能性是无止境的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.