首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Android整合mysql

Android整合mysql

作者头像
小王不头秃
发布2024-06-19 15:18:24
发布2024-06-19 15:18:24
4670
举报

前期准备

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

将jar包拖到libs下面就可以,然后右键拖入的jar包,有一个add to library,mysql的jar包就导入了。

mysql连接工具类

代码语言:javascript
复制
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

代码语言:javascript
复制
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层。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-06-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前期准备
  • mysql连接工具类
  • Dao
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档