码迷,mamicode.com
首页 > 数据库 > 详细

签到系统.MySQL/JDBC

时间:2015-07-06 17:29:31      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

DROP TABLE IF EXISTS testcheck;
CREATE TABLE users (
  user_id   INT          NOT NULL  AUTO_INCREMENT,
  username  VARCHAR(20)  NOT NULL,
  password  VARCHAR(20)  NOT NULL,
  PRIMARY KEY(user_id)
) ENGINE = InnoDB;

INSERT INTO users VALUES (null, test, 123456);

DROP TABLE IF EXISTS checkin;
CREATE TABLE checkin (
  username     VARCHAR(20)  NOT NULL,
  checkindays  DATE     NOT NULL
) ENGINE = InnoDB;

INSERT INTO checkin VALUES (test, CURDATE());

 

// 完成连接数据库操作,生成并返回Statement
    public Statement getStatement() {
        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            stmt = con.createStatement();
            return stmt;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 检查用户名&密码
    public boolean checkUser(String username, String password) {
        try {
            String loginQuery = "SELECT * FROM users WHERE username = ‘"
                    + username + "‘ AND password = ‘" + password + "‘";
            stmt = getStatement();
            rs = stmt.executeQuery(loginQuery);
            if (rs.next()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    // 添加签到记录
    public void checkin(String username) {
        try {
            // 调用MySQL的CURDATE()函数,自动获取当前日期
            String addCheckin = "INSERT INTO checkin VALUES (‘" + username
                    + "‘, CURDATE())";
            stmt = getStatement();
            stmt.executeUpdate(addCheckin);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // 查询签到年(本年)
    public ResultSet queryCURYear(String username) {
        try {
            String queryCURMonth = "SELECT YEAR(CURDATE()) AS currentYear";
            stmt = getStatement();
            rs = stmt.executeQuery(queryCURMonth);
            return rs;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // 查询签到月(当前月)
    public ResultSet queryCURMonth(String username) {
        try {
            String queryCURMonth = "SELECT MONTH(CURDATE()) AS currentMonth";
            stmt = getStatement();
            rs = stmt.executeQuery(queryCURMonth);
            return rs;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // 查询签到月(当前月)的第一天属于星期几
    public ResultSet queryStartDayOfWeekInCURMonth() {
        try {
            // 语句含义:星期几(日期减法(CURDATE() - 日期(CURDATE()) - 1天))
            String queryStartDayOfWeekInCURMonth = "SELECT DAYOFWEEK(DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE()) - 1 DAY)) "
                    + "AS startDayOfWeekInCURMonth";
            stmt = getStatement();
            rs = stmt.executeQuery(queryStartDayOfWeekInCURMonth);
            return rs;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // 查询签到月(当前月)的总天数
    public ResultSet queryTotalNumberOfDaysInCURMonth() {
        try {
            String queryTotalNumberOfDaysInCURMonth = "SELECT DAY(LAST_DAY(CURDATE())) "
                    + "AS totalNumberOfDaysInCURMonth";
            stmt = getStatement();
            rs = stmt.executeQuery(queryTotalNumberOfDaysInCURMonth);
            return rs;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // 获取用户在当前月的签到天数
    public ResultSet queryCheckinDaysInCURMonth(String username) {
        try {
            // 自行判断当前月份,并获取本月的签到记录(日期)
            String queryCheckinDaysInCURMonth = "SELECT DISTINCT DATE(checkindays) "
                    + "AS checkinDaysInCURMonth "
                    + "FROM checkin "
                    + "WHERE YEAR(checkindays) = YEAR(CURDATE()) "
                    + "AND MONTH(checkindays) = MONTH(CURDATE()) "
                    + "AND username = ‘" + username + "‘ "
                    + "ORDER BY checkindays;";
            stmt = getStatement();
            rs = stmt.executeQuery(queryCheckinDaysInCURMonth);
            return rs;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

 

签到系统.MySQL/JDBC

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4624843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!