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.
Try this one
jdbc:h2:file:~/test;USER=sa;PASSWORD=password
This should work according to H2 Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With