Android 连接MySQL

Posted 王睿丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 连接MySQL相关的知识,希望对你有一定的参考价值。

androidManifest.xml

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

在libs下添加mysql驱动
下载地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java
建议使用:mysql-connector-java-5.1.49.jar 版本
网盘下载:
链接:https://pan.baidu.com/s/153hgUmRVAr9xfbFQxsBbkQ
提取码:160j

build.gradle

implementation files('libs/mysql-connector-java-5.1.49.jar')

DBUtil

import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;


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


public class DBUtil 

    private static String diver = "com.mysql.jdbc.Driver";	//固定写法
    private static String url = "jdbc:mysql://ip地址:端口号/数据库名?characterEncoding=utf-8";
    private static String user = "wr";//用户名
    private static String password = "xxxxx";//密码


    /*
     * 连接数据库
     * */
    public static Connection getConn() 
        Connection conn = null;
        try 
            Class.forName(diver);
            conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接
            Log.e("getConn", "连接成功");
         catch (ClassNotFoundException e) 
            Log.e("getConn", e.getMessage(), e);
            e.printStackTrace();
         catch (SQLException e) 
            Log.e("getConn", e.getMessage(), e);
            e.printStackTrace();
        
        return conn;
    

    public static void mQuery(Connection conn)
        final String SQL ="select *from user";	//查询user表
        int id[]=new int[10];
        String name[]=new String[10],password[]=new String[10],age[]=new String[10],sfz[]=new String[10],address[]=new String[10];
        new Thread(new Runnable() 
            @Override
            public void run() 
                try 
                    Statement statement = (Statement)conn.createStatement();
                    ResultSet resultSet = statement.executeQuery(SQL);
                    int i=0;
                    while (resultSet.next())
                        id[i] = resultSet.getInt("ID");
                        name[i] = resultSet.getString("name");
                        password[i] = resultSet.getString("password");
                        age[i] = resultSet.getString("age");
                        sfz[i] = resultSet.getString("sfz");
                        address[i] = resultSet.getString("address");
                        Log.d("wangrui","id="+id[i]+"---name="+name[i]+"---password="+password[i]+"---age="+age[i]+"---sfz="+sfz[i]+"---address="+address[i]);
                        i++;
                    
                catch (SQLException e) 
                    e.printStackTrace();
                
            
        ).start();
    


    public static void close(Statement state, Connection conn) 
        if (state != null) 
            try 
                state.close();
             catch (SQLException e) 
                e.printStackTrace();
            
        

        if (conn != null) 
            try 
                conn.close();
             catch (SQLException e) 
                e.printStackTrace();
            
        
    

    public static void close(ResultSet rs, Statement state, Connection conn) 
        if (rs != null) 
            try 
                rs.close();
             catch (SQLException e) 
                e.printStackTrace();
            
        

        if (state != null) 
            try 
                state.close();
             catch (SQLException e) 
                e.printStackTrace();
            
        

        if (conn != null) 
            try 
                conn.close();
             catch (SQLException e) 
                e.printStackTrace();
            
        
    


MainActivity.java

public class MainActivity extends AppCompatActivity 
    private Connection conn;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new Runnable() 
            @Override
            public void run() 
                conn = DBUtil.getConn();
                DBUtil.mQuery(conn);
            
        ).start();
    

以上是关于Android 连接MySQL的主要内容,如果未能解决你的问题,请参考以下文章

Android用jdbc链接mysql并对数据库进行增,删,改,查;注意是Android

Android 连接MySQL

如何使用 Mysql JDBC 驱动程序将 Android 与 MySQL 连接 [重复]

Android:用 JDBC 连接 MySql

Android 连接 MySQL 数据库教程

Android - PHP - Mysql 之间的持久连接