How to Connect to a Database?
The database driver is |org.h2.Driver| , and the database URL starts
with |jdbc:h2:| . To connect to a database using JDBC, use the following
code:
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
Using JRuby and Sequel:-
Sequel connect code uses the single parameter form of get connection.
Mysql works OK with the single parameter form "jdbc:h2:~/test?user=myuser&password=mypwd"
H2 in this form just considers the database name to be "test?user=sa&password=sa"
Can h2 use the single parameter method?
Thanks
Paul Fraser
> Using JRuby and Sequel:-
> Sequel connect code uses the single parameter form of get connection.
> Mysql works OK with the single parameter form "jdbc:h2:~/test?user=myuser&password=mypwd"
> H2 in this form just considers the database name to be "test?user=sa&password=sa"
> Can h2 use the single parameter method?
Yes, see http://www.h2database.com/html/features.html#passwords:
"Passing the User Name and/or Password in the URL
Instead of passing the user name as a separate parameter as in
Connection conn = DriverManager. getConnection("jdbc:h2:~/test", "sa",
"123"); the user name (and/or password) can be supplied in the URL
itself: Connection conn = DriverManager.
getConnection("jdbc:h2:~/test;USER=sa;PASSWORD=123"); The settings in
the URL override the settings passed as a separate parameter."
Regards,
Thomas
> May I suggest that it might save this question in future if this info is
> added to the FAQ under
Actually, you are the first to ask this question. However, if more
people ask for it, I will add it.
Regards,
Thomas