使用android studio3.2.0连接本机mysql出错

android studio版本3.2.0
SDK版本28
mysql版本8.0.12
jar(也为8.0.12版的jar)以引入,依赖也添加了
连接地址为10.0.2.2

源码如下
package com.example.skyline.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 反复尝试连接,直到连接成功后退出循环
while (!Thread.interrupted()) {
try {
Thread.sleep(100); // 每隔0.1秒尝试连接
} catch (InterruptedException e) {
Log.e("链接数据库", e.toString());
}
// 2.设置好IP/端口/数据库名/用户名/密码等必要的连接信息
String url="jdbc:mysql://10.0.2.2:3306/AndroidMidterm?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true";
String user = "user";
String password = "password";

// 3.连接JDBC
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url,user,password);
Log.i("链接数据库", "远程连接成功!");
conn.close();
return;
} catch (SQLException e) {
Log.e("链接数据库", "远程连接失败!");
}
catch (ClassNotFoundException e){

}
}
}
});
thread.start();
//applypermission();
setContentView(R.layout.activity_main);
}
}

报错信息如下
2018-11-06 00:35:49.518 4691-4709/com.example.skyline.test E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.skyline.test, PID: 4691
java.lang.BootstrapMethodError: Exception from call site #14 bootstrap method
at com.mysql.cj.conf.ConnectionUrl.buildConnectionStringCacheKey(ConnectionUrl.java:247)
at com.mysql.cj.conf.ConnectionUrl.getConnectionUrlInstance(ConnectionUrl.java:186)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:204)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
at java.sql.DriverManager.getConnection(DriverManager.java:219)
at com.example.skyline.test.MainActivity$1.run(MainActivity.java:35)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at com.mysql.cj.conf.ConnectionUrl.buildConnectionStringCacheKey(ConnectionUrl.java:247) 
at com.mysql.cj.conf.ConnectionUrl.getConnectionUrlInstance(ConnectionUrl.java:186) 
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:204) 
at java.sql.DriverManager.getConnection(DriverManager.java:569) 
at java.sql.DriverManager.getConnection(DriverManager.java:219) 
at com.example.skyline.test.MainActivity$1.run(MainActivity.java:35) 
at java.lang.Thread.run(Thread.java:764) 
2018-11-06 00:38 添加评论 分享
已邀请:

要回复问题请先登录注册

退出全屏模式 全屏模式 回复