Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

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) + "
";
                    }
                    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:ProgramDataMySQLMySQL Server 8.0my.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

question from:https://stackoverflow.com/questions/65540750/problem-with-connecting-mysql-in-java-android-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...