Android connection error java.lang.UnsupportedOperationException

951 views Asked by At

I have a problem on android studio when I try to make a connection to a mysql database

This is the code:

public Connection getMySqlConnection()
{
    /* Declare and initialize a sql Connection variable. */
    Connection ret = null;

    try
    {

        /* Register for jdbc driver class. */
        Class.forName("com.mysql.cj.jdbc.Driver");

        /* Create connection url. */
        String mysqlConnUrl = "jdbc:mysql://ip/ristorante?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";

        /* db user name. */
        String mysqlUserName = "guest";

        /* db password. */
        String mysqlPassword = "guestpass";

        /* Get the Connection object. */
        ret = DriverManager.getConnection(mysqlConnUrl , mysqlUserName , mysqlPassword );


        /* Get related meta data for this mysql server to verify db connect successfully.. */
        DatabaseMetaData dbmd = ret.getMetaData();

        String dbName = dbmd.getDatabaseProductName();

        String dbVersion = dbmd.getDatabaseProductVersion();

        String dbUrl = dbmd.getURL();

        String userName = dbmd.getUserName();

        String driverName = dbmd.getDriverName();

        System.out.println("Database Name is " + dbName);

        System.out.println("Database Version is " + dbVersion);

        System.out.println("Database Connection Url is " + dbUrl);

        System.out.println("Database User Name is " + userName);

        System.out.println("Database Driver Name is " + driverName);

    }catch(Exception ex)
    {
        ex.printStackTrace();
    }finally
    {
        return ret;
    }
}

This is the main error:

W/System.err: 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:195)
    at java.sql.DriverManager.getConnection(DriverManager.java:569)
    at java.sql.DriverManager.getConnection(DriverManager.java:219)
    at com.example.spara.restaurant.activity_home.getMySqlConnection(activity_home.java:163)
    at com.example.spara.restaurant.activity_home.onCreate(activity_home.java:80)

I tried the same code in eclipse java and it worked.

I had tried several times in different ways but without positive results.

so the database connection works. I just cannot find a solution, I hope you can help me.

1

There are 1 answers

1
MoWa On

I have a same problem as yours. When I find a guy meeting a same proble with me in StackOverFlow, I am so exciting. But When I find there is 0 answer, I feel so hopeless and sad. (ಥ_ಥ)
After painful tryings , I replace mysql-connector-java 8.x with 5.1.47. Then the confusing errors java.lang.UnsupportedOperationException disappear. ( But I don't know whether other bugs will appear. At least other bugs appear at sql connect stage and they look resolvable. I think it may be related with SDK version? ) hope I can help you :)