tutorial.html 29.7 KB
Newer Older
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
<!--
3 4 5
Copyright 2004-2008 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).
6 7
Initial Developer: H2 Group
-->
8 9
<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>
10
Tutorial
11
</title><link rel="stylesheet" type="text/css" href="stylesheet.css" />
12
<!-- [search] { -->
13 14 15
<script type="text/javascript" src="navigation.js"></script>
</head><body onload="frameMe();">
<table class="content"><tr class="content"><td class="content"><div class="contentDiv">
16
<!-- } -->
17 18 19

<h1>Tutorial</h1>
<a href="#tutorial_starting_h2_console">
20
    Starting and Using the H2 Console</a><br />
21
<a href="#connecting_using_jdbc">
22
    Connecting to a Database using JDBC</a><br />
23
<a href="#creating_new_databases">
24
    Creating New Databases</a><br />
25
<a href="#using_server">
26
    Using the Server</a><br />
27
<a href="#using_hibernate">
28
    Using Hibernate</a><br />
29
<a href="#web_applications">
30
    Using Databases in Web Applications</a><br />
31
<a href="#csv">
32
    CSV (Comma Separated Values) Support</a><br />
33
<a href="#upgrade_backup_restore">
34
    Upgrade, Backup, and Restore</a><br />
35 36
<a href="#command_line_tools">
    Command Line Tools</a><br />
37
<a href="#open_office">
38
    Using OpenOffice Base</a><br />
39
<a href="#web_start">
40
    Java Web Start / JNLP</a><br />
41 42
<a href="#connection_pool">
    Using a Connection Pool</a><br />
43 44
<a href="#fulltext">
    Fulltext Search</a><br />
45
<a href="#user_defined_variables">
46
    User-Defined Variables</a><br />
47 48
<a href="#date_time">
    Date and Time</a><br />
49

50
<br /><a name="tutorial_starting_h2_console"></a>
51
<h2>Starting and Using the H2 Console</h2>
52
<p>
53 54
This application lets you access a SQL database using a browser interface.
This can be a H2 database, or another database that supports the JDBC API.
55
</p>
56
<img src="images/console.png" alt="Web Browser - H2 Console Server - H2 Database" />
57
<p>
58
This is a client / server application, so both a server and a client (a browser) are required to run it.
59
</p><p>
60
Depending on your platform and environment, there are multiple ways to start the application:
61
</p>
62
<table><tr><th>OS</th><th>Start</th></tr>
63 64 65
<tr>
  <td>Windows</td>
  <td>
66 67 68
    Click [Start], [All Programs], [H2], and [H2 Console (Command Line)]<br />
    When using the Sun JDK 1.4 or 1.5, a window with the title 'H2 Console ' should appear.
    When using the Sun JDK 1.6, an icon will be added to the system tray:
69
    <img src="images/db-16.png" alt="[H2 icon]" /><br />
70
    If you don't get the window and the system tray icon,
71
    then maybe Java is not installed correctly (in this case, try another way to start the application).
72
    A browser window should open and point to the Login page http://localhost:8082).
73 74 75 76 77
  </td>
</tr>
<tr>
  <td>Windows</td>
  <td>
78
    Open a file browser, navigate to h2/bin, and double click on h2.bat.<br />
79 80
    A console window appears. If there is a problem, you will see an error message
    in this window. A browser window will open and point to the Login page
