LoginSignup
4
4

More than 3 years have passed since last update.

Kotlin で MariaDB 10.4 に接続する

Last updated at Posted at 2019-06-10

最近人気の Kotlin で MariaDB Connector/J (JDBCドライバ)を用いて MariaDB 10.4 に接続してみました。

Java to Kotlin conversion を用いて,以前に書いた記事の Java サンプルコードを Kotlin に変換し,若干手作業で修正を行いました。

MariaDB Connector/J で MariaDB 10.4 に接続する

サンプルコード

src/main/kotlin/jdbc_sample/App.kt
import java.sql.*
import org.mariadb.jdbc.internal.util.constant.Version

object jdbc_sample {
    @JvmStatic fun main(args:Array<String>) {
        var conn: Connection? = null
        var stmt: Statement? = null
        var resultSet: ResultSet
        try {
            Class.forName("org.mariadb.jdbc.Driver")
            println("MariaDB Connector/J: " + Version.version + "\n")
            print("Connecting to DB...")
            conn = DriverManager.getConnection(
                    "jdbc:mariadb://192.168.2.104/mysql",
                    "db_user", "db_passwd")
            println("done.")
            stmt = conn.createStatement()
            resultSet = stmt.executeQuery("SELECT user, host FROM mysql.user")
            println("\nList of MariaDB users:")
            while (resultSet.next()) {
                var user = resultSet.getString(1)
                var host = resultSet.getString(2)
                println(user + "@'" + host + "'")
            }
        }
        catch (ex: SQLException) {
            ex.printStackTrace()
        }
        catch (ex: Exception) {
            ex.printStackTrace()
        }
        finally {
            try {
                if (stmt != null) {
                    conn?.close()
                }
            }
            catch (ex:SQLException) {}
            try {
                if (conn != null) {
                    conn.close()
                }
            }
            catch (ex: SQLException) {
                ex.printStackTrace()
            }
        }
    }
}

build.gradle.kts

今回 Gradle を使ってビルド,実行しました。
最初に以下のコマンドを実行します。

gradle init --type kotlin-application

以下の build.gradle.kts を以下のように修正しました。

build.gradle.kts

plugins {
    id("org.jetbrains.kotlin.jvm").version("1.3.21")
    application
}
repositories {
    jcenter()
}
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.mariadb.jdbc:mariadb-java-client:2.4.4")
}
application {
    mainClassName = "jdbc_sample"
}

MariaDB Connector/J は 2019-10-06 時点で最新版の 2.4.4 を用いました。

ビルド

まずは gradle build でビルドだけ行ってみます。

C:\Users\foo\kotlin\jdbc_sample>gradle build

BUILD SUCCESSFUL in 1s
6 actionable tasks: 5 executed, 1 up-to-date

実行結果

gradle run でサンプルプログラムを実行します。

C:\Users\foo\kotlin\jdbc_sample>gradle run

> Task :run
MariaDB Connector/J: 2.4.4

Connecting to DB... done.

List of MariaDB users:
db_user@'%'
mysql@'localhost'
root@'localhost'

BUILD SUCCESSFUL in 2s
2 actionable tasks: 1 executed, 1 up-to-date

問題ないようです。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4