H2 is a pure Java database. It can work as embedded database or in server mode. The developers of the database have made all to have a very small footprint for this database, it takes around 1MB jar file size.
Performances
Another feature the developers find important in this database is the performance. Here are two comparison graphs for H2, Derby and HSQLDB, generated directly from the benchmark data from the official H2 site.
Performances of H2 in Embedded Mode
Performances of H2 in Server Mode
Modes
H2 can work in three modes. Embedded, Server or Mixed modes. In each mode, you can use memory or persistent tables.
In this mode, the database is only accessible from the current virtual machine. It’s the fastest connection mode.
In this mode, the database runs in a server, so several applications can access the database engines. Of course, this mode is slower than in embedded mode, because all datas are transfered using TCP/IP.
H2 Mixed Mode
In this mode, the first application create the application in embedded mode but also start a server to provide access to the database.
Use
The use of this database is as simple as the use of any other database. You just have to create to the database using the JDBC Driver and the database will be automatically created if needed or the server launched if needed.
By example, here it’s an example to create an database in embedded mode, you just have to do that :
1
Class.forName(
"org.hsqldb.jdbcDriver"
).newInstance();
2
Connection connexion = DriverManager.getConnection(
"jdbc:h2:test"
,
"sa"
,
""
);
test is the name of the database. If the database already exists, the data will be loaded.
You can also create a database in memory. There is no persisting data. When you close the database, there is no data persisted. It’s useful for benchmarking by example. It’s also the mode with the best performances. Here is an example to create a memory database (named test) :
1
Connection connexion = DriverManager.getConnection(
"jdbc:h2:mem:test"
,
"sa"
,
""
);
To start the server, you have two choice. You can launch it from the command line :
1
java -cp h2*.jar org.h2.tools.Server
Or programmatically :
1
Server server = Server.createTcpServer(args).start();
2
...
3
server.stop();
And to connect to a remove server, just change the JDBC URL :
1
Connection connexion = DriverManager.getConnection(
"jdbc:h2:tcp://localhost/~/test"
,
"sa"
,
""
);
The database is closed when the last connection to the database is closed.
The SQL used is strandard SQL with some adds to support the features of H2. The syntax is described on the oficial site : SQL Grammar. By example, here are the SQL requests for creating the different types of database of H2 :
1
CREATE
CACHED
TABLE
cachedTable ...
2
CREATE
MEMORY
TABLE
memTable ...
3
CREATE
MEMORY
TEMPORARY
TABLE
memTempTable ...
4
CREATE
CACHED
TEMPORARY
TABLE
memTempTable ...
- A cached table is the default type of table. The data are persistent and not limited by the memory.
- A memory table is also persistent but the index of data is kept in the memory, so memory table cannot be too large.
- A temporary table is deleted when closing the database.
So, I think we covered here the main things we must know to start using H2 Database Engine.
[Original Post]: http://www.baptiste-wicht.com/2010/08/presentation-usage-h2-database-engine/
———————————————————————————————————————————-
Here’s My First Trial with H2, and I uploaded to the BaiduYun. http://pan.baidu.com/s/1qWsF6jU
Code Explains Everything, You Know^^;;
After Starting a Server Inside the Program, You will be able to connect to it by typing the url and port in the WebBrowser.
You can see all the records can be retrieved by it 🙂