Created
December 9, 2024 10:43
-
-
Save iKunalChhabra/e9af3bbaeeeac7cfe6047b03c85cc13f to your computer and use it in GitHub Desktop.
Kotlin connect to Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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