我继承了一些使用德比数据库的源代码,但启动服务器已不再适用了。
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
这些代码行是否有任何明显的缺失或错误?
答案 0 :(得分:2)
检查服务器是否已启动。您需要显式启动服务器。或通过
设置系统属性derby.drda.startNetworkServer=true
。
答案 1 :(得分: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.尝试......