Search code examples
javajdbcconnection-stringh2

What is the jdbc connection string for h2 database?


I'm trying to connect to an h2 database on my local machine to create a sql DataSource object. I'm running windows and i'm having some issues defining the path to the data file in my projects app.properties file.

Say the path to the local directory data file is:

D:\projects\myproject\data\project

How would one go about defining a connection url for this?

I've tried the many things including the following:

project.db.url = jdbc:h2:tcp://localhost\\\\D:\\projects\\myproject\\data\\project

Then I thought maybe it's the JDBC URL that's the issue, so I tried:

project.db.url = jdbc:h2:tcp:\\\\localhost\\\\D:\\projects\\myproject\\data\\project

Solution

  • As per documentation, default JDBC connection string is

    jdbc:h2:~/test  
    

    And, for TCP connection

    jdbc:h2:tcp://localhost/~/test  
    

    ==Update==

    But, if you wanted to create/read h2 database to/from specific folder, then it should be

     jdbc:h2:tcp://localhost/<path_to_database>
    

    That means,

    jdbc:h2:tcp://localhost/D:/myproject/data/project-name
    

    Thanks @Sam for sharing info.