Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/_assets/icon/kotlin-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions docs/connect/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
{material-regular}`javascript;2em`
::::

::::{grid-item-card} Kotlin
:link: connect-kotlin
:link-type: ref
:link-alt: Connect to CrateDB using Kotlin
:padding: 3
:text-align: center
:class-card: sd-pt-3
:class-body: sd-fs-1
:class-title: sd-fs-6
```{image} /_assets/icon/kotlin-logo.svg
:height: 50px
```
::::

::::{grid-item-card} PHP
:link: connect-php
:link-type: ref
Expand Down Expand Up @@ -184,6 +198,7 @@ application

java
javascript
kotlin/index
php
python
ruby
Expand Down
105 changes: 105 additions & 0 deletions docs/connect/kotlin/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
(connect-kotlin)=

# Kotlin

:::{include} /_include/links.md
:::

:::{div} sd-text-muted
Use JDBC to connect to CrateDB from Kotlin applications.
:::

:::{rubric} About
:::

:::
[JDBC] is a standard Java API that provides a common interface for accessing
databases in Java.
:::

:::{rubric} Driver options
:::

:::{div}
Like when using {ref}`connect-java`, you have two JDBC driver options:
The [PostgreSQL JDBC Driver] and the {ref}`crate-jdbc:index`.
PostgreSQL JDBC uses the `jdbc:postgresql://` protocol identifier,
while CrateDB JDBC uses `jdbc:crate://`.
:::

:::{rubric} Synopsis
:::

`example.kt`
```kotlin
import java.sql.DriverManager
import java.util.*

fun main() {

// Connect to database.
val jdbcUrl = "jdbc:postgresql://localhost:5432/doc?sslmode=disable"
val connection = DriverManager.getConnection(jdbcUrl, "crate", "crate")

// Invoke query.
val query = connection.prepareStatement("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3")
val result = query.executeQuery()

// Display results.
while (result.next()) {
val mountain = result.getString("mountain")
val height = result.getInt("height")
println("${mountain}: ${height}")
}

}
```
`build.gradle.kts`
```kotlin
plugins {
java
kotlin("jvm") version "2.2.20"
application
}

repositories {
mavenCentral()
}

dependencies {
runtimeOnly("org.postgresql:postgresql:42.7.8")
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

application {
mainClass = "ExampleKt"
}

kotlin {
sourceSets.main {
kotlin.srcDir(".")
}
}
```

:::{include} ../_cratedb.md
:::
```shell
gradle run
```

:::{rubric} CrateDB Cloud
:::

For connecting to CrateDB Cloud, use `sslmode=require`, and
replace username, password, and hostname with values matching
your environment.
```kotlin
val jdbcUrl = "jdbc:postgresql://testcluster.cratedb.net:5432/doc?sslmode=require"
val connection = DriverManager.getConnection(jdbcUrl, "admin", "password")
```