gpt4 book ai didi

java - Android 上的 DriverManager.getConnection() 出现 UnsupportedOperationException

转载 作者:行者123 更新时间:2023-12-01 18:32:40 25 4
gpt4 key购买 nike

我无法将 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 Connector/J 版本正在使用 Android 上不支持的 java.util.regex.Matcher 功能(可能 named groups )。解决方法是使用 5.1.x 版本的 MySQL Connector/J 而不是 8.0.x 版本。

但是,您不应该在 Android 应用程序中使用 JDBC。直接连接到数据库是不安全的,并且通常性能很差。正确的解决方案是编写 REST 服务(或其他形式的 Web 服务)来协调 Android 应用程序和数据库。

关于java - Android 上的 DriverManager.getConnection() 出现 UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60130091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com