ANDROID:java.lang.NoClassDefFoundError:解析失败:Lcom/mysql/cj/MysqlType

Mat*_*ira 0 java mysql android mysql-connector

我正在尝试将我的项目与 mysql 数据库连接,但我在驱动程序中收到此错误:

\n
E/AndroidRuntime: FATAL EXCEPTION: main\n        Process: com.example.catalyst, PID: 12531\n        java.lang.NoClassDefFoundError: Failed resolution of: Lcom/mysql/cj/MysqlType;\n            at com.mysql.cj.protocol.a.NativeProtocol.findMysqlType(NativeProtocol.java:1466)\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.unpackField(ColumnDefinitionReader.java:134)\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:77)\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:40)\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1588)\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:68)\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:48)\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1601)\n            at com.mysql.cj.protocol.a.NativeProtocol.readAllResults(NativeProtocol.java:1655)\n            at com.mysql.cj.NativeSession.loadServerVariables(NativeSession.java:765)\n            at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1303)\n            at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:964)\n            at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)\n            at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)\n            at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)\n            at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)\n            at java.sql.DriverManager.getConnection(DriverManager.java:569)\n            at java.sql.DriverManager.getConnection(DriverManager.java:219)\n            at modelo.Conexion.conectar(Conexion.java:24)\n            at modelo.EnviarIngresos.ejecutarServicio(EnviarIngresos.java:27)\n            at GUIs.QRscanner.enviarIngreso(QRscanner.java:75)\n            at GUIs.QRscanner.onClick(QRscanner.java:42)\n            at android.view.View.performClick(View.java:6297)\n            at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)\n            at android.view.View$PerformClick.run(View.java:24797)\n            at android.os.Handler.handleCallback(Handler.java:790)\n            at android.os.Handler.dispatchMessage(Handler.java:99)\n            at android.os.Looper.loop(Looper.java:164)\n            at android.app.ActivityThread.main(ActivityThread.java:6626)\n            at java.lang.reflect.Method.invoke(Native Method)\n            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)\n            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)\n         Caused by: java.lang.ClassNotFoundException: com.mysql.cj.MysqlType\n            at java.lang.VMClassLoader.findLoadedClass(Native Method)\n            at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)\n            at java.lang.ClassLoader.loadClass(ClassLoader.java:363)\n            at java.lang.ClassLoader.loadClass(ClassLoader.java:312)\n            at com.mysql.cj.protocol.a.NativeProtocol.findMysqlType(NativeProtocol.java:1466)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.unpackField(ColumnDefinitionReader.java:134)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:77)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:40)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1588)\xc2\xa0\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:68)\xc2\xa0\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:48)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1601)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.readAllResults(NativeProtocol.java:1655)\xc2\xa0\n            at com.mysql.cj.NativeSession.loadServerVariables(NativeSession.java:765)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1303)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:964)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)\xc2\xa0\n            at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)\xc2\xa0\n            at java.sql.DriverManager.getConnection(DriverManager.java:569)\xc2\xa0\n            at java.sql.DriverManager.getConnection(DriverManager.java:219)\xc2\xa0\n            at modelo.Conexion.conectar(Conexion.java:24)\xc2\xa0\n            at modelo.EnviarIngresos.ejecutarServicio(EnviarIngresos.java:27)\xc2\xa0\n            at GUIs.QRscanner.enviarIngreso(QRscanner.java:75)\xc2\xa0\n            at GUIs.QRscanner.onClick(QRscanner.java:42)\xc2\xa0\n            at android.view.View.performClick(View.java:6297)\xc2\xa0\n            at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)\xc2\xa0\n            at android.view.View$PerformClick.run(View.java:24797)\xc2\xa0\n            at android.os.Handler.handleCallback(Handler.java:790)\xc2\xa0\n            at android.os.Handler.dispatchMessage(Handler.java:99)\xc2\xa0\n            at android.os.Looper.loop(Looper.java:164)\xc2\xa0\n            at android.app.ActivityThread.main(ActivityThread.java:6626)\xc2\xa0\n            at java.lang.reflect.Method.invoke(Native Method)\xc2\xa0\n            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)\xc2\xa0\n            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)\xc2\xa0\n         Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/sql/SQLType;\n            at com.mysql.cj.protocol.a.NativeProtocol.findMysqlType(NativeProtocol.java:1466)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.unpackField(ColumnDefinitionReader.java:134)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:77)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:40)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1588)\xc2\xa0\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:68)\xc2\xa0\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:48)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1601)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.readAllResults(NativeProtocol.java:1655)\xc2\xa0\n            at com.mysql.cj.NativeSession.loadServerVariables(NativeSession.java:765)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1303)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:964)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)\xc2\xa0\n            at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)\xc2\xa0\n            at java.sql.DriverManager.getConnection(DriverManager.java:569)\xc2\xa0\n            at java.sql.DriverManager.getConnection(DriverManager.java:219)\xc2\xa0\n            at modelo.Conexion.conectar(Conexion.java:24)\xc2\xa0\n            at modelo.EnviarIngresos.ejecutarServicio(EnviarIngresos.java:27)\xc2\xa0\n            at GUIs.QRscanner.enviarIngreso(QRscanner.java:75)\xc2\xa0\n            at GUIs.QRscanner.onClick(QRscanner.java:42)\xc2\xa0\n            at android.view.View.performClick(View.java:6297)\xc2\xa0\n            at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)\xc2\xa0\n            at android.view.View$PerformClick.run(View.java:24797)\xc2\xa0\n            at android.os.Handler.handleCallback(Handler.java:790)\xc2\xa0\n            at android.os.Handler.dispatchMessage(Handler.java:99)\xc2\xa0\n            at android.os.Looper.loop(Looper.java:164)\xc2\xa0\n            at android.app.ActivityThread.main(ActivityThread.java:6626)\xc2\xa0\n            at java.lang.reflect.Method.invoke(Native Method)\xc2\xa0\n            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)\xc2\xa0\n            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)\xc2\xa0\n         Caused by: java.lang.ClassNotFoundException: Didn't find class "java.sql.SQLType" on path: DexPathList[[zip file "/data/app/com.example.catalyst-r_unhHUpn3kJ_xqUGFYc4w==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.catalyst-fumFeudS3H2K2UqSBclr7A==/lib/arm, /system/lib, /system/vendor/lib]]\n            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)\n            at java.lang.ClassLoader.loadClass(ClassLoader.java:379)\n            at java.lang.ClassLoader.loadClass(ClassLoader.java:312)\n            at com.mysql.cj.protocol.a.NativeProtocol.findMysqlType(NativeProtocol.java:1466)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.unpackField(ColumnDefinitionReader.java:134)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:77)\xc2\xa0\n            at com.mysql.cj.protocol.a.ColumnDefinitionReader.read(ColumnDefinitionReader.java:40)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1588)\xc2\xa0\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:68)\xc2\xa0\n            at com.mysql.cj.protocol.a.TextResultsetReader.read(TextResultsetReader.java:48)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.read(NativeProtocol.java:1601)\xc2\xa0\n            at com.mysql.cj.protocol.a.NativeProtocol.readAllResults(NativeProtocol.java:1655)\xc2\xa0\n            at com.mysql.cj.NativeSession.loadServerVariables(NativeSession.java:765)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1303)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:964)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)\xc2\xa0\n            at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)\xc2\xa0\n            at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)\xc2\xa0\n            at java.sql.DriverManager.getConnection(DriverManager.java:569)\xc2\xa0\n            at java.sql.DriverManager.getConnection(DriverManager.java:219)\xc2\xa0\n            at modelo.Conexion.conectar(Conexion.java:24)\xc2\xa0\n            at modelo.EnviarIngresos.ejecutarServicio(EnviarIngresos.java:27)\xc2\xa0\n            at GUIs.QRscanner.enviarIngreso(QRscanner.java:75)\xc2\xa0\n            at GUIs.QRscanner.onClick(QRscanner.java:42)\xc2\xa0\n            at android.view.View.performClick(View.java:6297)\xc2\xa0\n            at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)\xc2\xa0\n            at android.view.View$PerformClick.run(View.java:24797)\xc2\xa0\n            at android.os.Handler.handleCallback(Handler.java:790)\xc2\xa0\n            at android.os.Handler.dispatchMessage(Handler.java:99)\xc2\xa0\n            at android.os.Looper.loop(Looper.java:164)\xc2\xa0\n            at android.app.ActivityThread.main(ActivityThread.java:6626)\xc2\xa0\n            at java.lang.reflect.Method.invoke(Native Method)\xc2\xa0\n            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)\xc2\xa0\n            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)\n
Run Code Online (Sandbox Code Playgroud)\n

