1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License, Version 1.0,
and under the Eclipse Public License, Version 1.0
(http://h2database.com/html/license.html).
Initial Developer: H2 Group
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>
H2 Database Engine
</title>
<style type="text/css">
td, input, select, textarea, body, code, pre, td, th {
font: 9pt/130% Tahoma, Arial, Helvetica, sans-serif;
font-weight: normal;
}
h1, h2, h3, h4, h5 {
font: 9pt Tahoma, Arial, Helvetica, sans-serif;
font-weight: bold;
}
td, input, select, textarea, body, code, pre {
font-size: 9pt;
}
pre {
background-color: #ece9d8;
border: 1px solid rgb(172, 168, 153);
padding: 4px;
}
code {
background-color: #ece9d8;
padding: 0px 2px;
}
h1 {
background-color: #0000bb;
padding: 2px 4px 2px 4px;
color: #fff;
font-size: 15pt;
line-height: normal;
}
h2 {
font-size: 13pt;
}
h3 {
font-size: 10pt;
}
h4 {
font-size: 9pt;
}
table {
background-color: #ffffff;
border-collapse: collapse;
border: 1px solid #aca899;
}
th {
text-align: left;
background-color: #ece9d8;
border: 1px solid #aca899;
padding: 2px;
}
td {
background-color: #ffffff;
text-align: left;
vertical-align: top;
border: 1px solid #aca899;
padding: 2px;
}
</style>
</head><body>
<h1>H2 Database Engine Cheat Sheet</h1>
<h2>Downloads</h2>
<a href="http://www.h2database.com/h2-setup-2009-04-10.exe">Windows Installer</a> -
<a href="http://www.h2database.com/h2-2009-04-10.zip">Zip</a> -
<a href="http://repo1.maven.org/maven2/com/h2database/h2/1.1.111/h2-1.1.111.jar">Jar</a>
<h2>Using H2</h2>
<ul><li>Add h2*.jar to the classpath (the only dependency is Java 1.4 or newer)
</li><li>JDBC driver class: <code>org.h2.Driver</code>
</li><li>Database URL: <code>jdbc:h2:~/test</code>
</li><li>A new database is automatically created if it does not exist
</li><li>Closing the last connection closes a database
</li><li>H2 is open source, free to use and distribute. See also license.
</li></ul>
<h2>Database URL Shortlist</h2>
<table>
<tr><td rowspan="3">Embedded</td>
<td>jdbc:h2:~/test</td><td>'test' in the current user home directory</td></tr>
<tr><td>jdbc:h2:/data/test</td><td>'test' in the directory /data/</td></tr>
<tr><td>jdbc:h2:test</td><td>'test' in the current working directory</td></tr>
<tr><td rowspan="2">In-Memory</td>
<td>jdbc:h2:mem:test</td><td>named; multiple connections within the same process</td></tr>
<tr><td>jdbc:h2:mem:</td><td>unnamed private database; only one connections</td></tr>
<tr><td rowspan="2">Server Mode</td>
<td>jdbc:h2:tcp://localhost/~/test</td><td>'test' in the current user home directory</td></tr>
<tr><td>jdbc:h2:tcp://localhost//data/test</td><td>'test' in the directory /data/</td></tr>
<tr><td>Compatibility</td>
<td>jdbc:h2:...;MODE=MySQL</td><td>or DB2, Derby, HSQLDB, MSSQLServer, Oracle, PostgreSQL</td></tr>
<tr><td>Debug</td>
<td>jdbc:...;TRACE_LEVEL_FILE=3</td><td>logs JDBC calls to test.trace.db</td></tr>
</table>
<h2>H2 Console Tool</h2>
Start: double click the h2*.jar file, or run <code>java -jar h2*.jar</code>, <code>h2.bat</code> or <code>h2.sh</code>.
<h2>Maven</h2>
<pre>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${version}</version>
</dependency>
</pre>
<h2>Hibernate</h2>
Use the H2 or the HSQLDB dialect.
<h2>TopLink and Glassfish</h2>
Datasource Classname: org.h2.jdbcx.JdbcDataSource<br />
toplink.target-database: oracle.toplink.essentials.platform.database.H2Platform
<h2>Connect using JDBC</h2>
<pre>
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
conn.close();
</pre>
<h2>Connection Pool</h2>
<pre>
JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:~/test", "sa", "");
Connection conn = cp.getConnection(); ... conn.close();
cp.dispose();
</pre>
<h2>Start a Server</h2>
<pre>
java -cp h2*.jar org.h2.tools.Server
</pre>
<h2>Reference</h2>
<a href="http://www.h2database.com/html/grammar.html">SQL Grammar</a>
(or run 'HELP <command>' in the H2 Console) -
<a href="http://www.h2database.com/html/functions.html">Functions</a> -
<a href="http://www.h2database.com/html/datatypes.html">Data Types</a>
<h2>Other Features</h2>
Fulltext search -
Database Files Encryption -
Read Only Databases -
Read Only Databases in Zip or Jar File
<h2>Command Line Tools</h2>
Backup - ChangeFileEncryption - Console - ConvertTraceFile - CreateCluster -
DeleteDbFiles - Recover - Restore - RunScript - Script - Server - Shell
<h2>Reading CSV files</h2>
SELECT * FROM CSVREAD('test.csv');
</body></html>