Android连接错误java.lang.UnsupportedOperationException

时间:2018-11-03 23:59:20

标签: java android mysql connection unsupportedoperation

当我尝试建立与mysql数据库的连接时,我在android studio上遇到问题

这是代码:

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;
    }
}

这是主要错误:

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)

我在eclipse java中尝试了相同的代码,并且有效...

我以不同的方式尝试了几次,但没有取得积极的结果。

因此数据库连接有效。 我只是找不到解决方案,希望您能为我提供帮助。

1 个答案:

答案 0 :(得分:1)

我和你有同样的问题。当我在StackOverFlow中发现一个遇到与我同样的问题的人时,我感到非常兴奋。但是,当我发现答案为0时,我感到非常绝望和悲伤。 (ಥ_ಥ)
经过痛苦的尝试,我将MySQL-connector-java 8.x替换为5.1.47 。然后,混乱的错误 java.lang.UnsupportedOperationException 消失了。 (但我不知道是否会出现其他错误。至少其他错误会在sql connect阶段出现,并且看起来可以解决。我认为这可能与SDK版本有关?)希望可以为您提供帮助:)