【问题标题】:External MySQL database in KotlinKotlin 中的外部 MySQL 数据库
【发布时间】: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


    【解决方案1】:

    Android 不支持开箱即用的 MySQL。访问数据库的“正常”方式是在其前面放置一个 Restful 服务器并使用 HTTPS 协议连接到 Restful 前端。

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

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

    或者你可以选择Firebase CloudStore。更好更简单的 NoSQL 数据库。

    如果您有任何疑问,请发表评论。

    【讨论】:

    • 终于有人了!感谢你的回答。我会尝试你的一些建议。之后我会接受你的回答。
    • 如果您需要任何帮助,只需将其评论下来
    • 好的,我尝试在 php 中创建一个“中间”组件,它似乎可以正常工作。我用 okhttp 来做这个。 O 在 pho 中创建了一个函数,该函数向我的 MySQL 数据库发出请求,并通过 HTTP 请求将其发送到 Kotlin。我认为这是最简单的方法。感谢您的帮助:)
    猜你喜欢
    • 2022-11-09
    • 2016-05-30
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多