Java Derby远程连接超时:如何配置Derby网络服务器的网络设置?

Java Derby远程连接超时:如何配置Derby网络服务器的网络设置?,java,derby,Java,Derby,我尝试构建一个支持网络/客户端数据库连接的程序。我已经完成了数据库创建和GUI设计的所有工作,用户可以与数据库联系。在本地机器上,一切都很正常(我是说DB创建并启动/停止Derby服务器的是哪台PC) 服务器配置 //I take the exact path of derby jar files (derbyrun, derbynet, derby client etc.) File file = new File(FirstTimeMainFrame.class.getProtectionD

我尝试构建一个支持网络/客户端数据库连接的程序。我已经完成了数据库创建和GUI设计的所有工作,用户可以与数据库联系。在本地机器上,一切都很正常(我是说DB创建并启动/停止Derby服务器的是哪台PC)

服务器配置

//I take the exact path of derby jar files (derbyrun, derbynet, derby client etc.)
File file = new File(FirstTimeMainFrame.class.getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .getPath().replace(new File(FirstTimeMainFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName(), "").replace("%20", " "));
String path = file+"\\DB";

//Execute CMD Command for derbyrun.jar set the DERBY INSTALL Classpath
ProcessBuilder builder = new ProcessBuilder();
Process process = null;
String[] commandStart = new String[3];
String[] commandStop = new String[3];

commandStart[0] = "cmd.exe";
commandStart[1] = "/c";
commandStart[2] = "cd "+path+" && java -jar derbyrun.jar server start";

builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Create the DB with using Derby Client Driver
String driver = "org.apache.derby.jdbc.ClientDriver";
String connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=true";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(connectionURL);

//Connect to DB and Setting the DB Properties
connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=false";
conn = DriverManager.getConnection(connectionURL);
//Turning on authentication, Create sample users, Create sample tables etc...

//Shutdown the Derby Server
commandStop[0] = "cmd.exe";
commandStop[1] = "/c";
commandStop[2] = "cd "+path+" && java -jar derbyrun.jar server shutdown";

builder = new ProcessBuilder(commandStop[0], commandStop[1], commandStop[2]);
builder.redirectErrorStream(true);
process = builder.start();
//Start the Server First
builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Load the driver and connect to Local DB
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://localhost:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://10.90.232.2:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);
工作良好的局部连接

//I take the exact path of derby jar files (derbyrun, derbynet, derby client etc.)
File file = new File(FirstTimeMainFrame.class.getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .getPath().replace(new File(FirstTimeMainFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName(), "").replace("%20", " "));
String path = file+"\\DB";

//Execute CMD Command for derbyrun.jar set the DERBY INSTALL Classpath
ProcessBuilder builder = new ProcessBuilder();
Process process = null;
String[] commandStart = new String[3];
String[] commandStop = new String[3];

commandStart[0] = "cmd.exe";
commandStart[1] = "/c";
commandStart[2] = "cd "+path+" && java -jar derbyrun.jar server start";

builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Create the DB with using Derby Client Driver
String driver = "org.apache.derby.jdbc.ClientDriver";
String connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=true";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(connectionURL);

//Connect to DB and Setting the DB Properties
connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=false";
conn = DriverManager.getConnection(connectionURL);
//Turning on authentication, Create sample users, Create sample tables etc...

//Shutdown the Derby Server
commandStop[0] = "cmd.exe";
commandStop[1] = "/c";
commandStop[2] = "cd "+path+" && java -jar derbyrun.jar server shutdown";

builder = new ProcessBuilder(commandStop[0], commandStop[1], commandStop[2]);
builder.redirectErrorStream(true);
process = builder.start();
//Start the Server First
builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Load the driver and connect to Local DB
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://localhost:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://10.90.232.2:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);
问题远程连接

//I take the exact path of derby jar files (derbyrun, derbynet, derby client etc.)
File file = new File(FirstTimeMainFrame.class.getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .getPath().replace(new File(FirstTimeMainFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName(), "").replace("%20", " "));
String path = file+"\\DB";

//Execute CMD Command for derbyrun.jar set the DERBY INSTALL Classpath
ProcessBuilder builder = new ProcessBuilder();
Process process = null;
String[] commandStart = new String[3];
String[] commandStop = new String[3];

commandStart[0] = "cmd.exe";
commandStart[1] = "/c";
commandStart[2] = "cd "+path+" && java -jar derbyrun.jar server start";

builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Create the DB with using Derby Client Driver
String driver = "org.apache.derby.jdbc.ClientDriver";
String connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=true";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(connectionURL);

//Connect to DB and Setting the DB Properties
connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=false";
conn = DriverManager.getConnection(connectionURL);
//Turning on authentication, Create sample users, Create sample tables etc...

//Shutdown the Derby Server
commandStop[0] = "cmd.exe";
commandStop[1] = "/c";
commandStop[2] = "cd "+path+" && java -jar derbyrun.jar server shutdown";

builder = new ProcessBuilder(commandStop[0], commandStop[1], commandStop[2]);
builder.redirectErrorStream(true);
process = builder.start();
//Start the Server First
builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Load the driver and connect to Local DB
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://localhost:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://10.90.232.2:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);
--PC1(服务器1)创建并存储数据库文件。当程序启动时,它还可以启动和停止Derby网络服务器。当PC1运行程序时,我在derby.log中看到下面几行,并了解服务器已成功启动并运行

Sat Dec 26 04:06:49 EET 2015 : Apache Derby Network Server - 10.12.1.1 - (1704137) started and ready to accept connections on port 1527
Sat Dec 26 04:06:49 EET 2015: Booting Derby version The Apache Software Foundation - Apache Derby - 10.12.1.1 - (1704137): instance a816c00e-0151-dc09-cf3e-ffffaab85dad on database directory C:\Users\Server_PC\Desktop\Trying Project\DB\myDB with class loader sun.misc.Launcher$AppClassLoader@1909752 Loaded from file:/C:/Users/Server_PC/Desktop/Trying%20Project/DB/derby.jar
java.vendor=Oracle Corporation
java.runtime.version=1.8.0_66-b18
user.dir=C:\Users\Server_PC\Desktop\Trying Project\DB
os.name=Windows 7
os.arch=x86
os.version=6.1
derby.system.home=C:\Users\Server_PC\Desktop\Trying Project\DB
Database Class Loader started - derby.database.classpath=''
--PC2(客户端1)尝试使用其LAN ip连接到远程服务器

我已经附加了一些日志写入程序,当我在客户机上运行程序时,我看到下面的异常

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server 10.90.232.2 on port 1.527 with message Connection timed out: connect.

我真的被困在这一点上了。我遗漏了什么吗?

可能是Windows防火墙,它通常在默认情况下处于打开状态,并且会阻止从另一台计算机到Derby网络服务器的入站连接,除非您明确配置Windows防火墙以允许此类连接

原始海报阐明:

I forgot to say, sorry: I have already added Firewall Port rule 
for 1527 to Inbound and Outbound in PC1. Interesting thing is
when I check netroutes with netstat -an | find "1527" in PC1 I
see 127.0.0.1 1527 is in LISTENING state.

It should be 10.90.232.2 1527 ?

您可以使用-h和-p参数影响Derby网络服务器的侦听地址和端口,以符合这些防火墙要求 你想说的规则:

-h 10.90.232.2 -p 1527
原始海报确认,以这种方式指定网络配置允许防火墙接受连接:

Exactly right. I found solution about 6-7 hours ago. 
For server Start should be 

commandStart[2] = "cd "+path+" && java -jar derbyrun.jar server start -h 10.90.232.2"; 

and shutdown 

commandStop[2] = "cd "+path+" && java -jar derbyrun.jar server shutdown -h 10.90.232.2"; 

then server and client side connect with 

String connectionUrl = "jdbc:derby://10.90.232.2:1527/myDB"+
    ";create=false;" + "user=" +"\""+ unameTextField.getText() +
    "\""+ ";" + "password=" +"\""+ 
    new String (passwordPasswordField.getPassword()) +"\""+ ";";

它可能是Windows防火墙,默认情况下通常是打开的,它将阻止从另一台计算机到Derby网络服务器的入站连接,除非您明确配置Windows防火墙以允许此类连接。我忘了说抱歉。我已经在PC1中将1527的防火墙端口规则添加到入站和出站。有趣的是,当我使用netstat检查netroutes时,发现PC1 isee 127.0.0.1 1527中的“1527”处于侦听状态。Is应该是10.90.232.2 1527?您可以使用-h和-p参数影响Derby网络服务器的侦听地址和端口。完全正确。我在6-7小时前找到了解决方案。对于服务器启动,应该是
commandStart[2]=“cd”+path+”&&java-jar-derbyrun.jar服务器启动-h10.90.232.2”
shutdown commandStop[2]=“cd”+path+“&&java-jar-derbyrun.jar服务器shutdown-h10.90.232.2”而不是服务器端和客户端通过
字符串连接url=“jdbc:derby://10.90.232.2:1527/myDB“+”create=false;“+”user=“+”\”“+unatextfield.getText()+“\”“+”;“+”password=“+”\”“+新字符串(passwordPasswordField.getPassword())+“\”“+”;”很高兴听到它正在工作。我将编辑问题标题,明确您的问题涉及如何配置Derby网络服务器以遵守防火墙限制,并将您的工作解决方案作为答案发布。