You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用连接云MySQL报错:找不到javax.security.sasl.SaslClientFactory类

Android连接云MySQL异常排查建议

问题背景

使用真实设备连接云服务器上的MySQL数据库,通过phpMyAdmin可正常登录,但Android应用代码连接时抛出异常,以下是相关代码及异常信息。

相关代码

线程处理逻辑

private fun changeScrollView() {
    val thread = object : Thread(){
        override fun run(){
            try {
                Log.d("Thread", "Start loading")
                events = mySqlHelper.getEventsByDate(roomId, chosenDate).sortedBy { it.time }
                Log.d("Thread", "First loaded successfully")
                tasks = mySqlHelper.getTasksByDate(chosenDate, roomId).sortedBy { it.time }
                Log.d("Thread", "Loaded successfully")

                val handler = Handler(Looper.getMainLooper())
                handler.postDelayed({
                    createAllEventsAndTasks()
                }, 0)
            }catch (e: Exception){
                Log.e("Thread","Error: ${e.message}", e)
            }
        }
    }

    thread.start()
}

MySQLHelper连接代码

private val url = "jdbc:mysql://mysql.hosting.nic.ru:3306/h406497953_db"
private val username = "h406497953_mysql"
private val passwordUrl = "************************"

private fun getConnection(): Connection {
    Class.forName("com.mysql.cj.jdbc.Driver")
    return DriverManager.getConnection(url, username, passwordUrl)
}


fun getEventsByDate(roomId: Int, date :String) :List<Event>{
    val connection = getConnection()
    val query = "SELECT * FROM events WHERE idRoom =? AND data =?"
    val preparedStatement = connection.prepareStatement(query)
    preparedStatement.setInt(1,roomId)
    preparedStatement.setDate(2,java.sql.Date.valueOf(date))
    val resultSet = preparedStatement.executeQuery()
    val events = mutableListOf<Event>()
    while (resultSet.next()) {
        val event = Event(
            data = resultSet.getDate("data").toString(),
            time = resultSet.getString("time"),
            place = resultSet.getString("place"),
            event = resultSet.getString("event")
        )
        events.add(event)
    }
    connection.close()
    return events
}

Gradle依赖配置

implementation (libs.mysql.connector.java.v8022)

Manifest权限

<uses-permission android:name="android.permission.INTERNET" />

异常栈信息

Start loading
FATAL EXCEPTION: Thread-2
Process: com.example.helper1, PID: 15374
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/security/sasl/SaslClientFactory;
    at com.mysql.cj.protocol.a.authentication.AuthenticationLdapSaslClientPlugin.<clinit>(AuthenticationLdapSaslClientPlugin.java:59)
    at com.mysql.cj.protocol.a.NativeAuthenticationProvider.loadAuthenticationPlugins(NativeAuthenticationProvider.java:246)
    at com.mysql.cj.protocol.a.NativeAuthenticationProvider.proceedHandshakeWithPluggableAuthentication(NativeAuthenticationProvider.java:387)
    at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:174)
    at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1348)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:157)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) 
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
    at java.sql.DriverManager.getConnection(DriverManager.java:580)
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.security.sasl.SaslClientFactory" on path: DexPathList[[dex file "/data/data/com.example.helper1/code_cache/.overlay/base.apk/classes.dex", dex file "/data/data/com.example.helper1/code_cache/.overlay/base.apk/classes5.dex", zip file "/data/app/~~8xorLfwXJG9tw6gBIhW1aQ==/com.example.helper1-RzqhcYGm2Ltb7G8Bt70gpA==/base.apk"],nativeLibraryDirectories=[/data/app/~~8xorLfwXJG9tw6gBIhW1aQ==/com.example.helper1-RzqhcYGm2Ltb7G8Bt70gpA==/lib/arm, /system/lib, /system_ext/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 16 more

排查解决方向

  • 修改MySQL连接URL参数:Android系统不包含javax.security.sasl包,高版本MySQL Connector/J会尝试加载SASL相关认证插件,导致类找不到。在连接URL末尾添加参数强制使用原生密码认证,禁用SASL:

    jdbc:mysql://mysql.hosting.nic.ru:3306/h406497953_db?useSSL=false&authenticationPlugins=mysql_native_password
    

    若服务器要求SSL加密,可将useSSL设为true。

  • 降低MySQL Connector/J版本:8.0.19及以后的版本引入了SASL相关依赖,兼容性变差。可尝试降级到8.0.18或更早版本,修改Gradle依赖为对应版本即可。

  • 重构架构(推荐):直接在Android应用中用JDBC连接MySQL存在安全风险(数据库账号密码易暴露),且兼容性问题多。建议搭建后端API服务(如Java Web、PHP、Node.js),Android通过HTTP请求调用API间接操作数据库,这是移动应用与数据库交互的标准做法。

内容的提问来源于stack exchange,提问作者tumannick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:14:51