个人作业6

完善优化了打卡功能,加入了自动记录时间和日期判断功能,方便用户记录学习时间,且用户一天只能打卡一次,避免重复打卡,优化用户体验
public int panduan(){
    Connection conn = null;
    Statement stmt = null;
    try {
        // 连接数据库
        conn = JDBCUtils.getConn();

        // 获取今天的日期
        LocalDate today = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            today = LocalDate.now();
        }

        // 创建 Statement 对象
        stmt = conn.createStatement();

        // 用户ID(假设用户ID为123)
        int userId = 123;

        // 执行 SQL 查询,检查用户今天是否已经打过卡
        String sqlCheck = "SELECT * FROM daka WHERE riqi = '" + today + "'";
        ResultSet rs = stmt.executeQuery(sqlCheck);

        // 如果查询结果不为空,则表示用户今天已经打过卡,否则允许用户进行打卡
        if (rs.next()) {
            System.out.println("今天已经打过卡了!");
            return 0;
        }
        // 关闭连接
        rs.close();
        stmt.close();
        conn.close();
    } catch (SQLException se) {
        // 处理 SQL 异常
        se.printStackTrace();
    } catch (Exception e) {
        // 处理其他异常
        e.printStackTrace();
    } finally {
        // 关闭资源
        try {
            if (stmt != null) stmt.close();
        } catch (SQLException se2) {
        }
        try {
            if (conn != null) conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
        return 1;
}

 

public void daka(View view) {
        new Thread() {
            @Override
            public void run() {
                UserDao userDao = new UserDao();
                int msg = userDao.panduan();
                hand1.sendEmptyMessage(msg);
            }
        }.start();

    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){

                Toast.makeText(getApplicationContext(), "今日已打卡,请勿重复打卡", Toast.LENGTH_LONG).show();

            } else if (msg.what == 1) {
                Intent intent = new Intent(aaa0.this, today.class);
                startActivity(intent);
            }
        }
    };

 

posted @ 2024-04-12 23:03  奶油冰激凌  阅读(10)  评论(0)    收藏  举报