81
    (URL: http://localhost:8082).
82 83 84 85 86
  </td>
</tr>
<tr>
  <td>Any</td>
  <td>
87
      Open a console window, navigate to the directory 'h2/bin' and type:
88 89 90 91 92 93 94 95
<pre>
java -cp h2.jar org.h2.tools.Server
</pre>
  </td>
</tr>
</table>

<h3>Firewall</h3>
96
<p>
97
If you start the server, you may get a security warning from the firewall (if you have installed one).
98
If you don't want other computers in the network to access the application on your machine, you can
99 100 101
let the firewall block those connections. The connection from the local machine will still work.
Only if you want other computers to access the database on this computer, you need allow remote connections
in the firewall.
102
</p><p>
103
A small firewall is already built into the server: other computers may not connect to the server by default.
104
To change this, go to 'Preferences' and select 'Allow connections from other computers'.
105
</p>
106 107

<h3>Native Version</h3>
108
<p>
109 110
The native version does not require Java, because it is compiled using GCJ.
However H2 does currently not run stable with GCJ on Windows
111
It is possible to compile the software to different platforms.
112
</p>
113 114

<h3>Testing Java</h3>
115
<p>
116
To check the Java version you have installed, open a command prompt and type:
117
</p>
118 119 120
<pre>
java -version
</pre>
121
<p>
122
If you get an error message, you may need to add the Java binary directory to the path environment variable.
123
</p>
124 125

<h3>Error Message 'Port is in use'</h3>
126
<p>
127
You can only start one instance of the H2 Console,
128 129 130 131
otherwise you will get the following error message:
<code>Port is in use, maybe another ... server already running on...</code>.
It is possible to start multiple console applications on the same computer (using different ports),
but this is usually not required as the console supports multiple concurrent connections.
132
</p>
133 134

<h3>Using another Port</h3>
135
<p>
136 137 138 139
If the port is in use by another application, you may want to start the H2 Console on a different port.
This can be done by changing the port in the file .h2.server.properties. This file is stored
in the user directory (for Windows, this is usually in "Documents and Settings/&lt;username&gt;").
The relevant entry is webPort.
140
</p>
141 142

<h3>Starting Successfully</h3>
143
<p>
144
If starting the server from a console window was successful,
145
a new window will open and display the following text:
146
</p>
147 148 149 150
<pre>
H2 Server running on port 9092
Webserver running on https://localhost:8082/
</pre>
151
<p>
152
Don't click inside this window; otherwise you might block the application (if you have the Fast-Edit mode enabled).
153
</p>
154 155

<h3>Connecting to the Server using a Browser</h3>
156
<p>
157 158 159 160
If the server started successfully, you can connect to it using a web browser.
The browser needs to support JavaScript, frames and cascading stylesheets (css).
If you started the server on the same computer as the browser, go to http://localhost:8082 in the browser.
If you want to connect to the application from another computer, you need to provide the IP address of the server, for example:
161 162
<a href="http://192.168.0.2:8082">http://192.168.0.2:8082</a>. 
If you enabled SSL on the server side, the URL needs to start with HTTPS.
163
</p>
164 165

<h3>Multiple Concurrent Sessions</h3>
166
<p>
167 168
Multiple concurrent browser sessions are supported. As that the database objects reside on the server,
the amount of concurrent work is limited by the memory available to the server application.
169
</p>
170 171

<h3>Application Properties</h3>
172
<p>
173 174 175
Starting the server will create a configuration file in you local home directory called <code>.h2.server.properties</code>.
For Windows installations, this file will be in the directory <code>C:\Documents and Settings\[username]</code>.
This file contains the settings of the application.
176
</p>
177 178

<h3>Login</h3>
179
<p>
180 181 182
At the login page, you need to provide connection information to connect to a database.
Set the JDBC driver class of your database, the JDBC URL, user name and password.
If you are done, click [Connect].
183
</p><p>
184
You can save and reuse previously saved settings. The settings are stored in the
185
Application Properties file.
186
</p>
187 188

<h3>Error Messages</h3>
189
<p>
190 191
Error messages in are shown in red. You can show/hide the stack trace of the exception
by clicking on the message.
192
</p>
193 194

<h3>Adding Database Drivers</h3>
195
<p>
196
Additional database drivers can be registered by adding the Jar file location of the driver to the environment
197 198
variables H2DRIVERS or CLASSPATH. Example (Windows): To add the database driver library
C:\Programs\hsqldb\lib\hsqldb.jar, set the environment variable H2DRIVERS to
199 200
C:\Programs\hsqldb\lib\hsqldb.jar.
</p><p>
201 202
Multiple drivers can be set; each entry needs to be separated with a ';' (Windows) or ':' (other operating systems).
Spaces in the path names are supported. The settings must not be quoted.
203
</p>
204 205

<h3>Using the Application</h3>
206
<p>
207 208 209
The application has three main panels, the toolbar on top, the tree on the left and the query / result panel on the right.
The database objects (for example, tables) are listed on the left panel.
Type in a SQL command on the query panel and click 'Run'. The result of the command appears just below the command.
210
</p>
211 212

<h3>Inserting Table Names or Column Names</h3>
213
<p>
214 215 216 217
The table name and column names can be inserted in the script by clicking them in the tree.
If you click on a table while the query is empty, a 'SELECT * FROM ...' is added as well.
While typing a query, the table that was used is automatically expanded in the tree.
For, example if you type 'SELECT * FROM TEST T WHERE T.' then the table TEST is automatically expanded in the tree.
218
</p>
219 220

<h3>Disconnecting and Stopping the Application</h3>
221
<p>
222
On the browser, click 'Disconnect' on the toolbar panel. You will be logged out of the database.
223 224
However, the server is still running and ready to accept new sessions.
</p><p>
225 226
To stop the server, right click on the system tray icon and select [Exit].
If you don't have the icon (because you started it in another way),
227 228
press [Ctrl]+[C] on the console where the server was started (Windows),
or close the console window.
229
</p>
230

231
<br /><a name="connecting_using_jdbc"></a>
232
<h2>Connecting to a Database using JDBC</h2>
233
<p>
234
To connect to a database, a Java application first needs to load the database driver,
235
and then get a connection. A simple way to do that is using the following code:
236
</p>
237 238 239
<pre>
import java.sql.*;
public class Test {
240
  public static void main(String[] a)
241 242 243
  throws Exception {
    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.
244
      getConnection("jdbc:h2:~/test", "sa", "");
245 246 247 248
    // add application code here
  }
}
</pre>
249
<p>
250 251 252 253 254 255 256
This code first loads the driver (<code>Class.forName()</code>)
and then opens a connection (using <code>DriverManager.getConnection()</code>).
The driver name is <code>"org.h2.Driver"</code> in every case.
The database URL always needs to start with <code>jdbc:h2:</code>
to be recognized by this database. The second parameter in the <code>getConnection()</code> call
is the user name ('sa' for System Administrator in this example). The third parameter is the password.
Please note that in this database, user names are not case sensitive, but passwords are case sensitive.
257
</p>
258

259
<br /><a name="creating_new_databases"></a>
260
<h2>Creating New Databases</h2>
261
<p>
262
By default, if the database specified in the URL does not yet exist, a new (empty)
263 264
database is created automatically. The user that created the database automatically becomes
the administrator of this database.
265
</p>
266

267
<br /><a name="using_server"></a>
268
<h2>Using the Server</h2>
269
<p>
270 271
H2 currently supports three servers: a Web Server, a TCP Server and an ODBC Server.
The servers can be started in different ways.
272
</p>
273

274
<h3>Starting the Server from Command Line</h3>
275
<p>
276
To start the Server from the command line with the default settings, run
277
</p>
278 279 280
<pre>
java org.h2.tools.Server
</pre>
281
<p>
282
This will start the Server with the default options. To get the list of options and default values, run
283
</p>
284 285 286
<pre>
java org.h2.tools.Server -?
</pre>
287
<p>
288
There are options available to use different ports, and start or not start
289
parts of the Server and so on. For details, see the API documentation of the Server tool.
290
</p>
291

292
<h3>Connecting to the TCP Server</h3>
293
<p>
294
To remotely connect to a database using the TCP server, use the following driver and database URL:
295
</p>
296 297 298 299
<ul>
<li>JDBC driver class: org.h2.Driver
</li><li>Database URL: jdbc:h2:tcp://localhost/~/test
</li></ul>
300
<p>
301
For details about the database URL, see also in Features.
302
</p>
303 304

<h3>Starting the Server within an Application</h3>
305
<p>
306
It is also possible to start and stop a Server from within an application. Sample code:
307
</p>
308 309 310
<pre>
import org.h2.tools.Server;
...
311
// start the TCP Server
312
Server server = Server.createTcpServer(args).start();
313 314 315 316 317 318
...
// stop the TCP Server
server.stop();
</pre>

<h3>Stopping a TCP Server from Another Process</h3>
319
<p>
320
The TCP Server can be stopped from another process.
321
To stop the server from the command line, run:
322
</p>
323 324 325
<pre>
java org.h2.tools.Server -tcpShutdown tcp://localhost:9092
</pre>
326
<p>
327
To stop the server from a user application, use the following code:
328
</p>
329 330 331
<pre>
org.h2.tools.Server.shutdownTcpServer("tcp://localhost:9094");
</pre>
332
<p>
333
This function will call System.exit on the server.
334
This function should be called after all connections to the databases are closed
335 336
to avoid recovery when the databases are opened the next time.
To stop remote server, remote connections must be enabled on the server.
337
</p>
338

339
<br /><a name="using_hibernate"></a>
340
<h2>Using Hibernate</h2>
341
<p>
342
This database supports Hibernate version 3.1 and newer. You can use the HSQLDB Dialect,
343 344 345 346 347
or the native H2 Dialect. Unfortunately the H2 Dialect included in Hibernate is buggy. A 
<a href="http://opensource.atlassian.com/projects/hibernate/browse/HHH-3401">patch
for Hibernate</a> has been submitted. The dialect for the newest version of Hibernate 
is also available at src/tools/org/hibernate/dialect/H2Dialect.java.txt.
You can rename it to H2Dialect.java and include this as a patch in your application.
348
</p>
349

350
<br /><a name="web_applications"></a>
351
<h2>Using Databases in Web Applications</h2>
352
<p>
353 354
There are multiple ways to access a database from within web
applications. Here are some examples if you use Tomcat or JBoss.
355
</p>
356 357

<h3>Embedded Mode</h3>
358
<p>
359
The (currently) simplest solution is to use the database in the
360 361 362 363 364 365 366 367 368 369
embedded mode, that means open a connection in your application when
it starts (a good solution is using a Servlet Listener, see below), or
when a session starts. A database can be accessed from multiple
sessions and applications at the same time, as long as they run in the
same process. Most Servlet Containers (for example Tomcat) are just
using one process, so this is not a problem (unless you run Tomcat in
clustered mode). Tomcat uses multiple threads and multiple
classloaders. If multiple applications access the same database at the
same time, you need to put the database jar in the shared/lib or
server/lib directory. It is a good idea to open the database when the
370
web application starts, and close it when the web application stops.
371 372 373 374 375
If using multiple applications, only one (any) of them needs to do
that. In the application, an idea is to use one connection per
Session, or even one connection per request (action). Those
connections should be closed after use if possible (but it's not that
bad if they don't get closed).
376
</p>
377 378

<h3>Server Mode</h3>
379
<p>
380
The server mode is similar, but it allows you to run the server in another process.
381
</p>
382 383

<h3>Using a Servlet Listener to Start and Stop a Database</h3>
384
<p>
385 386
Add the h2.jar file your web application, and
add the following snippet to your web.xml file (after context-param and before filter):
387
</p>
388 389
<pre>
&lt;listener>
390
   &lt;listener-class>org.h2.server.web.DbStarter&lt;/listener-class>
391 392
&lt;/listener>
</pre>
393
<p>
394
For details on how to access the database, see the code DbStarter.java
395
</p>
396
<p>
397
By default the DbStarter listener opens an embedded connection using the database URL jdbc:h2:~/test 
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
and user name and password 'sa'. It can also start the TCP server, however this is disabled by default. 
To enable it, use the db.tcpServer parameter in web.xml. Here is the complete list of options.
These options are set just after the display-name and description tag, but before any listener and filter tags:
</p>
<pre>
&lt;context-param>
   &lt;param-name>db.url&lt;/param-name>
   &lt;param-value>jdbc:h2:~/test&lt;/param-value>
&lt;/context-param>
&lt;context-param>
   &lt;param-name>db.user&lt;/param-name>
   &lt;param-value>sa&lt;/param-value>
&lt;/context-param>
&lt;context-param>
   &lt;param-name>db.password&lt;/param-name>
   &lt;param-value>sa&lt;/param-value>
&lt;/context-param>
&lt;context-param>
   &lt;param-name>db.tcpServer&lt;/param-name>
   &lt;param-value>-tcpAllowOthers&lt;/param-value>
&lt;/context-param>
</pre>
420 421 422 423 424
<p>
When the web application is stopped, the database connection will be closed automatically.
If the server is started within the DbStarter, it will also be stopped automatically when the 
web application is stopped.
</p>
425

426
<br /><a name="csv"></a>
427
<h2>CSV (Comma Separated Values) Support</h2>
428
<p>
429
The CSV file support can be used inside the database using the functions CSVREAD and CSVWRITE,
430
and the CSV library can be used outside the database as a standalone tool.
431
</p>
432 433

<h3>Writing a CSV File from Within a Database</h3>
434
<p>
435 436
The built-in function CSVWRITE can be used to create a CSV file from a query.
Example:
437
</p>
438 439 440 441 442 443 444
<pre>
CREATE TABLE TEST(ID INT, NAME VARCHAR);
INSERT INTO TEST VALUES(1, 'Hello'), (2, 'World');
CALL CSVWRITE('test.csv', 'SELECT * FROM TEST');
</pre>

<h3>Reading a CSV File from Within a Database</h3>
445
<p>
446
A CSV file can be read using the function CSVREAD. Example:
447
</p>
448 449 450 451 452
<pre>
SELECT * FROM CSVREAD('test.csv');
</pre>

<h3>Writing a CSV File from a Java Application</h3>
453
<p>
454 455
The CSV tool can be used in a Java application even when not using a database at all.
Example:
456
</p>
457
<pre>
458 459 460
import org.h2.tools.Csv;
import org.h2.tools.SimpleResultSet;
...
461 462 463 464
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("NAME", Types.VARCHAR, 255, 0);
rs.addColumn("EMAIL", Types.VARCHAR, 255, 0);
rs.addColumn("PHONE", Types.VARCHAR, 255, 0);
465 466 467
rs.addRow(new String[] { "Bob Meier", "bob.meier@abcde.abc", "+41123456789" });
rs.addRow(new String[] { "John Jones", "john.jones@abcde.abc", "+41976543210" });
Csv.getInstance().write("data/test.csv", rs, null);
468 469 470
</pre>

<h3>Reading a CSV File from a Java Application</h3>
471
<p>
472 473
It is possible to read a CSV file without opening a database.
Example:
474
</p>
475
<pre>
476 477 478
import org.h2.tools.Csv;
...
ResultSet rs = Csv.getInstance().read("data/test.csv", null, null);
479
ResultSetMetaData meta = rs.getMetaData();
480 481 482
while (rs.next()) {
    for (int i = 0; i &lt; meta.getColumnCount(); i++) {
        System.out.println(meta.getColumnLabel(i + 1) + ": " + rs.getString(i + 1));
483 484 485 486 487 488
    }
    System.out.println();
}
rs.close();
</pre>

489
<br /><a name="upgrade_backup_restore"></a>
490 491 492
<h2>Upgrade, Backup, and Restore</h2>

<h3>Database Upgrade</h3>
493
<p>
494 495 496
The recommended way to upgrade from one version of the database engine to the next
version is to create a backup of the database (in the form of a SQL script) using the old engine,
and then execute the SQL script using the new engine.
497
</p>
498

499
<h3>Backup using the Script Tool</h3>
500
<p>
501 502
There are different ways to backup a database. For example, it is possible to copy the database files.
However, this is not recommended while the database is in use. Also, the database files are not human readable
503
and quite large. The recommended way to backup a database is to create a compressed SQL script file.
504
This can be done using the Script tool:
505
</p>
506
<pre>
507
java org.h2.tools.Script -url jdbc:h2:~/test -user sa -script test.zip -options compression zip
508
</pre>
509
<p>
510
It is also possible to use the SQL command SCRIPT to create the backup of the database.
511 512 513
For more information about the options, see the SQL command SCRIPT.
The backup can be done remotely, however the file will be created on the server side.
The built in FTP server could be used to retrieve the file from the server.
514
</p>
515

516
<h3>Restore from a Script</h3>
517
<p>
518
To restore a database from a SQL script file, you can use the RunScript tool:
519
</p>
520
<pre>
521
java org.h2.tools.RunScript -url jdbc:h2:~/test -user sa -script test.zip -options compression zip
522
</pre>
523
<p>
524 525 526 527 528 529 530
For more information about the options, see the SQL command RUNSCRIPT.
The restore can be done remotely, however the file needs to be on the server side.
The built in FTP server could be used to copy the file to the server.
It is also possible to use the SQL command RUNSCRIPT to execute a SQL script.
SQL script files may contain references to other script files, in the form of
RUNSCRIPT commands. However, when using the server mode, the references script files
need to be available on the server side.
531
</p>
532

533
<h3>Online Backup</h3>
534
<p>
535 536
The BACKUP SQL statement and the Backup tool both create a zip file
with all database files. However, the contents of this file are not human readable.
537 538 539
Other than the SCRIPT statement, the BACKUP statement does not lock the
database objects, and therefore does not block other users. The resulting
backup is transactionally consistent:
540
</p>
541 542 543
<pre>
BACKUP TO 'backup.zip'
</pre>
544
<p>
545 546
The Backup tool (org.h2.tools.Backup) can not be used to create a online backup;
the database must not be in use while running this program.
547
</p>
548

549 550 551 552 553 554 555 556 557 558 559 560 561
<br /><a name="command_line_tools"></a>
<h2>Command Line Tools</h2>
<p>
This database comes with a number of command line tools. To get more information about a tool,
start it with the parameter '-?', for example:
</p>
<pre>
java -cp h2.jar org.h2.tools.Backup -?
</pre>
<p>
The command line tools are:
</p>
<ul><li><b>Backup</b> creates a backup of a database.
562
</li><li><b>ChangeFileEncryption</b> allows changing the file encryption password or algorithm of a database.
563 564 565 566
</li><li><b>Console</b> starts the browser based H2 Console.
</li><li><b>ConvertTraceFile</b> converts a .trace.db file to a Java application and SQL script.
</li><li><b>CreateCluster</b> creates a cluster from a standalone database.
</li><li><b>DeleteDbFiles</b> deletes all files belonging to a database.
567
</li><li><b>Script</b> allows converting a database to a SQL script for backup or migration.
568 569 570 571 572 573 574 575 576 577 578
</li><li><b>Recover</b> helps recovering a corrupted database.
</li><li><b>Restore</b> restores a backup of a database.
</li><li><b>RunScript</b> runs a SQL script against a database.
</li><li><b>Server</b> is used in the server mode to start a H2 server.
</li><li><b>Shell</b> is a command line database tool. 
</li></ul>
<p>
The tools can also be called from an application by calling the main or another public methods. 
For details, see the Javadoc documentation.
</p>

579
<br /><a name="open_office"></a>
580
<h2>Using OpenOffice Base</h2>
581
<p>
582
OpenOffice.org Base supports database access over the JDBC API. To connect to a H2 database
583 584
using OpenOffice Base, you first need to add the JDBC driver to OpenOffice.
The steps to connect to a H2 database are:
585
</p>
586 587 588 589 590
<ul><li>Start OpenOffice Writer, go to [Tools], [Options]
</li><li>Make sure you have selected a Java runtime environment in OpenOffice.org / Java
</li><li>Click [Class Path...], [Add Archive...]
</li><li>Select your h2.jar (location is up to you, could be wherever you choose)
</li><li>Click [OK] (as much as needed), stop OpenOffice (including the Quickstarter)
591
</li><li>Start OpenOffice Base
592
</li><li>Connect to an existing database; select JDBC; [Next]
593
</li><li>Example datasource URL: jdbc:h2:~/test
594 595
</li><li>JDBC driver class: org.h2.Driver
</li></ul>
596
<p>
597
Now you can access the database stored in the current users home directory.
598
</p>
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
<p>
To use H2 in NeoOffice (OpenOffice without X11):
</p>
<ul><li>In NeoOffice, go to [NeoOffice], [Preferences]
</li><li>Look for the page under [NeoOffice], [Java]
</li><li>Click [Classpath], [Add Archive...]
</li><li>Select your h2.jar (location is up to you, could be wherever you choose)
</li><li>Click [OK] (as much as needed), restart NeoOffice.
</li></ul>
<p>
Now, when creating a new database using the "Database Wizard":
</p>
<ul><li>Select "connect to existing database" and the type "jdbc". Click next.
</li><li>Enter your h2 database URL. The normal behavior of H2 is that a new db is created if it doesn't exist.
</li><li>Next step - up to you... you can just click finish and start working.
</li></ul>
<p>
Another solution to use H2 in NeoOffice is:
</p>
<ul><li>Package the h2 jar within an extension package
</li><li>Install it as a Java extension in NeoOffice
</li></ul>
<p>
This can be done by create it using the NetBeans OpenOffice plugin.
See also <a href="http://wiki.services.openoffice.org/wiki/Extensions_development_java">Extensions Development</a>.
</p>
625 626 627

<br /><a name="web_start"></a>
<h2>Java Web Start / JNLP</h2>
628
<p>
629 630
When using Java Web Start / JNLP (Java Network Launch Protocol), permissions tags must be set in the .jnlp file,
and the application .jar file must be signed. Otherwise, when trying to write to the file system, the following
631 632
exception will occur: java.security.AccessControlException: access denied (java.io.FilePermission ... read).
Example permission tags:
633
</p>
634 635
<pre>
&lt;security>
636
    &lt;all-permissions/>
637 638 639
&lt;/security>
</pre>

640 641 642 643 644
<br /><a name="connection_pool"></a>
<h2>Using a Connection Pool</h2>
<p>
For many databases, opening a connection is slow, and it is a good idea to use a connection pool
to re-use connections. For H2 however opening a connection usually is fast if the database is already
645
open. Using a connection pool for H2 actually slows down the process a bit, except if
646
file encryption is used (in this case opening a connection is about half as fast as using
647
a connection pool). A simple connection pool is included in H2. It is based on the
648 649 650
<a href="http://www.source-code.biz/snippets/java/8.htm">Mini Connection Pool Manager</a>
from Christian d'Heureuse. There are other, more complex connection pools available, for example 
<a href="http://jakarta.apache.org/commons/dbcp/">DBCP</a>. The build-in
651
connection pool is used as follows:
652
</p>
653 654
<pre>
// init
655 656
import org.h2.jdbcx.*;
...
657 658 659 660
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:~/test");
ds.setUser("sa");
ds.setPassword("sa");
661
JdbcConnectionPool cp = JdbcConnectionPool.create(ds);
662 663

// use
664
Connection conn = cp.getConnection();
665 666 667 668
...
conn.close();

// dispose
669
cp.dispose();
670
</pre>
671

672 673 674
<br /><a name="fulltext"></a>
<h2>Fulltext Search</h2>
<p>
675
H2 supports Lucene full text search and native full text search implementation.
676 677 678 679 680
</p>

<h3>Using the Native Full Text Search</h3>
<p>
To initialize, call:
681
</p>
682 683 684 685 686
<pre>
CREATE ALIAS IF NOT EXISTS FT_INIT FOR "org.h2.fulltext.FullText.init";
CALL FT_INIT();
</pre>
<p>
687
You need to initialize it in each database where you want to use it.
688 689 690 691 692 693 694 695
Afterwards, you can create a full text index for a table using:
</p>
<pre>
CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR);
INSERT INTO TEST VALUES(1, 'Hello World');
CALL FT_CREATE_INDEX('PUBLIC', 'TEST', NULL);
</pre>
<p>
696 697
PUBLIC is the schema, TEST is the table name. The list of column names (column separated) is optional,
in this case all columns are indexed. The index is updated in read time.
698 699 700 701 702 703 704 705 706 707 708 709 710 711
To search the index, use the following query:
</p>
<pre>
SELECT * FROM FT_SEARCH('Hello', 0, 0);
</pre>
<p>
You can also call the index from within a Java application:
</p>
<pre>
org.h2.fulltext.FullText.search(conn, text, limit, offset)
</pre>

<h3>Using the Lucene Fulltext Search</h3>
<p>
712
To use the Lucene full text search, you need the Lucene library in the classpath.
713
How his is done depends on the application; if you use the H2 Console, you can add the Lucene
714
jar file to the environment variables H2DRIVERS or CLASSPATH.
715
To initialize the Lucene full text search in a database, call:
716 717 718 719 720 721
</p>
<pre>
CREATE ALIAS IF NOT EXISTS FTL_INIT FOR "org.h2.fulltext.FullTextLucene.init";
CALL FTL_INIT();
</pre>
<p>
722
You need to initialize it in each database where you want to use it.
723 724 725 726 727 728 729 730
Afterwards, you can create a full text index for a table using:
</p>
<pre>
CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR);
INSERT INTO TEST VALUES(1, 'Hello World');
CALL FTL_CREATE_INDEX('PUBLIC', 'TEST', NULL);
</pre>
<p>
731
PUBLIC is the schema, TEST is the table name. The list of column names (column separated) is optional,
732 733 734 735 736 737 738 739 740 741 742 743
in this case all columns are indexed. The index is updated in read time. To search the index, use the following query:
</p>
<pre>
SELECT * FROM FTL_SEARCH('Hello', 0, 0);
</pre>
<p>
You can also call the index from within a Java application:
</p>
<pre>
org.h2.fulltext.FullTextLucene.search(conn, text, limit, offset)
</pre>

