|  | 
|  | 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