diff --git a/docs/_assets/icon/kotlin-logo.svg b/docs/_assets/icon/kotlin-logo.svg new file mode 100644 index 00000000..511a08f6 --- /dev/null +++ b/docs/_assets/icon/kotlin-logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/docs/connect/index.md b/docs/connect/index.md index ffe0cae1..a03765a2 100644 --- a/docs/connect/index.md +++ b/docs/connect/index.md @@ -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 @@ -184,6 +198,7 @@ application java javascript +kotlin/index php python ruby diff --git a/docs/connect/kotlin/index.md b/docs/connect/kotlin/index.md new file mode 100644 index 00000000..47698d3f --- /dev/null +++ b/docs/connect/kotlin/index.md @@ -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") +```