我遇到以下问题: 当我尝试使用我的外部IP地址创建TCP服务器(PC的IP而不是我的本地IP =我们在cmd.exe中运行ipconfig后看到的输出)时发生以下错误:
服务器出错:异常打开端口“9092”(端口可能正在使用中),原因:“java.net.BindException:无法分配请求的地址:JVM_Bind”[90061-169]
但是,端口未使用。我已经使用netstat -a -n检查过了。 我启用了外部IP,并且已从路由器禁用了防火墙。我的外部IP现在可以被ping。
请帮帮我。
更新:这是启动tcp服务器的代码。
package businessApp;
import org.h2.tools.Server; //imports the server utility
public class startTcpServerForH2 {
Server server; //the server's instance variable
private static final String SERVER_IP = "192.168.1.101"; //fixed IP of the server
private static final String SERVER_PORT = "9092"; //fixed port the server is listening to
public void tcpServer() { //method responsible to create the tcp server
optionPane optPane = new optionPane(); //option pane for debugging purposes, shows the server's status
try { //catches any server related errors, if the connection is broken etc.
//server uses the IP and port defined earlier, allows other computers in the LAN to connect and implements the secure socket layer (SSL) feature
server = Server.createTcpServer( //create tcp server
new String[] { "-tcpPort" , SERVER_PORT , "-tcpAllowOthers" , "-tcpSSL" }).start();
System.out.println(server.getStatus()); //prints out the server's status
optPane.checkServerStatus(server.getStatus()); //prints out the server's status on the option pane as well
} catch(Exception ex){
System.out.println("Error with Server: " + ex.getMessage());
}
}
public static void main(String[] args){
startTcpServerForH2 tcpServ = new startTcpServerForH2(); //create a new server object
tcpServ.tcpServer(); //starts the tcp server
}
}
第二次更新:这是h2Connection代码。
package businessApp;
import java.sql。*; //导入sql功能
//负责与H2数据库引擎连接的类 公共类h2Connection {
Connection conn; //connection variable
DatabaseMetaData dbmd; /** Metadata variable which include methods such as the following:
* 1) Database Product Name
* 2) Database Product Version
* 3) URL where the database files are located (in TCP mode)
*/
Statement stm; //statements variable
ResultSet rst; //result sets variable
private static final String SERVER_IP = "..."; //here I enter my WAN_IP
private static final String SERVER_PORT = "9092";
public Connection connectionToH2(Connection connt) {
optionPane optPane = new optionPane(); //create new option pane object
String outputConn = null; //declare & initialize string which will hold important messages
try {
Class.forName("org.h2.Driver"); //Driver's name
/** The String URL is pertained of the following:
* 1) jdbc which java implements so that it can take advantage of the SQL features
* 2) Which Database Engine will be used
* 3) URL where the files will be stored (as this is a TCP connection)
* 4) Schema: businessApp
* 5) Auto server is true means that other computers can connect with the same databse at any time
* 6) Port number of the server is also defined
*/
String url = "jdbc:h2:tcp://" + SERVER_IP + ":" + SERVER_PORT + "/C:/Databases/businessApp;IFEXISTS=TRUE";
System.out.println(url); //prints out the url the database files are located as well as the h2 features used (SSL)
connt = DriverManager.getConnection(url, "sa", ""); //Driver Manager defines the username & password of the database
System.out.println(connt.getCatalog()); //prints out the database schema
optPane.checkServerStatus(connt.getCatalog()); //prints out the database schema on the option pane as well
connt.setAutoCommit(false); //set AutoCommit to false to control commit actions manually
//outputs H2 version and the URL of the database files which H2 is reading from, for confirmation
dbmd = connt.getMetaData(); //get MetaData to confirm connection
outputConn = "Connection to "+dbmd.getDatabaseProductName()+" "+
dbmd.getDatabaseProductVersion()+ " with the URL " + dbmd.getURL()+" was successful.\n";
System.out.println(outputConn); //outputs the message on the system (NetBeans compiler)
optPane.checkH2Connection(outputConn); //outputs the message on top of the frame
} catch (ClassNotFoundException ex){ //In case there is an error for creating the class for the Driver to be used
System.out.println("Error creating class: " + ex.getMessage());
} catch(SQLException ex){ //Any error associated with the Database Engine
System.out.println("SQL error: " + ex.getMessage());
optPane.checkServerStatus("SQL error: " + ex.getMessage());
}
return connt; //As the method is not void, a connection variable must be returned
}
}
当我想连接到h2数据库时,我创建了一个新的h2Connection对象并用它来连接。我一字一句地跟着H2手册。你还需要什么?
答案 0 :(得分:7)
如下面显示的命令行帮助中所述,Protection against Remote Access建议如下:
默认情况下,此数据库在启动H2控制台,TCP服务器或PG服务器时不允许来自其他计算机的连接。可以使用命令行选项
-webAllowOthers
,-tcpAllowOthers
,-pgAllowOthers
启用远程访问。
请参阅有关这些选项的重要caveats文档。
附录:对我来说,只要我在打开防火墙后重新启动Server
;你根本不需要setProperty()
行; LAN IP
转发端口9092应为您的主机IP地址的WAN_IP
;然后你可以通过WAN_IP
:
java -cp h2.jar org.h2.tools.Shell -url
jdbc:h2:tcp://WAN_IP/~/path/to/test;ifexists=true"
命令行帮助:
$ java -cp .:/opt/h2/bin/h2.jar org.h2.tools.Shell -? Interactive command line tool to access a database using JDBC. Usage: java org.h2.tools.Shell Options are case sensitive. Supported options are: [-help] or [-?] Print the list of options [-url ""] The database URL (jdbc:h2:...) [-user ] The user name [-password ] The password [-driver ] The JDBC driver class to use (not required in most cases) [-sql ""] Execute the SQL statements and exit [-properties ""] Load the server properties from this directory If special characters don't work as expected, you may need to use -Dfile.encoding=UTF-8 (Mac OS X) or CP850 (Windows). See also http://h2database.com/javadoc/org/h2/tools/Shell.html $ java -cp /opt/h2/bin/h2.jar org.h2.tools.Server -? Starts the H2 Console (web-) server, TCP, and PG server. Usage: java org.h2.tools.Server When running without options, -tcp, -web, -browser and -pg are started. Options are case sensitive. Supported options are: [-help] or [-?] Print the list of options [-web] Start the web server with the H2 Console [-webAllowOthers] Allow other computers to connect - see below [-webDaemon] Use a daemon thread [-webPort ] The port (default: 8082) [-webSSL] Use encrypted (HTTPS) connections [-browser] Start a browser connecting to the web server [-tcp] Start the TCP server [-tcpAllowOthers] Allow other computers to connect - see below [-tcpDaemon] Use a daemon thread [-tcpPort ] The port (default: 9092) [-tcpSSL] Use encrypted (SSL) connections [-tcpPassword ] The password for shutting down a TCP server [-tcpShutdown ""] Stop the TCP server; example: tcp://localhost [-tcpShutdownForce] Do not wait until all connections are closed [-pg] Start the PG server [-pgAllowOthers] Allow other computers to connect - see below [-pgDaemon] Use a daemon thread [-pgPort ] The port (default: 5435) [-properties ""] Server properties (default: ~, disable: null) [-baseDir ] The base directory for H2 databases (all servers) [-ifExists] Only existing databases may be opened (all servers) [-trace] Print additional trace information (all servers) The options -xAllowOthers are potentially risky. For details, see Advanced Topics / Protection against Remote Access. See also http://h2database.com/javadoc/org/h2/tools/Server.html