Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2database connection string

For a project im working on atm, Im trying to use h2 database as an alternative to test my DAO classes. For the normal code Im using MySQL and everything is working fine there. Now I have no problem connecting to the H2 databaes, but since im using multiple classes I made 1 class for the database properties. This includes a mysql connection String, which is working fine. The h2 connection is giving me some trouble though, since it won't connect to the database. My code looks like this:

    mysqlconnectionstring=jdbc:mysql://localhost/database?user=root&password=
    h2connectionstring=jdbc:h2:~/test,sa,password

I want to be able to connect to the H2 database using a variable like this

    Connection connection = DriverManager.getConnection(databaseConnectionString);

As soon as I use this, it says it cant connect to the database, however when I use this:

    Connection connection = DriverManager.getConnection("jdbc:h2:~/test","sa","password");

Now I have tried several things, including adding slashes to the string, but I cant seem to figure it out other than making it 3 seperate variables and use those, but I want to use 1 single variable.

like image 971
jemoeee Avatar asked Mar 13 '23 22:03

jemoeee


1 Answers

Try this one

jdbc:h2:file:~/test;USER=sa;PASSWORD=password

This should work according to H2 Docs

like image 81
pomkine Avatar answered Mar 24 '23 17:03

pomkine