ginloader.bukkit-api.1.13.0.source-code.MySQL.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bukkit-api Show documentation
Show all versions of bukkit-api Show documentation
Bukkit API for runtime kotlin plugin loader
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"))
}
}