Skip to content

Commit 04d83ea

Browse files
committed
Connect: Add Kotlin
1 parent c2721f4 commit 04d83ea

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

docs/_assets/icon/kotlin-logo.svg

Lines changed: 1 addition & 0 deletions
Loading

docs/connect/index.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
7373
{material-regular}`javascript;2em`
7474
::::
7575

76+
::::{grid-item-card} Kotlin
77+
:link: connect-kotlin
78+
:link-type: ref
79+
:link-alt: Connect to CrateDB using Kotlin
80+
:padding: 3
81+
:text-align: center
82+
:class-card: sd-pt-3
83+
:class-body: sd-fs-1
84+
:class-title: sd-fs-6
85+
```{image} /_assets/icon/kotlin-logo.svg
86+
:height: 50px
87+
```
88+
::::
89+
7690
::::{grid-item-card} PHP
7791
:link: connect-php
7892
:link-type: ref
@@ -184,6 +198,7 @@ application
184198
185199
java
186200
javascript
201+
kotlin/index
187202
php
188203
python
189204
ruby

docs/connect/kotlin/index.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
(connect-kotlin)=
2+
3+
# Kotlin
4+
5+
:::{include} /_include/links.md
6+
:::
7+
8+
:::{div} sd-text-muted
9+
Use JDBC to connect to CrateDB from Kotlin applications.
10+
:::
11+
12+
:::{rubric} About
13+
:::
14+
15+
:::
16+
[JDBC] is a standard Java API that provides a common interface for accessing
17+
databases in Java.
18+
:::
19+
20+
:::{rubric} Driver options
21+
:::
22+
23+
:::{div}
24+
Like when using {ref}`connect-java`, you have two JDBC driver options:
25+
The [PostgreSQL JDBC Driver] and the {ref}`crate-jdbc:index`.
26+
PostgreSQL JDBC uses the `jdbc:postgresql://` protocol identifier,
27+
while CrateDB JDBC uses `jdbc:crate://`.
28+
:::
29+
30+
:::{rubric} Synopsis
31+
:::
32+
33+
`example.kt`
34+
```kotlin
35+
import java.sql.DriverManager
36+
import java.util.*
37+
38+
fun main() {
39+
40+
// Connect to database.
41+
val jdbcUrl = "jdbc:postgresql://localhost:5432/doc?sslmode=disable"
42+
val connection = DriverManager.getConnection(jdbcUrl, "crate", "crate")
43+
44+
// Invoke query.
45+
val query = connection.prepareStatement("SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3")
46+
val result = query.executeQuery()
47+
48+
// Display results.
49+
while (result.next()) {
50+
val mountain = result.getString("mountain")
51+
val height = result.getInt("height")
52+
println("${mountain}: ${height}")
53+
}
54+
55+
}
56+
```
57+
`build.gradle.kts`
58+
```kotlin
59+
plugins {
60+
java
61+
kotlin("jvm") version "2.2.20"
62+
application
63+
}
64+
65+
repositories {
66+
mavenCentral()
67+
}
68+
69+
dependencies {
70+
runtimeOnly("org.postgresql:postgresql:42.7.8")
71+
}
72+
73+
java {
74+
toolchain {
75+
languageVersion.set(JavaLanguageVersion.of(11))
76+
}
77+
}
78+
79+
application {
80+
mainClass = "ExampleKt"
81+
}
82+
83+
kotlin {
84+
sourceSets.main {
85+
kotlin.srcDir(".")
86+
}
87+
}
88+
```
89+
90+
:::{include} ../_cratedb.md
91+
:::
92+
```shell
93+
gradle run
94+
```
95+
96+
:::{rubric} CrateDB Cloud
97+
:::
98+
99+
For connecting to CrateDB Cloud, use `sslmode=require`, and
100+
replace username, password, and hostname with values matching
101+
your environment.
102+
```kotlin
103+
val jdbcUrl = "jdbc:postgresql://testcluster.cratedb.net:5432/doc?sslmode=require"
104+
val connection = DriverManager.getConnection(jdbcUrl, "admin", "password")
105+
```

0 commit comments

Comments
 (0)