title: Connect remotely to an H2 Database using a Java Application
body: <p>I'm having the following problem:
  When I try to createTcpServer with my external IP address (the PC's IP and not my local IP = the one we see as an output after running ipconfig in cmd.exe) the following error occurs:</p>
  
  <p>Error with Server: Exception opening port "9092" (port may be in use), cause: "java.net.BindException: Cannot assign requested address: JVM_Bind" [90061-169]</p>
  
  <p>However, the port is not in use. I've checked that using netstat -a -n .
  I have enabled my external IP and I have disabled the firewall from the router. My external IP can now be pinged.</p>
  
  <p>Please help me.</p>
  
  <p>Update: Here is my code to start the tcp server.</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>Second Update: here is the h2Connection code.</p>
  
  <p>package businessApp;</p>
  
  <p>import java.sql.*; //imports sql features</p>
  
  <p>//Class responsible for connection with H2 Database Engine
  public class h2Connection {</p>
  
  <pre><code>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 &amp; 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 &amp; 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
  }
  </code></pre>
  
  <p>}</p>
  
  <p>When I want to connect to the h2 database, I make a new h2Connection object and use it to connect. I have followed the H2 manual word by word. What more do you need?</p>