# H2

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

Kotlin DataFrame supports reading from an [H2](https://www.h2database.com/html/main.html) database using JDBC.

API reference: [H2](https://kotlin.github.io/dataframe/api-docs/dataframe-jdbc/org.jetbrains.kotlinx.dataframe.io.db/-h2/index.html), [readSqlTable](https://kotlin.github.io/dataframe/api-docs/dataframe-jdbc/org.jetbrains.kotlinx.dataframe.io/read-sql-table.html)

Requires the [dataframe-jdbc module](modules.html#dataframe-jdbc), which is included by default in the general [dataframe artifact](modules.html#dataframe-general) and in [%use dataframe](setupkotlinnotebook.html#integrate-kotlin-dataframe) for Kotlin Notebook.

You’ll also need [the official H2 JDBC driver](https://www.h2database.com/html/main.html):

Gradle project:

```KOTLIN
dependencies {
    implementation("com.h2database:h2:$version")
}
```

Kotlin Notebook:

```KOTLIN
USE {
    dependencies("com.h2database:h2:$version")
}
```

The actual Maven Central driver version can be found [here](https://mvnrepository.com/artifact/com.h2database/h2).

## Read

A [DataFrame](dataframe.html) can be loaded from a database in several ways:

* a user can read data from a SQL table by a given name ([readSqlTable](readsqldatabases.html)),

* as a result of a user-defined SQL query ([readSqlQuery](readsqldatabases.html)), or

* from a given `ResultSet` ([readResultSet](readsqldatabases.html)).

It is also possible to load all data from non-system tables, each into a separate `DataFrame` ([readAllSqlTables](readsqldatabases.html)).

See [Read from SQL databases](readsqldatabases.html) for more details.

### H2 Compatibility Modes

When working with an H2 database, the library automatically detects the compatibility mode from the connection. If no `MODE` is specified in the JDBC URL, the default `Regular` mode is used. H2 supports the following compatibility modes: `MySQL`, `PostgreSQL`, `MSSQLServer`, `MariaDB`, and `Regular`.

#### Basic H2 connection (uses Regular mode by default)

```KOTLIN
val url = "jdbc:h2:mem:testDatabase"
val username = "sa"
val password = ""

val dbConfig = DbConnectionConfig(url, username, password)

val tableName = "Customer"

val df = DataFrame.readSqlTable(dbConfig, tableName)
```

#### H2 with PostgreSQL compatibility mode

```KOTLIN
val postgresUrl = "jdbc:h2:mem:testDatabase;MODE=PostgreSQL"
val username = "sa"
val password = ""

val postgresConfig = DbConnectionConfig(postgresUrl, username, password)

val tableName = "Customer"

val dfPostgres = DataFrame.readSqlTable(postgresConfig, tableName)
```

