https://kotlinlang.org logo
p

Patrick Doering

01/18/2021, 5:20 AM
Hi all, I am using Kotlin with Exposed which is working. Can I also use it with JVM? Does anybody have a running example for database connection and query database?
c

crummy

01/18/2021, 7:01 AM
Kotlin runs on the JVM, and Exposed works on Kotlin on the JVM. Or do you mean Java?
p

Patrick Doering

01/18/2021, 7:02 AM
I mean Java
Really no Java example available?
e

Endre Deak

01/18/2021, 4:57 PM
I don’t think it should be complicated. I guess there are a bunch of “Hello world”-like examples written in Kotlin, and Kotlin can be called from Java so you should be able to write something in Java. However, I started to write a super simple table in Java and after 5 minutes I asked myself “What’s the point?“:
Copy code
// kotlin, single file
object HealthCheckTable : Table() {
    val content = varchar("content", 255)

    override val primaryKey: PrimaryKey by lazy { PrimaryKey(content) }

    fun toObject(row: ResultRow): HealthCheckResponse =
        HealthCheckResponse(row[content])
}

data class HealthCheckResponse(val value: String)
Copy code
// java, would be multiple files but I gave up
public class HealthCheckTableJava extends Table {
    private Column<String> content = varchar("content", 255, null);

    @Nullable
    @Override
    public PrimaryKey getPrimaryKey() {
        return new PrimaryKey(Collections.singletonList(content));
    }
}
so unless you have a very strong reason not to write this code in Kotlin, I’d encourage you to do it. If you are tied to Java, maybe Exposed is not the best option for you… 🤷‍♂️
p

Patrick Doering

01/18/2021, 5:53 PM
Thank you for the reply I will give it a try. Maybe Exposed is not the best. I will check this.
4 Views