从网络连接到Java DB服务器(Derby)

时间:2016-05-16 12:16:25

标签: java derby javadb

我使用以下代码启动Java DB服务器:

System.setProperty("derby.drda.startNetworkServer", "true");
    try {
        NetworkServerControl serverControl = new NetworkServerControl();
        serverControl.start(new PrintWriter(System.out, true));
    } catch (Exception e) {
        e.printStackTrace();
    }

我可以使用以下网址连接到本地PC上的数据库:

String dbURLTable = "jdbc:derby://localhost:1527/Tables;create=true;user=Gen;password=YZG";

但是当我尝试在LAN上连接它时(禁用防火墙)我使用以下URL:

String dbURLTable = "jdbc:derby://192.168.1.105:1527/Tables;create=true;user=Gen;password=YZG";

然而另一台PC无法连接到这个数据库,问题是什么? 我得到的错误是:

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server 192.168.1.106 on port 1,527 with message Connection refused: connect.
    at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at Admin.CreateTables.CreateT(CreateTables.java:188)
    at Admin.CreateTables.run(CreateTables.java:13)
    at java.lang.Thread.run(Thread.java:745)
Caused by: ERROR 08001: java.net.ConnectException : Error connecting to server 192.168.1.106 on port 1,527 with message Connection refused: connect.
    at org.apache.derby.client.net.NetAgent.<init>(Unknown Source)
    at org.apache.derby.client.net.NetConnection.newAgent_(Unknown Source)
    at org.apache.derby.client.am.ClientConnection.<init>(Unknown Source)
    at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
    at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl.newNetConnection(Unknown Source)
    ... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:271)
    at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)
    at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    ... 11 more

1 个答案:

答案 0 :(得分:3)

这种情况正在发生,因为您使用0-arg构造函数NetworkServerControl创建NetworkServerControl() 不允许远程连接

您必须使用NetworkServerControl(InetAddress address, int portNumber)才能允许远程连接

//accepts connections from other hosts on an IPv4 system
NetworkServerControl serverControl = 
  new NetworkServerControl(InetAddress.getByName("0.0.0.0"),1527);

详细信息please see this