android - Kotlin 中的外部 MySQL 数据库

标签 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)

最佳答案

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

用于在您的应用程序中访问 MySQL。你可以试试 Restful API .使用 Restful API,您可以继续执行与 MySQL 相同的操作。用于与 API 交互 Retrofit是更好的图书馆。

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

或者您可以使用 Firebase CloudStore .更好、更简单的 NoSQL 数据库。

如果您有任何疑问,请在评论下留言。

关于android - Kotlin 中的外部 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56893894/

相关文章:

android - Kotlin 内部类无法发起 Session 对象

Android EditText 保持提示可见和右侧

mysql - 是否可以使用 MySQL 'OR' 查询设置首选项?

php - 使用 Redis 的大规模策略的新闻提要

sql - MySQL : how to load data with fixed-row format into user variables

java - 使用 Kivy 或 CLI 获取我的位置?

android - 我在哪里可以找到 Mac 中的 local.properties 文件?

python - Windows 命令提示符在 Python 中不起作用

mysql - 如何通过另一个表中的相似数据更新sql中的列

mysql - 在 MySQL 中查找部分重复项(最后 6 个字符相同的键)