Android Studio无法连接到mysql

时间:2018-10-24 15:14:59

标签: android mysql

我是android开发的初学者。最近,我尝试从本地Android Studio连接Mysql。当我单击应用程序并连接到Mysql时,该应用程序刚刚停止。有错误消息。

Process: com.example.hasee.mysqlsampleproject, PID: 18544
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:318)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
 Caused by: java.lang.UnsupportedOperationException
    at java.util.regex.Matcher.group(Matcher.java:383)
    at com.mysql.cj.conf.ConnectionUrlParser.parseConnectionString(ConnectionUrlParser.java:141)
    at com.mysql.cj.conf.ConnectionUrlParser.<init>(ConnectionUrlParser.java:129)
    at com.mysql.cj.conf.ConnectionUrlParser.parseConnectionString(ConnectionUrlParser.java:118)
    at com.mysql.cj.conf.ConnectionUrl.getConnectionUrlInstance(ConnectionUrl.java:179)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:201)
    at java.sql.DriverManager.getConnection(DriverManager.java:569)
    at java.sql.DriverManager.getConnection(DriverManager.java:219)
    at com.example.hasee.mysqlsampleproject.MainActivity$GetData.doInBackground(MainActivity.java:77)
    at com.example.hasee.mysqlsampleproject.MainActivity$GetData.doInBackground(MainActivity.java:54)
    at android.os.AsyncTask$2.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:761) 

这是build.gradle

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.example.hasee.mysqlsampleproject"
    minSdkVersion 15
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}}

还有我的主要代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GetData getData=new GetData();
    getData.execute("");
}

private class GetData extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... strings) {
        Connection connection=null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection= DriverManager.getConnection(DbString.DATABASE_URL,DbString.DATABASE_USER,DbString.DATABASE_passwd);
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}}

我还将这段代码写到了清单中:

<uses-permission android:name="android.permission.INTERNET"/>

另外,我还设置了Mysql打开以进行连接。但是,我仍然无法连接Mysql。

请有人告诉我如何解决此问题。

非常感谢您。

0 个答案:

没有答案