从网络连接到 Java 数据库服务器 (Derby)

Connect to Java DB server (Derby) from network

我已使用以下代码启动 Java 数据库服务器:

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

我可以使用 URL:

连接到本地 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

发生这种情况是因为您正在使用 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