每日总结2023/3/9

今天试验了网上基础的安卓MySQL、连接提示以及检测

package com.example.a86191.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {



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

    }}

mainactivity

package com.example.a86191.test;

import android.util.Log;

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

/**
 * Created by 86191 on 2023/3/5.
 */

public class Connection {
    private static final String TAG = "mysql11111";
    java.sql.Connection conn=null;
    public static void mymysql(){
        final Thread thread =new Thread(new Runnable() {
            @Override
            public void run() {

                while (!Thread.interrupted()) {
                    try {
                        Thread.sleep(100);  // 每隔0.1秒尝试连接
                    } catch (InterruptedException e) {
                        Log.e(TAG, e.toString());
                    }

// 1.加载JDBC驱动
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        Log.v(TAG, "加载JDBC驱动成功");
                    } catch (ClassNotFoundException e) {
                        Log.e(TAG, "加载JDBC驱动失败");
                        return;
                    }

                    // 2.设置好IP/端口/数据库名/用户名/密码等必要的连接信息
                    String ip = "169.254.37.73";
                    int port =5418;
                    String dbName = "mysql";
                    String url = "jdbc:mysql://" + ip + ":" + port
                            + "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
                    // 构建连接mysql的字符串
                    String user = "root";
                    String password = "zhaowenzhe1007";

                    // 3.连接JDBC
                    try {
                        java.sql.Connection conn = DriverManager.getConnection(url, user, password);
                        Log.d(TAG, "数据库连接成功");
                        conn.close();
                        return;
                    }
                    catch (SQLException e) {
                        Log.e(TAG, e.getMessage());
                    }

                }
            }
        });
        thread.start();



    }

connection类

这个比较简单,只需要注意更改ip地址以及端口,端口确定好自己的低版本mysql就行

posted @ 2023-03-09 23:07  橘子味芬达水  阅读(7)  评论(0编辑  收藏  举报