简体   繁体   中英

Problem with connecting MYSQL in JAVA android app

I've tried all methods and ways from stack and videos and none of them actually worked so far.

MAIN ACTIVITY CLASS

package com.example.androidmysql;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    TextView text;
    Button show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.textView);
        show = (Button) findViewById(R.id.button);
        show.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
                    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/example","root","test");
                    Statement statement = connection.createStatement();
                    ResultSet resultSet = statement.executeQuery("SELECT * FROM user");
                    String records ="";
                    while (resultSet.next()) {
                        records += resultSet.getString(1) + " " + resultSet.getString(2) + "\n";
                    }
                    text.setText(records);
                }
                catch (Exception e) {
                    e.printStackTrace();
                    text.setText(e.toString());
                }
            }
        });
    }
}

The main problem is that JDBC can't connect to database

OUTPUT

W/System.err: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
W/System.err:     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1009)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
        at java.sql.DriverManager.getConnection(DriverManager.java:580)
        at java.sql.DriverManager.getConnection(DriverManager.java:218)
        at com.example.androidmysql.MainActivity$1.onClick(MainActivity.java:47)
        at android.view.View.performClick(View.java:7448)
W/System.err:     at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
  • I'm using the latest mysql-connector-8.0.22.
  • Added permissions to access internet in manifest
  • Installed mysql server 8.0.22
  • I've also changed config C:\ProgramData\MySQL\MySQL Server 8.0\my.ini => bind-address=0.0.0.0
  • Tested on different JDK's: 15, 1.8

Here is the code which i uploaded on github. Can someone test it on their pc with their example database? https://github.com/simplybychris/DatabaseClientApp/tree/master

Database user and password are good, cause I've tested it on separate class without main activity and outside Java project and it works... It looks like it won't work in Mobile Classes

Thanks for any tips

I don't know why people minusing the question if there is no correct answer. Anyway after 2 days of searching I've finally found the solution. There's the problem while connecting with mysql latest 8.0 driver. I suggest to change it to mysql 5.7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM