Android整合mysql

前期准备

我这边使用的开发工具是Android Studio,采用的方式是导入jar包的方式引入mysql

在这里插入图片描述
将jar包拖到libs下面就可以,然后右键拖入的jar包,有一个add to library,mysql的jar包就导入了。

mysql连接工具类

package com.example.dayfour2021_6_10.utils;

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

/**
 * @author xiaow
 * Created on 2021/6/16
 */
public class DbOpenHelper {

    private static String driver = "com.mysql.jdbc.Driver";// mysql 驱动
    private static String ip = "ipaddr";  // 安装了 mysql 的电脑的 ip 地址
    private static String dbName = "ssm";    // 要连接的数据库
    private static String url = "jdbc:mysql://ipaddr/home??useUnicode=true&characterEncoding=UTF-8&useAffectedRows=true&useTimezone=true&serverTimezone=GMT%2B8";    // mysql 数据库连接 url
    private static String user = "username";    // 用户名
    private static String password = "pwd"; // 密码

    private static Connection sConnection;

    /**
     * 连接数据库
     */
    public static Connection getConnection() {
        if (sConnection == null) {
            try {
                Class.forName(driver);  // 获取 mysql 驱动
                sConnection = DriverManager.getConnection(url, user, password);   // 获取连接
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return sConnection;
    }

    /**
     * 关闭数据库
     */
    public static void closeConnection() {
        if (sConnection != null) {
            try {
                sConnection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

这里没有加入连接池,有兴趣的可以加一下,毕竟有了连接池会更好一些。

Dao

package com.example.dayfour2021_6_10.dao;

import com.example.dayfour2021_6_10.utils.DbOpenHelper;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import static java.lang.System.*;

/**
 * @author xiaow
 * Created on 2021/6/16
 */

public class AllDao {
    private Connection connection;
    public AllDao(){
        connection= DbOpenHelper.getConnection();
    }

    public List<Float> list(String tableName){
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        List<Float> all=new LinkedList<>();
        try {
            preparedStatement=connection.prepareStatement("select * from "+tableName+" order by id desc limit 10");
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
            all.add(resultSet.getFloat(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(resultSet!=null)
                    resultSet.close();
                if(preparedStatement!=null)
                    preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return all;
    }

    public void deleteFirst(String tableName){
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement=connection.prepareStatement("delete from "+tableName+" limit 1");
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(preparedStatement!=null)
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void addLast(String tableName,Float data){
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement=connection.prepareStatement("insert into "+tableName+" values(0,?,now())");
            preparedStatement.setDouble(1,data);
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(preparedStatement!=null)
                    preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

仅仅实现了简单的查询和删除功能,其他功能可以自主添加,和java操作mysql一摸一样,有兴趣的可以在封装一个service层。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王不头秃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值