无法从Android应用程序连接到数据库,出现“java.sql.SQLException:没有找到合适的驱动程序--“url链接”

nbnkbykc  于 2023-05-12  发布在  Android
关注(0)|答案(1)|浏览(215)
url = "jdbc:postgres://cvzsfhiy:**********.db.elephantsql.com/cvzsfhiy"
    user = "cvzsfhiy"
    password = "************"

    var conn1: Connection? = null
    var conn2: Connection? = null
    var conn3: Connection? = null

    button.setOnClickListener {
        try {
            Class.forName("org.postgresql.Driver")

            conn1 = DriverManager.getConnection(url, user, password)
            if (conn1 != null) {
                println("Connected to the database test1")
                tv.text = "Connection1"
            }

            // connect way #2
            val url2 = "jdbc:postgres://cvzsfhiy:*********?user=cvzsfhiy&password=***********"
            conn2 = DriverManager.getConnection(url2)
            if (conn2 != null) {
                println("Connected to the database test2")
                tv.text = "Connection2"
            }

            // connect way #3
            val url3 = "jdbc:postgres://cvzsfhiy:************y.db.elephantsql.com/cvzsfhiy"
            val info = Properties()
            info["user"] = "cvzsfhiy"
            info["password"] = "***************"
            conn3 = DriverManager.getConnection(url3, info)
            if (conn3 != null) {
                println("Connected to the database test3")
                tv.text = "Connection3"
            }
        } catch (ex: SQLException) {
            tv.text = "An error occurred. Maybe user/password is invalid"
            ex.printStackTrace()
        } catch (e: ClassNotFoundException) {
            tv.text = "Class Not Found"
            e.printStackTrace()
        }
    }
}

This is my instance for the elephant sql我得到SQLException错误,无法连接到数据库从Android应用程序给出“java.sql.SQLException:没有找到合适的驱动程序--“url链接”

yruzcnhs

yruzcnhs1#

示例代码中的url拼写错误(jdbc:postgres:部分)。
请参阅PostgreSQL documentation以获得正确的拼写。

相关问题