这个问题在这里已经有答案了:
android上drivermanager.getconnection()不支持操作异常(2个答案)
11个月前关门了。
当我尝试连接到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)
我在EclipseJava中尝试了相同的代码,结果成功了。
我用不同的方法试了好几次,但都没有积极的结果。
所以数据库连接工作正常。我就是找不到解决办法,希望你能帮我。
1条答案
按热度按时间px9o7tmv1#
我和你有同样的问题。当我发现一个家伙在stackoverflow遇到同样的问题时,我很兴奋。但当我发现没有答案时,我感到绝望和悲伤(ಥ_ಥ)
经过痛苦的尝试,我用5.1.47替换mysql连接器Java8.x。然后混乱的错误java.lang.unsupportedoperationexception消失但我不知道是否会出现其他的虫子。至少在sqlconnect阶段还出现了其他bug,它们看起来是可以解决的。我想可能和sdk版本有关?)希望我能帮助你:)