How to include JDBC into Kotlin project?

I am trying to use database on Kotlin but I have got some problems. I’ve imported Java SQL lib and I found an example for trying.

The line that I get error:
connection = DriverManager.getConnection("jdbc:sqlite: C:/sql/database.db")

Error:

java.sql.SQLException: No suitable driver found for jdbc:sqlite

How can I import the jdbc:sqlite?

JDBC is just an API. In order to actually use it, you need to include a driver for the specific DBMS that you are using - in your case SQLite - as a runtime dependency. An SQLite JDBC driver is available as Maven artifact org.xerial:sqlite-jdbc.

Thanks but I don’t use Maven. I use Gradle.

Maven artifacts can be used as Gradle dependencies quite easily, in fact Gradle mainly depends on using Maven artifacts. Go here to simply see how to add that artifact to your gradle projecct, but the TL;DR of it is this: implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.34.0'

Actually it doesn’t work.

build.gradle.kts:27:25: Expecting an element

By the way can you explain how to use Maven artifacts is used as Gradle dependicies?

1 Like

oh you’re using kts that’s why. The instructions on that are for the “default” (for lack of a better word) gradle language of Groovy (i.e. which is just build.gradle without the kts). For kts, use implementation(group = "org.xerial", name = "sqlite-jdbc", version: "3.34.0")

1 Like

You don’t actually need to declare it as an implementation dependency. runtimeOnly is enough:

runtimeOnly("org.xerial:sqlite-jdbc:3.34.0")
2 Likes

Thanks to you. @kyay10

1 Like