Skip to content

Instantly share code, notes, and snippets.

@iKunalChhabra
Created December 9, 2024 10:43
Show Gist options
  • Save iKunalChhabra/e9af3bbaeeeac7cfe6047b03c85cc13f to your computer and use it in GitHub Desktop.
Save iKunalChhabra/e9af3bbaeeeac7cfe6047b03c85cc13f to your computer and use it in GitHub Desktop.
Kotlin connect to Postgres
package org.example
import java.sql.DriverManager
fun main() {
val rows: List<Map<String, Any?>>
DriverManager.getConnection("jdbc:postgresql://localhost:5432/myapp", "myapp", "password").use { connection ->
connection.createStatement().use { stmt ->
stmt.executeQuery("select * from salesforce.account").use { rs ->
rows = generateSequence {
if (rs.next()) {
(1..rs.metaData.columnCount).associate { index ->
rs.metaData.getColumnName(index) to rs.getObject(index)
}
} else null
}.toList()
}
}
}
rows.forEach { println(it) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment