JDBC Connection String

737 views
Skip to first unread message

Paul Fraser

unread,
Feb 28, 2009, 7:13:23 AM2/28/09
to h2-da...@googlegroups.com

From FAQ and tutorial


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

Thomas Mueller

unread,
Mar 3, 2009, 1:15:18 AM3/3/09
to h2-da...@googlegroups.com
Hi,

> 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

Paul Fraser

unread,
Mar 3, 2009, 1:34:36 AM3/3/09
to h2-da...@googlegroups.com
Thanks, Thomas,

May I suggest that it might save this question in future if this info is
added to the FAQ under


How to Connect to a Database?

Paul Fraser

Thomas Mueller

unread,
Mar 3, 2009, 3:43:20 PM3/3/09
to h2-da...@googlegroups.com
Hi,

> 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

Reply all
Reply to author
Forward
0 new messages