Kotlin中的外部MySQL数据库

时间:2019-07-04 20:23:21

标签: android mysql database kotlin

我正在制作应该具有外部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)

1 个答案:

答案 0 :(得分:2)

Android不支持现成的MySQL。访问数据库的“常规”方法是将一个Restful服务器放在它的前面,并使用HTTPS协议连接到Restful前端。

要在您的应用程序中访问MySQL。您可以尝试使用Restful API。使用Restful API,您可以继续进行与MySQL相同的操作。与API Retrofit进行交互是更好的库。

本地数据库SQLite之一。但是我不建议使用它。

或者您可以选择Firebase CloudStore。更好,更轻松的NoSQL数据库。

如果有任何疑问,只需将其注释掉。