Android连接mysql可视化数据库

Android连接Mysql数据库,实现数据库的可视化

我之前连接的是SQLite数据库,然后它就只能实现数据库的各种操作,但是无法使得数据库内容的可视化,就,其实也挺崩溃的

现在再来尝试连接一下Mysql数据库,也算是顺便弥补一下心里的遗憾吧!

具体步骤

1、导入跟自己数据库版本相同的jar包(我这里是8.0.29版本)

2、将jar包导入到library里面

出现这样的文件之后,才算是成功导入jar包:

3、在数据库里面创建一个较为简单的表,用于连接数据库的测试

4、新建数据库帮助类DBHelper

package com.example.database2;

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

public class DBHelper {
    private static String driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://192.168.16.147:3306/aa";
    private static String user="root";
    private static String password="20214063";

    public static Connection getConn(){
        Connection conn=null;
        try{
            System.out.println("连接数据库1");
            Class.forName(driver);
            System.out.println("连接数据库2");
            conn=(Connection) DriverManager.getConnection(url,user,password);
            System.out.println("conn:"+conn);
            System.out.println("连接数据库3");
        }catch(ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }

        return conn;
    }
}

5、渲染android测试页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询数据库">
    </Button>

    <TextView
        android:id="@+id/tv_look"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </TextView>


</LinearLayout>

6、添加网络权限(在AndroidManifest.xml文件里面添加)

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

7、编写页面后台,连接数据库

package com.example.database2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import android.os.Message;

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


public class MainActivity extends AppCompatActivity {
    private Button btn_send;
    private TextView tv_look;

    private static final int TEST_USER_SELECT = 1;
    int i =0;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String user;
            switch (msg.what) {
                case TEST_USER_SELECT:
                    com.example.database2.Test test= (com.example.database2.Test) msg.obj;
                    user = test.getName();
                    System.out.println("***********");
                    System.out.println("***********");
                    System.out.println("user:" + user);
                    tv_look.setText(user);
                    break;
            }
        }
    };

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

        btn_send=findViewById(R.id.btn_send);
        tv_look=findViewById(R.id.tv_look);
    }
    @Override
    protected void onStart() {

        super.onStart();

        btn_send.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                //执行查询操作
                //通多点击buttoni自增长查询对应id的name
                if (i<=3){//因为数据库我就添加了三个数据条数,所以进行判断使其可以循环查询
                    i++;
                }
                else{
                    i=1;
                }
                //连接数据库进行操作需要在主线程操作
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Connection conn = null;
                        conn =(Connection) DBHelper.getConn();
                        if(conn==null){
                            System.out.println("&&&&&&&&&&&&&&&&&");
                            System.out.println("输出为null");
                        }
                        String sql = "select name from test_one where id='"+i+"'";
                        Statement st;
                        try {
                            st = (Statement) conn.createStatement();
                            ResultSet rs = st.executeQuery(sql);
                            while (rs.next()){
                                //因为查出来的数据试剂盒的形式,所以我们新建一个Test实体类存储
                                Test test=new Test();
                                test.setName(rs.getString(1));
                                Message msg=new Message();
                                msg.what =TEST_USER_SELECT;
                                msg.obj = test;
                                handler.sendMessage(msg);
                            }
                            st.close();
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
}

8、页面测试连接数据库即可

posted @ 2023-04-18 21:21  yesyes1  阅读(118)  评论(0编辑  收藏  举报