All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ginloader.bukkit-api.1.13.0.source-code.MySQL.kt Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package pluginloader.api

import kotlinx.serialization.Serializable
import org.bukkit.configuration.ConfigurationSection
import java.sql.Connection
import java.sql.DriverManager

@Serializable
class Connector(private val host: String, private val port: Int, private val database: String, private val user: String, private val password: String) {
    @kotlinx.serialization.Transient
    private val url = "jdbc:mysql://$host:$port/$database"
    @kotlinx.serialization.Transient
    private var connection: Connection? = null

    constructor(): this("localhost", 3306, "database", "user", "password")

    fun connection(): Connection {
        if(connection == null || connection!!.isClosed) connection = connect()
        return connection!!
    }

    fun close(){
        if(connection == null)return
        if(connection!!.isClosed)return
        connection!!.close()
    }

    private fun connect() = DriverManager.getConnection(url, user, password)

    companion object{
        fun parse(section: ConfigurationSection) =
                Connector(section.getString("host"),
                section.getInt("port"), section.getString("database"),
                section.getString("user"), section.getString("password"))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy