H2 Database Engine - A very fast, open source, JDBC API database with embedded and server modes, transaction support, multi-version concurrency, browser-based console application, encrypted databases, fulltext search, and pure Java implementation with small footprint
npx @tessl/cli install tessl/maven-com-h2database--h2@2.3.0H2 Database Engine is a very fast, open source, JDBC API database with embedded and server modes, transaction support, multi-version concurrency, browser-based console application, encrypted databases, fulltext search, and pure Java implementation with small footprint. H2 supports both in-memory and persistent databases, making it ideal for development, testing, embedded applications, and production systems requiring a lightweight yet powerful SQL database solution.
pom.xml:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.3.232</version>
</dependency>implementation 'com.h2database:h2:2.3.232'import org.h2.Driver;
import org.h2.jdbcx.JdbcDataSource;For JDBC usage:
import java.sql.*;For tools and utilities:
import org.h2.tools.*;import java.sql.*;
public class H2Example {
public static void main(String[] args) throws SQLException {
// Register H2 driver (optional in modern Java)
Class.forName("org.h2.Driver");
// Create in-memory database connection
Connection conn = DriverManager.getConnection(
"jdbc:h2:mem:testdb", "sa", "");
// Create table and insert data
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))");
stmt.execute("INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob')");
// Query data
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") +
", Name: " + rs.getString("name"));
}
// Close connection
conn.close();
}
}// File-based database (creates testdb.mv.db file)
Connection conn = DriverManager.getConnection(
"jdbc:h2:~/testdb", "sa", "password");import org.h2.jdbcx.JdbcDataSource;
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:testdb");
ds.setUser("sa");
ds.setPassword("");
Connection conn = ds.getConnection();H2 Database Engine is built around several key components:
Standard JDBC database connectivity with H2-specific enhancements for embedded and server modes. Supports connection pooling, XA transactions, and various authentication methods.
public class Driver implements java.sql.Driver {
public Connection connect(String url, Properties info) throws SQLException;
public boolean acceptsURL(String url) throws SQLException;
public static Driver load();
}public class JdbcDataSource implements DataSource, XADataSource, ConnectionPoolDataSource {
public Connection getConnection() throws SQLException;
public Connection getConnection(String username, String password) throws SQLException;
public void setURL(String url);
public void setUser(String user);
public void setPassword(String password);
}Comprehensive set of command-line tools for database administration, maintenance, and data management operations.
public class Server extends Tool implements Runnable {
public static void main(String... args);
public Server();
public Server(Service service, String... args);
}public class Script extends Tool {
public static void main(String... args);
}public class Backup extends Tool {
public static void main(String... args);
}High-performance key-value storage engine that can be used independently of the SQL database. Provides persistent maps with ACID transactions and MVCC.
public class MVStore implements AutoCloseable {
public static MVStore open(String fileName);
public <K, V> MVMap<K, V> openMap(String name);
public Set<String> getMapNames();
public void close();
}public class MVMap<K, V> {
public V put(K key, V value);
public V get(Object key);
public V remove(Object key);
public long size();
}Server components for deploying H2 as a standalone database server with support for multiple client connections, web-based administration, and PostgreSQL protocol compatibility.
public class TcpServer {
// TCP/IP database server implementation
}public class WebServer {
// HTTP server for H2 Console web interface
}public class PgServer {
// PostgreSQL protocol compatibility server
}Interfaces for extending H2 Database functionality with custom triggers, aggregate functions, table engines, and event listeners.
public interface Trigger {
int INSERT = 1;
int UPDATE = 2;
int DELETE = 4;
int SELECT = 8;
void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException;
default void init(Connection conn, String schemaName, String triggerName,
String tableName, boolean before, int type) throws SQLException;
}public interface AggregateFunction {
void add(Object value) throws SQLException;
Object getResult() throws SQLException;
int getType(int[] inputTypes) throws SQLException;
}public interface DatabaseEventListener extends EventListener {
default void exceptionThrown(SQLException e, String sql);
default void setProgress(int state, String name, long x, long max);
}jdbc:h2:mem:dbnamejdbc:h2:~/dbname or jdbc:h2:./dbnamejdbc:h2:~/secure;CIPHER=AESjdbc:h2:tcp://localhost/~/dbnamejdbc:h2:ssl://localhost/~/dbnamejdbc:h2:~/dbname;AUTO_SERVER=TRUEjdbc:h2:~/dbname;ACCESS_MODE_DATA=rH2 uses standard SQLException hierarchy with H2-specific error codes defined in ErrorCode class:
public class ErrorCode {
public static final int NO_DATA_AVAILABLE = 2000;
// ... numerous other error codes
}Common patterns:
try {
Connection conn = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "");
// database operations
} catch (SQLException e) {
System.err.println("Error Code: " + e.getErrorCode());
System.err.println("SQL State: " + e.getSQLState());
System.err.println("Message: " + e.getMessage());
}