\xc2\xa0这些是我的依赖项(gradle):

\n
dependencies {\n    implementation 'androidx.appcompat:appcompat:1.3.0'\n    implementation 'com.google.android.material:material:1.4.0'\n    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'\n    implementation 'com.google.android.gms:play-services-maps:17.0.1'\n\n    implementation files('libs\\\\UMLDesignTool.jar')\n    implementation files('libs/mysql-connector-java-8.0.25.jar')\n    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0'\n    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.4.0'\n    testImplementation 'org.junit.platform:junit-platform-launcher:1.0.0'\n    androidTestImplementation 'androidx.test.ext:junit:1.1.3'\n    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'\n    implementation 'com.journeyapps:zxing-android-embedded:3.4.0'\n    androidTestImplementation 'org.junit.jupiter:junit-jupiter'\n    androidTestImplementation 'org.testng:testng:6.9.6'\n    implementation 'com.android.volley:volley:1.2.0'\n\n} \n
Run Code Online (Sandbox Code Playgroud)\n

这是我进行连接的项目的类:

\n
import android.os.StrictMode;\n\nimport java.sql.Connection;\nimport java.sql.DriverManager;\n\npublic class Conexion {\n    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";\n    private static final String DB_URL = "jdbc:mysql://localhost:3306/databasename";\n    private static final String USER = "root";\n    private static final String PASS = "";\n\n\n    public Connection conectar(){\n\n        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();\n        StrictMode.setThreadPolicy(policy);\n\n        Connection conexion = null;\n\n        try {\n            Class.forName(JDBC_DRIVER).newInstance();\n            conexion= DriverManager.getConnection(DB_URL, USER, PASS);\n        } catch (Exception ex) {\n            ex.printStackTrace();\n        }\n\n        return conexion;\n    }\n    \n}\n
Run Code Online (Sandbox Code Playgroud)\n

该错误表明我没有驱动程序,但我有,我不知道该怎么办,也找不到任何与我有相同错误的帖子。

\n

Mar*_*eel 7

您无法在 Android 上使用最新版本的 MySQL Connector/J,因为它使用 Android 上不可用的类型和功能。这里的具体原因是类型com.mysql.cj.MysqlType实现java.sql.SQLType(Java 8 / JDBC 4.2中引入),并且通过错误判断该类型在Android中不存在。过去,我还看到过与在正则表达式中使用命名组相关的错误,Android 也不支持(或曾经)支持这些错误。

一般来说,您不应该在 Android 上使用 JDBC,最好使用 REST API 在 Android 应用程序和数据库之间进行协调。但是,如果您确实想从 Android 使用 MySQL,则必须使用 MySQL Connector/J 5.1.x 而不是 8.0.x。