0

I have a new H2 test DB all setup and have no issue getting to it locally.

I used this site to help https://www.tutorialspoint.com/h2_database/h2_database_jdbc_connection.htm

I took the code and put it up on an Amazon web server and can confirm it is indeed running and can, again, locally add data to it and access it VIA code and the H2 console. The H2 console can even be reached from my remote PC.

Now on my PC I am trying to get to the server VIA JDBC but cannot. Server is even set up for the TCP server. in the properties file

spring.datasource.url=jdbc.h2.mem.test

In the application.java file, in a try-catch, and it reports the server did start.

Server.createTcp.Server().start();

The DB has a table called testable in there with server columns and test rows. And Again locally and even in the H2 console, I can get to the data no problem.

I tried several ways to list the URL in Java code to get to the server VIA JDBC.

here is the fake URL that I can get to a website hosted there just fine as well as the H2 console. https://www.mywebsitehostedonamazon.com/h2

I have added this to my code to the connection for the URL.

       import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement;  

public class H2jdbcCreateDemo { 
   // JDBC driver name and database URL 
   static final String JDBC_DRIVER = "org.h2.Driver";   
   static final String DB_URL = "jdbc:h2:tcp://https://www.mywebsitehostedonamazon.com:8082/test";  

   
   //  Database credentials 
   static final String USER = "sa"; 
   static final String PASS = "password"; 
  
   public static void main(String[] args) { 
      Connection conn = null; 
      Statement stmt = null; 
      try { 
         // STEP 1: Register JDBC driver 
         Class.forName(JDBC_DRIVER); 
             
         //STEP 2: Open a connection 
         System.out.println("Connecting to database..."); 
         conn = DriverManager.getConnection(DB_URL,USER,PASS);  
         
         //STEP 3: Execute a query 
         System.out.println("Creating table in given database..."); 
         stmt = conn.createStatement(); 
         String sql =  "SELECT * FROM testable LIMIT 1";  
         stmt.executeUpdate(sql);
         System.out.println("Created table in given database..."); 
         
         // STEP 4: Clean-up environment 
         stmt.close(); 
         conn.close(); 
      } catch(SQLException se) { 
         //Handle errors for JDBC 
         se.printStackTrace(); 
      } catch(Exception e) { 
         //Handle errors for Class.forName 
         e.printStackTrace(); 
      } finally { 
         //finally block used to close resources 
         try{ 
            if(stmt!=null) stmt.close(); 
         } catch(SQLException se2) { 
         } // nothing we can do 
         try { 
            if(conn!=null) conn.close(); 
         } catch(SQLException se){ 
            se.printStackTrace(); 
         } //end finally try 
      } //end try 
      System.out.println("Goodbye!");
   } 
}

When I run this I receive a Zero length string error for line conn = DriverManager.getConnection(DB_URL,USER,PASS);

If I change up the URL then I get a connection time out. jdbc:h2:tcp://www.mywebsitehostedonamazon.com:8082/test

If I change up the URL then I get a connection time out. jdbc:h2:tcp://www.mywebsitehostedonamazon.com:8082/mem:test

If I change up the URL then I get an error that the Table "testable" not found. jdbc:h2:mem://www.mywebsitehostedonamazon.com:8082/mem:test

Can anyone help out with this?

reddragon72
  • 191
  • 1
  • 3
  • 16
  • jdbc:h2:tcp://www.mywebsitehostedonamazon.com:8082/test looks correct, AFAIK for AWS you need to open ports to outside otherwise it is closed check https://stackoverflow.com/a/17182531/175554 – ozkanpakdil Mar 09 '21 at 21:41
  • All ports are open and I can ping the server website VIA the same port if I switch it over. It is just odd that the H2 will not allow any connections. It just times out. – reddragon72 Mar 23 '21 at 14:05
  • did you try to use telnet ? check https://superuser.com/a/621876/126098 – ozkanpakdil Mar 24 '21 at 14:32

0 Answers0