<!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, 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; } h1 { background-color: #0000bb; padding: 2px 4px 2px 4px; color: #fff; font-size: 15pt; line-height: normal; } h2 { font-size: 10pt; } 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; } table { background-color: #ffffff; border-collapse: collapse; border: 0px 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: 0px solid #aca899; padding: 2px; } ul, ol { list-style-position: outside; padding-left: 20px; } li { margin-top: 2px; } </style> </head><body> <div style="width:660px; margin:0 auto;"> <h1>H2 Database Engine Cheat Sheet</h1> <div style="float:left; margin:5px; width:320px;"> <h2>Using H2</h2> <ul><li>H2 is open source, free to use and distribute. </li><li>Download the <a href="http://repo1.maven.org/maven2/com/h2database/h2/1.1.111/h2-1.1.111.jar">jar file</a>, <a href="http://www.h2database.com/h2-setup-2009-04-10.exe">Windows installer</a>, or <a href="http://www.h2database.com/h2-2009-04-10.zip">zip file</a>. </li><li>To start the H2 Console tool, double click the jar file, or run <code>java -jar h2*.jar</code>, <code>h2.bat</code> or <code>h2.sh</code>. </li><li>A new database is automatically created if it does not yet exist. </li><li>Closing the last connection closes a database. </li></ul> <h2>Database URLs</h2> <p> <b>Embedded</b><br /> <code>jdbc:h2:~/test</code> 'test' in the user home directory<br /> <code>jdbc:h2:/data/test</code> 'test' in the directory /data/<br /> <code>jdbc:h2:test</code> in the current(!) working dir<br /> </p><p> <b>In-Memory</b><br /> <code>jdbc:h2:mem:test</code> multiple connections in one process<br /> <code>jdbc:h2:mem:</code> unnamed private; one connection<br /> </p><p> <b>Server Mode</b><br /> <code>jdbc:h2:tcp://localhost/~/test</code> user home dir<br /> <code>jdbc:h2:tcp://localhost//data/test</code> absolute dir<br /> </p><p> <b>Settings</b><br /> <code>jdbc:h2:..;MODE=MySQL</code> compatibility (or HSQLDB,...)<br /> <code>jdbc:h2:..;TRACE_LEVEL_FILE=3</code> log to *.trace.db<br /> </p> <h2>Documentation</h2> Reference: <a href="http://www.h2database.com/html/grammar.html">SQL grammar</a>, <a href="http://www.h2database.com/html/functions.html">functions</a>, <a href="http://www.h2database.com/html/datatypes.html">data types</a>, <a href="http://www.h2database.com/html/datatypes.html">tools</a>. <br /> Features: <a href="http://www.h2database.com/html/grammar.html">fulltext search</a>, <a href="http://www.h2database.com/html/functions.html">database encryption</a>, <a href="http://www.h2database.com/html/datatypes.html">read-only databases</a> <a href="http://www.h2database.com/html/datatypes.html">(in a zip or jar file)</a>, <a href="http://www.h2database.com/html/datatypes.html">CSV files</a>, <br /> </div> <div style="float:right; padding:5px; width:320px;"> <h2>Using the JDBC API</h2> <pre> Class.forName("org.h2.Driver"); Connection conn = DriverManager. getConnection("jdbc:h2:~/test"); conn.close(); </pre> <h2>Connection Pool</h2> <pre> import org.h2.jdbcx.JdbcConnectionPool; DataSource cp = JdbcConnectionPool. create("jdbc:h2:~/test"); 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>Maven</h2> <pre> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${version}</version> </dependency> </pre> <h2>Hibernate</h2> hibernate.cfg.xml (or use the HSQLDialect): <pre> <property name="dialect"> org.hibernate.dialect.H2Dialect </property> </pre> <h2>TopLink and Glassfish</h2> datasource classname: org.h2.jdbcx.JdbcDataSource<br /> oracle.toplink.essentials.platform.database.H2Platform </div> </div> </body></html>