[英]UnsupportedOperationException with DriverManager.getConnection() on Android
我无法将 JDBC 连接到我的数据库,出现以下错误
Error : E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myprojectapplication, PID: 32686
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myprojectapplication/com.example.myprojectapplication.MainActivity}:
java.lang.UnsupportedOperationException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.UnsupportedOperationException
at java.util.regex.Matcher.group(Matcher.java:383)
at com.mysql.cj.conf.ConnectionUrlParser.isConnectionStringSupported(ConnectionUrlParser.java:152)
at com.mysql.cj.conf.ConnectionUrl.acceptsUrl(ConnectionUrl.java:258)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:187)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
at java.sql.DriverManager.getConnection(DriverManager.java:219)
at com.example.myprojectapplication.ObjectClass.JDBC.CONN(JDBC.java:19)
at com.example.myprojectapplication.MainActivity.onCreate(MainActivity.java:72)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
package com.example.myprojectapplication.ObjectClass;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBC {
public void CONN() {
Connection con = null;
try {
String url = "jdbc:mysql://localhost:3306/prog";
String username = "root";
String password = "mysql";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, username, password);
if (con != null) {
System.out
.println("Successfully connected to MySQL database test");
}
} catch (SQLException ex) {
System.out
.println("An error occurred while connecting MySQL databse");
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedOperationException exa){
exa.printStackTrace();
}
}
}
根据堆栈跟踪,您正在使用的 MySQL 连接器/J 版本正在使用java.util.regex.Matcher
功能不支持 Android(可能命名为组) 解决方法是使用 MySQL 连接器/J 的 5.1.x 版本,而不是 8.0.x 版本。
但是,您不应使用 Android 应用程序中的 JDBC。 直接连接到数据库是不安全的,并且通常性能很差。 正确的解决方案是编写 REST 服务(或其他形式的 Web 服务)来在 Android 应用程序和数据库之间进行调解。
我可能会迟到回答这个问题,但是在尝试了多个小时的不同解决方案之后,就像我要放弃 android 和 jconnector 一样,我尝试了下面的方法,它对我有用。
要使用来自 Android 应用程序的 jconnector 连接到运行 MySQL 版本 8 和/或 5.7 的 MySQL 远程服务器,此解决方案适用于我:
请注意,旧版本的 jconnector 比我在这里建议的版本不适用于远程 MySQL 8 服务器!
I tried to use mysql-connector-java-8.xxx.jar with " com.mysql.cj.jdbc.Driver
" but it did not work either on an Android application. 仅当您使用 java swing 或其他 java 应用程序并且再次“不在 android 上”时,它才会起作用!
android 的解决方案:
Class.forName("com.mysql.jdbc.Driver").newInstance();
可能意味着你配置中的msql驱动信息与你代码中的加载驱动不匹配。 The high version of the mysql driver registration, try to use this method: Class.forName("com.mysql.cj.jdbc.Driver");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.