'UnsupportedOperationException with DriverManager.getConnection() on Android
I am unable to connect JDBC to my database, getting following error
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();
}
}
}
Solution 1:[1]
I might be late to answer this, but after many hours of trying different solutions just as I was going to give up on android and jconnector I tried bellow and it worked for me.
To use jconnector from Android application to connect to a MySQL remote server that runs MySQL version 8 and/or 5.7 this solution worked for me:
Note that older versions of jconnector than the one I am suggesting here will not work with a remote MySQL 8 server!
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. it will only work if you use java swing or other java applications and again "not on android"!
SOLUTION for android:
- Download /or import mysql-connector-java-5.1.49.jar as your connector jar file and replace it with your connector class! (note that versions bellow this will/may not work!)
- Use this connector class:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Solution 2:[2]
Probably means that the msql driver information in your configuration does not match the load driver in your code. The high version of the mysql driver registration, try to use this method: Class.forName("com.mysql.cj.jdbc.Driver");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stacker |
| Solution 2 |
