【发布时间】:2019-07-04 20:23:21
【问题描述】:
我正在制作一个应该有一个外部 MySQL 数据库的 Kotlin 应用程序。如何设置它以获得所需的结果?
应用程序应该从外部 MySQL 数据库中以字符串形式获取结果。我已经尝试过使用这个link,但似乎有问题。
我使用这个代码:
class DBConnection {
internal var conn: Connection? = null
internal var username = "some username"
internal var password = "some password"
fun executeMySQLQuery() {
var stmt: Statement? = null
var resultset: ResultSet? = null
try {
stmt = conn!!.createStatement()
resultset = stmt!!.executeQuery("SHOW DATABASES;")
if (stmt.execute("SHOW DATABASES;")) {
resultset = stmt.resultSet
}
while (resultset!!.next()) {
println(resultset.getString("Database"))
}
} catch (ex: SQLException) {
// handle any errors
ex.printStackTrace()
} finally {
// release resources
if (resultset != null) {
try {
resultset.close()
} catch (sqlEx: SQLException) {
}
resultset = null
}
if (stmt != null) {
try {
stmt.close()
} catch (sqlEx: SQLException) {
}
stmt = null
}
if (conn != null) {
try {
conn!!.close()
} catch (sqlEx: SQLException) {
}
conn = null
}
}
}
fun getConnection() {
val connectionProps = Properties()
connectionProps.put("user", username)
connectionProps.put("password", password)
try {
Class.forName("com.mysql.jdbc.Driver").newInstance()
conn = DriverManager.getConnection(
"jdbc:" + "mysql" + "://" +
"remotemysql.com" +
":" + "3306" + "/" +
"",
connectionProps)
} catch (ex: SQLException) {
// handle any errors
ex.printStackTrace()
} catch (ex: Exception) {
// handle any errors
ex.printStackTrace()
}
}
}
在一个主文件中我使用这个:
val submit: Button = findViewById(R.id.submitBtn)
submit.setOnClickListener{
val dbConnection: DBConnection
dbConnection = DBConnection()
dbConnection.getConnection()
dbConnection.executeMySQLQuery()
}
现在我只需要从数据库中获取结果。当我使用提到的代码时,我得到了这个错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sk.letsdream, PID: 18521
kotlin.KotlinNullPointerException
at sk.letsdream.dbMethods.DBConnection.executeMySQLQuery(DBConnection.kt:19)
at sk.letsdream.DochadzkaActivity$onCreate$2.onClick(DochadzkaActivity.kt:86)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3400(View.java:801)
at android.view.View$PerformClick.run(View.java:27301)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
编辑: 添加 mysql-connector-java-8.0.16 后,它给了我一个错误。 SDK 应该是 26。但是在将 SDK 更改为 26 后,它给了我这种类型的错误:
java.lang.BootstrapMethodError: Exception from call site #39 bootstrap method
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.<clinit>(AbandonedConnectionCleanupThread.java:58)
其中 58. 行是:
Class.forName("com.mysql.jdbc.Driver").newInstance()
当我们有旧版本的 mysql 连接器 mysql-connector-java-5.1.47 时,它抛出了这个错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
在这行代码上:
var conn = DriverManager.getConnection("jdbc:mysql://remotemysql.com:3306/My_DB_Name", connectionProps)
【问题讨论】:
标签: android mysql database kotlin