Newbie here...

244 views
Skip to first unread message

Thotheolh

unread,
Mar 16, 2008, 4:20:32 AM3/16/08
to H2 Database
Hi this is the first time I would be using H2. I have lots of
questions to ask because I have never used Derby or Java DB or H2 or
HSQL. I have some knowledge of Oracle and MySQL.

May I know how should I create a database and set the password and
username for it. For example, my application would need to use a
database called 'accounts' and the password to access of 'password'
and username of 'username', how should I set it ?

I would also like to use it in the client-server mode with SSL and
encryption of database files.

May I know where would the database files be generated after I created
a database and created the tables and populated it with data ?

Rich

unread,
Mar 16, 2008, 10:05:44 PM3/16/08
to H2 Database
Hi -

Most of this is in the documentation... but here is an easy example.

Download the latest, open a command window. Set the
CLASSPATH=h2.jar;.

Then type:

java org.h2.tools.Server -tcpSSL true -baseDir /tmp/database

That will start a server with an SSL connection default. All the
files will be stored in /tmp/database ( any windows variation of C:
\temp\database is of course also valid ). This should also result in
the system automatically opening the web client with the locally
configured default browser. If you take the configuration settings
from teh code below, and connect via the browser client, all of what I
am about to cover remains true. You can also add your default user
through that web client.

Now, the database is created upon first connection, and the 'sa' or
system administrator password and file encryption passwords are also
set upon first connection and creation. The following code for
example:

import java.sql.*;

public class TestConnection {
public static void main( String[] args ) {
try {
System.out.println( "Testing connection" );
Class.forName("org.h2.Driver");
String url = "jdbc:h2:ssl://localhost/test;CIPHER=AES";
String user = "sa";
String pwds = "test test";
Connection conn = DriverManager.getConnection(url, user, pwds);
System.out.println( "Test successful" );
} catch( Throwable t ) {
System.out.println( "Throwable - " + t );
}
}
}

Will create the database 'test' with all the files prefixed with
'test' in the 'database' directory wherever you create it. You will
have to connect to this database the same way everytime because the
files are encrypted with the file encryption password as well.

Good luck. Check out the default docs at www.h2database.com, they are
pretty good and all this is covered pretty well.

Thanks

Rich

Thotheolh

unread,
Mar 17, 2008, 1:38:01 AM3/17/08
to H2 Database
Thanks for the reply. I would try it out and see how H2 works.

I am planning a java project that requires login and some data to be
kept somewhere sufficiently safe and encrypted for my application. I
don't want to use buky databases like Oracle and MySQL which I have
used in the past because one of my aims is to keep the total entire
application package as small as possibel without the hassle to install
the database into the machine during deployment so I thought H2 have
matched up those requirements that i have so I would like to try it
olut and see how H2 performs in my application.

Thotheolh

unread,
Mar 18, 2008, 4:37:33 AM3/18/08
to H2 Database
Hi. I have another group of questions to ask.

1. How do you check for the existance of a table ? Do I need to carry
out a select statement to check if the table exist ?

2. I would like to select the first row , first column of a table, how
should I do that ?

3. I am creating a java application in Client-Server mode with SSL and
AES encryption. Besides adding the h2.jar into the project (NetBeans
6), do I need to install any program before my java application can
access the H2 database ?

4. I have a H2 installed in my computer. I have no trouble running H2
and getting to the web console although the command prompt(Win XP)
would say the following...

Port is in use, maybe another Web server already running on http://localhost:8082
Port is in use, maybe another TCP server already running on tcp://localhost:9092

Press any key to continue . . .

Despite the message, the web console started fine and I can connect
and create user and tables in the web console. Now when I was thinking
to start H2 to do some testing , the same message above appear but the
web console didn't appear instead it's a blank untitled page with a
pop up (Firefox 2) saying:

" You have chosen to open

which is a: application/octet-stream
from: http://localhsot:8082

What should Firefox do with this file ?

Open with ...

Save to Disc "

I am not sure why this time the web console failed ? All I did in the
pass few days was just copying the h2.jar into a few netbeans project
folders and compiling the java projects I made. Do I need to reinstall
again ?

Thotheolh

unread,
Mar 18, 2008, 4:40:15 AM3/18/08
to H2 Database
Hi. I was just curious if I reinstall the service would do any
difference and after reinstalling it and running the batch file to
start the H2.jar, it worked... but my data and users are
lost ...sigh ...

Anyway, do look into the above incident. I am curious to know what
happened.

Thotheolh

unread,
Mar 18, 2008, 4:43:10 AM3/18/08
to H2 Database
I thought I should reinstall the service and after reinstalling and
running H2, it worked... the web console was up again...although I lst
my users and data but luckily...these were test data and not
important.

Anyway, do look into the above ... I am curious what caused the
failure.

Thomas Mueller

unread,
Mar 18, 2008, 2:57:23 PM3/18/08
to h2-da...@googlegroups.com
Hi,

> 1. How do you check for the existance of a table ? Do I need to carry
> out a select statement to check if the table exist ?

Why do you need to check it?
See http://www.h2database.com/html/grammar.html for the grammar.
specially CREATE TABLE IF NOT EXISTS

> 2. I would like to select the first row , first column of a table, how
> should I do that ?

Maybe you should read a general SQL tutorial first, for example
http://www.w3schools.com/sql/default.asp
Tables don't have a 'first row'. Maybe you mean 'any row'.
SELECT ID FROM TEST LIMIT 1;
See http://www.h2database.com/html/grammar.html

> 3. I am creating a java application in Client-Server mode with SSL and
> AES encryption. Besides adding the h2.jar into the project (NetBeans
> 6), do I need to install any program before my java application can
> access the H2 database ?

