android连接mysql数据库

在android中我们是可以连接mysql数据库的。连接方式如下:

1.首先我们需要导入mysql驱动jar包下载地址:

链接:https://pan.baidu.com/s/1PV9jV9m3LLjXeLfSE5ChOg 
提取码:3v2q

 2.连接数据库的代码:

package com.demo.take.dao;

import android.util.Log;


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://192.168.0.199:3306/school_take?characterEncoding=utf-8";
    private static String user = "root";//用户名
    private static String password = "root";//密码

    /*
     * 连接数据库
     * */
    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 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();
            }
        }
    }

}

 3.下面给出一个增删改查的类

//用户数据库连接类
public class UserDao {

    //新增
    public static boolean add(UserBean bean) {
        String sql = "insert into user(user_name,phone,create_date,password)values('" + bean.getUser_name() + "','" + bean.getPhone() + "','" + bean.getCreate_date() + "','" + bean.getPassword() + "')";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (Exception e) {
            Log.e("add->", e.getMessage(), e);
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }
        if (a > 0) {
            f = true;
        }
        return f;
    }

    //删除
    public static boolean delete(UserBean bean) {
        String sql = "delete from user where id=" + bean.getId();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (Exception e) {
            Log.e("delete->", e.getMessage(), e);
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }
        if (a > 0) {
            f = true;
        }
        return f;
    }


    //修改
    public static boolean update(UserBean bean) {

        String sql = "update user set " + "user_name='" + bean.getUser_name() + "', phone='" + bean.getPhone() + "', create_date='" + bean.getCreate_date() + "', password='" + bean.getPassword() + "' where id='" + bean.getId() + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (Exception e) {
            Log.e("update->", e.getMessage(), e);
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }
        if (a > 0) {
            f = true;
        }
        return f;
    }

    //获取列表
    public static List<UserBean> getListByPhone(String phone) {

        //结果存放集合
        List<UserBean> list = new ArrayList<>();
        //MySQL 语句
        String sql = "select * from user where phone=" + phone;
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Log.e("getListByPhone->","getListByPhone");
            while (rs.next()) {
                UserBean bean = new UserBean();
                bean.setId(rs.getInt("id"));
                bean.setUser_name(rs.getString("user_name"));
                bean.setPhone(rs.getString("phone"));
                bean.setPassword(rs.getString("password"));
                bean.setCreate_date(rs.getString("create_date"));
                list.add(bean);
                Log.e("getListByPhone->",bean.toString());

            }
        } catch (Exception e) {
            Log.e("getListByPhone->", e.getMessage(), e);
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        if (a > 0) {
            f = true;
        }
        return list;
    }

    //获取列表
    public static List<UserBean> getList() {

        //结果存放集合
        List<UserBean> list = new ArrayList<>();
        //MySQL 语句
        String sql = "select * from user";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                UserBean bean = new UserBean();
                bean.setId(rs.getInt("id"));
                bean.setUser_name(rs.getString("user_name"));
                bean.setPhone(rs.getString("phone"));
                bean.setPassword(rs.getString("password"));
                bean.setCreate_date(rs.getString("create_date"));
                list.add(bean);

            }
        } catch (Exception e) {
            Log.e("update->", e.getMessage(), e);
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        if (a > 0) {
            f = true;
        }
        return list;
    }


}

4.下面是对应的用户实体类

package com.demo.take.bean;

import java.io.Serializable;
//用户实体类
public class UserBean implements Serializable {
    //主键
    private int id;
    //用户姓名
    private String user_name;
    //手机号
    private String phone;
    //密码
    private String password;
    //创建时间
    private String create_date;

    public String getCreate_date() {
        return create_date;
    }

    public void setCreate_date(String create_date) {
        this.create_date = create_date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserBean{" +
                "id=" + id +
                ", user_name='" + user_name + '\'' +
                ", phone='" + phone + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

5.然后呢mysql数据库是需要自己去安装的,安装完数据库根据实体类创建对应的表,连接上就可以调用了。如下:

注意 :1.mysql安装需要安装5版本的,高版本连不上。

            2.android连接mysql需要再子线程中去执行。

项目下载地址:android发布任务小项目-Android文档类资源-CSDN下载

如果有啥不明白可以加qq:332872622

  • 19
    点赞
  • 204
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序中连接MySQL数据库,可以使用以下步骤: 1. 在MySQL服务器上创建数据库和表,并添加数据。 2. 在Android应用程序中添加连接MySQL数据库的驱动程序(例如,MySQL Connector/J)。 3. 在Android应用程序中创建一个线程(如AsyncTask),并在线程中编写代码以连接MySQL数据库,并执行SQL查询。 4. 在Android应用程序中解析MySQL数据库返回的结果,并将结果显示在UI上。 下面是一个简单的示例,演示如何在Android应用程序中连接MySQL数据库: ```java // 导入MySQL Connector/J库 import java.sql.*; // 定义MySQL数据库连接参数 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "password"; // 在线程中连接MySQL数据库 new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { try { // 加载MySQL驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 创建MySQL连接 Connection con = DriverManager.getConnection(url, user, password); // 创建SQL查询 Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users"); // 解析查询结果 while (rs.next()) { String name = rs.getString("name"); int age = rs.getInt("age"); // TODO: 显示查询结果 } // 关闭数据库连接 rs.close(); stmt.close(); con.close(); } catch (Exception e) { // TODO: 处理异常 } return null; } }.execute(); ``` 需要注意的是,在实际应用中,需要考虑到数据安全和性能等问题,例如,使用SSL加密连接、防止SQL注入等。同时,还需要考虑到在移动网络环境下,数据库连接的稳定性和性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值