当前位置:   article > 正文

android连接mysql

android连接mysql

1、Androidmanifest赋予联网的权限

<!--    赋予网络的权限-->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 1
  • 2
  • 3

2、将jar包导入项目中

https://repo1.maven.org/maven2/mysql/
使用的5.1.47的版本

3、写一个工具类

package com.example.study_mysql;

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

public class MySQLConnector {
    private Connection connection;
    private Statement statement;

    // 连接的方法
    public boolean connect(String url, String username, String password) {
        try {
            // 8.0 以后 com.mysql.cj.jdbc.Driver
            // 5.0 com.mysql.jdbc.Driver
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            statement = connection.createStatement();
            return true;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            return false;
        }
    }


    // 返回集
    public ResultSet executeQuery(String query) {
        try {
            return statement.executeQuery(query);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 其他数据库操作方法,如添加、更新、删除数据等
    public boolean add(String sql) {
        if (statement != null) {
            try {
                int res = statement.executeUpdate(sql);
                return res > 0;
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }




    // 关闭的操作
    public void close() {
        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

4、在MianActivity中异步调用即可

package com.example.study_mysql;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MainActivity extends AppCompatActivity {

    //    private static final String DB_URL = "jdbc:mysql://your_database_url";
    private static final String DB_URL = "jdbc:mysql://192.168.1.22:3306/sys?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "012200";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 执行任务
//        new DatabaseTask().execute();
        
        Button button = findViewById(R.id.button2);
        button.setOnClickListener(v -> {
            new Thread(runnable).start();
        });

        System.out.println("1111");

    }

    Runnable runnable = () -> {
        MySQLConnector connector = new MySQLConnector();
        if (connector.connect(DB_URL, DB_USERNAME, DB_PASSWORD)) {
            // 执行数据库操作
            // 示例:查询所有记录并打印结果
            String query = "SELECT * FROM sys_config";
            ResultSet resultSet = connector.executeQuery(query);
            if (resultSet != null) {
                try {
                    while (resultSet.next()) {
                        System.out.println("mysql" +
                                resultSet.getString(1) + resultSet.getString(2));
                    }
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        connector.close();
    };
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

需要跑到手机上时,可能因为版本的问题需要加一下代码:

gradle.properties

android.injected.testOnly=false
  • 1

注意:当前跑的程序一定可以访问到这个数据库!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/764841
推荐阅读
相关标签
  

闽ICP备14008679号