744
<br /><a name="user_defined_variables"></a>
745
<h2>User-Defined Variables</h2>
746
<p>
747
This database supports user-defined variables. Variables start with @ and can be used wherever
748 749 750 751 752 753 754 755 756 757 758 759 760 761
expressions or parameters are used. Variables not persisted and session scoped, that means only visible for
the session where they are defined. A value is usually assigned using the SET command:
</p>
<pre>
SET @USER = 'Joe';
</pre>
<p>
It is also possible to change a value using the SET() method. This is useful in queries:
</p>
<pre>
SET @TOTAL = NULL;
SELECT X, SET(@TOTAL, IFNULL(@TOTAL, 1.) * X) F FROM SYSTEM_RANGE(1, 50);
</pre>
<p>
762
Variables that are not set evaluate to NULL. The data type of a user-defined variable is the data type
763
of the value assigned to it, that means it is not necessary (or possible) to declare variable names before using them.
764
There are no restrictions on the assigned values; large objects (LOBs) are supported as well.
765 766
</p>

767 768 769
<br /><a name="date_time"></a>
<h2>Date and Time</h2>
<p>
770
Date, time and timestamp values support ISO 8601 formatting, including time zone:
771
</p>
772 773 774
<pre>
CALL TIMESTAMP '2008-01-01 12:00:00+01:00';
</pre>
775
<p>
776
If the time zone is not set, the value is parsed using the current time zone setting of the system. 
777
Date and time information is stored in H2 database files in GMT (Greenwich Mean Time). 
778 779
If the database is opened using another system time zone, the date and time will change accordingly. 
If you want to move a database from one time zone to the other and don't want this to happen, 
780
you need to create a SQL script file using the SCRIPT command or Script tool, and then load 
781
the database using the RUNSCRIPT command or the RunScript tool in the new time zone.
782 783
</p>

784 785
<!-- [close] { --></div></td></tr></table><!-- } --><!-- analytics --></body></html>