No.

> 4. I have a H2 installed in my computer. I have no trouble running H2
> and getting to the web console although the command prompt(Win XP)
> would say the following...
>
> Port is in use, maybe another Web server already running on http://localhost:8082
> Port is in use, maybe another TCP server already running on tcp://localhost:9092
>
> Press any key to continue . . .
>
> Despite the message, the web console started fine and I can connect
> and create user and tables in the web console. Now when I was thinking
> to start H2 to do some testing , the same message above appear but the
> web console didn't appear instead it's a blank untitled page with a
> pop up (Firefox 2) saying:
>
> " You have chosen to open
>
> which is a: application/octet-stream
> from: http://localhsot:8082
>
> What should Firefox do with this file ?
>
> Open with ...
>
> Save to Disc "
>
> I am not sure why this time the web console failed ?

Maybe some other application is running on this port. What about
downloading this file and seeing what is the content (with Notepad).

> Do I need to reinstall
> again ?

Probably not.

Regards,
Thomas

Thomas Mueller

unread,
Mar 18, 2008, 2:59:14 PM3/18/08
to h2-da...@googlegroups.com
Hi,

> I thought I should reinstall the service and after reinstalling and
> running H2, it worked... the web console was up again...although I lst
> my users and data but luckily...these were test data and not
> important.

What database URL did you use?
jdbc:h2:~/test
should create the database in the home directory of the current user,
so the database should not get lost.

Regards,
Thomas

Rich

unread,
Mar 18, 2008, 9:23:34 PM3/18/08
to H2 Database
Hi -

> 1. How do you check for the existance of a table ? Do I need to carry
> out a select statement to check if the table exist ?

There are system tables that you can leverage for testing the
existence of object in the system. I am not sure why your application
would have to do such a thing, ... The application code should be
reasonably paired with a pre-prepared database schema IMHO.

> 2. I would like to select the first row , first column of a table, how
> should I do that ?

I am not sure about getting the column lists from the system. Someone
else will have to comment on that. First row is easy, check the SQL
grammar and build it into your query.

> 3. I am creating a java application in Client-Server mode with SSL and
> AES encryption. Besides adding the h2.jar into the project (NetBeans
> 6), do I need to install any program before my java application can
> access the H2 database ?

The beauty of H2 that I have found since starting to use it is that it
seems to be completely self contained. No, you don't need to install
anything else I think.

> 4. I have a H2 installed in my computer. I have no trouble running H2
> and getting to the web console although the command prompt(Win XP)
> would say the following...

The only time I have seen where the web app doesn't work was my fault
while I was messing with the internals. I have never seen that
behavior out of the box. Can you post exactly what you were up to
step by step?

Thanks

Rich

On Mar 18, 5:37 pm, Thotheolh <twzger...@gmail.com> wrote:
> Hi. I have another group of questions to ask.
>
> 1. How do you check for the existance of a table ? Do I need to carry
> out a select statement to check if the table exist ?
>
> 2. I would like to select the first row , first column of a table, how
> should I do that ?
>
> 3. I am creating a java application in Client-Server mode with SSL and
> AES encryption. Besides adding the h2.jar into the project (NetBeans
> 6), do I need to install any program before my java application can
> access the H2 database ?
>
> 4. I have a H2 installed in my computer. I have no trouble running H2
> and getting to the web console although the command prompt(Win XP)
> would say the following...
>
> Port is in use, maybe another Web server already running onhttp://localhost:8082

Rich

unread,
Mar 18, 2008, 9:24:53 PM3/18/08
to H2 Database
Hi

I never use the home directory, I always use -baseDir [] with a URL
that includes an absolute path: jdbc:h2:tcp://localhost/test ... is
there a problem with this configuration? I have never had a
problem ...

Thanks

Rich

On Mar 19, 3:59 am, "Thomas Mueller" <thomas.tom.muel...@gmail.com>
wrote:

Thotheolh

unread,
Mar 19, 2008, 7:45:44 PM3/19/08
to H2 Database
Hi. The same problem with H2 happened again when I tried to run
H2.bat . The firefox prompted the same thing and the command prompt
claim that the ports are not open.

I managed to take a screen shot of it. Where should I load the screen
shot ?

Thomas Mueller

unread,
Mar 22, 2008, 7:01:52 PM3/22/08
to h2-da...@googlegroups.com
Hi,

> I never use the home directory, I always use -baseDir [] with a URL
> that includes an absolute path: jdbc:h2:tcp://localhost/test ... is
> there a problem with this configuration? I have never had a
> problem ...

No, but it looks quite a lot of people have trouble with the database
path. Maybe I should just disallow relative database paths...

Regards,
Thomas

Thomas Mueller

unread,
Mar 22, 2008, 7:05:53 PM3/22/08
to h2-da...@googlegroups.com
Hi,

> The same problem with H2 happened again when I tried to run
> H2.bat . The firefox prompted the same thing and the command prompt
> claim that the ports are not open.

Could you check if the ports are in use before you start H2.bat? For
example (when using port 1039):

c:\data>telnet localhost 1039
Connecting To localhost...Could not open connection to the host, on
port 1039: Connect failed

This means the port is not in use. If it is in use, you will get a
blank screen or maybe some other strange data.

Regards,
Thomas

Thotheolh

unread,
Mar 23, 2008, 8:25:37 PM3/23/08
to H2 Database
Thanks for the help. Somehow after after I did an uninstall and
reinstall and start the service, everything is back to normal and it
is operating fine without trouble for a couple of days.

Thanks.
Reply all
Reply to author
Forward
0 new messages