首先先连接MySQL 可以参考以下链接:Android studio连接mysql(能踩的坑都踩了)_黑名单 2841703718的博客-CSDN博客_android studio连接mysql
需要注意的是,如果Android Studio连接MySQL失败,可以排查以下是不是MySQL没有开启远程连接,或者有没有改用户名,密码,ip,数据库名以及字段:

GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%'IDENTIFIED BY '密码' WITH GRANT OPTION;

 

需要注意的是引入libs包,导入后可以在app/build.gradle文件中检查:

在文件中为我们的应用程序添加互联网权限AndroidManifest.xml

 

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

 接下来,从数据库中读取数据并在屏幕上显示信息。MainActivity java类下:

package com.example.party;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class homepage extends AppCompatActivity {

    private static final String URL = "jdbc:mysql://192.168.00.00:3306/数据库名";
    private static final String USER = "用户名";
    private static final String PASSWORD = "密码";

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

        new InfoAsyncTask().execute();
    }
    @SuppressLint("StaticFieldLeak")
    public class InfoAsyncTask extends AsyncTask<Void, Void, Map<String, String>> {
        @Override
        protected Map<String, String> doInBackground(Void... voids) {
            Map<String, String> info = new HashMap<>();

            try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
                String sql = "select usertype,userstate,userdel from usertable";
                PreparedStatement statement = connection.prepareStatement(sql);
                ResultSet resultSet = statement.executeQuery();
                if (resultSet.next()) {
                    info.put("usertype", resultSet.getString("usertype"));
                    info.put("userstate", resultSet.getString("userstate"));
                    info.put("userdel", resultSet.getString("userdel"));
                }
            } catch (Exception e) {
                Log.e("InfoAsyncTask", "Error reading school information", e);
            }

            return info;
        }

        @Override
        protected void onPostExecute(Map<String, String> result) {
            if (!result.isEmpty()) {
                TextView textViewName = findViewById(R.id.text1);
                TextView textViewAddress = findViewById(R.id.text2);
                TextView textViewPhoneNumber = findViewById(R.id.text3);

                textViewName.setText(result.get("usertype"));
                textViewAddress.setText(result.get("userstate"));
                textViewPhoneNumber.setText(result.get("userdel"));
            }
        }
    }
}

最后就可以运行了。

原文以及代码Demo请参考以下链接:

如何使用 JDBC 从 Android 读取 MySQL 数据?_allway2的博客-CSDN博客_安卓jdbc访问mysql数据库

更多推荐