我继承了一些使用derby数据库的源代码,但启动服务器不再工作。
public void startDatabase(){
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
System.setProperty("derby.system.home", "D:\runtime-my.product\log");
NetworkServerControl nsc = new NetworkServerControl(InetAddress.getByName("localhost"), 1527)
nsc.start(null);
nsc.ping();
catch (Exception e){
e.printStackTrace();
}
}
执行nsc.ping()
时,会引发以下异常:
Exception: java.lang.Exception: DRDA_NoIO.S:Could not connect to Derby Network Server on host 127.0.0.1, port 1527: Connection refused: connect
这些代码行有什么明显的缺失或错误吗?
检查服务器是否已启动。您需要显式启动服务器。或通过设置系统属性CCD_ 2。
在套接字准备好进行连接之前,NetworkServerControl构造函数需要一段时间才能返回。在我的机器上大约50毫秒。我这样更改了你的示例:
NetworkServerControl nsc = new NetworkServerControl(InetAddress.getByName("localhost"), 1527);
nsc.start(null);
for (int i = 0; i < 10; ++i) {
try {
System.out.println("Attempting to ping...");
nsc.ping();
break;
} catch (Exception e) {
System.out.println(e.getMessage());
}
Thread.sleep(10);
}
它在5号成功了。企图