签到系统.MySQL/JDBC

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;
        }
    }
时间: 2024-11-08 16:12:11

签到系统.MySQL/JDBC的相关文章

重装系统,新安装IDEA启动项目后,classnotfound:com.mysql.jdbc.Driver

我最后解决是:这个Test connection会自动帮你下载的,但是如果中途一直叫你try again,甚至到后面点这个test connection有弹窗,但是单窗里面的选项你点击后没反应,我是直接卸载IDEA重装了,(浪费一个下午弄这个问题),然后再来一次,就成功了. 我再说一下症状:我新装的IDEA,(重装系统),打开我以前的maven项目试着启动,报错找不到com.mysql.jdbc.Driver,检查依赖和本地的仓库,没发现mysql-connector-java有问题,打开IDE

com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 > 1024)

### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 > 1024). You can change this value on the server by setting the max_allowed_packet' variable. ; SQL []; Packet for query is too large (1169 > 1024). You can change

MySql & JDBC

1.什么是数据库? 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以通过SQL对数据库中的数据进行增加.修改.删除.及查询操作. 数据库系统类型(历史发展): 网状型数据库 层次型数据库 关系数据库 ---理论最成熟.应用最广泛 面向对象数据库 常见的数据库(软件): MYSQL Oracle DB2 SQLServer SyBase SQLite Java相关: MYSQL  Oracle 2.数据库和表 数据表示存储数据的逻辑单元,可以把数据表想象成

MySql & JDBC & 连接池 & 总结

连接池:解决资源浪费,提高代码性能. 本小节目标: 使用DBCP,C3P0连接池完成基本数据库的操作. 使用DBUtils完成CRUD的操作. 数据库连接池的解决方案是: 当应用程序启动时,系统主动建立足够的数据库连接,并将这些连接组成一个连接池.每次应用程序请求数据库连接时,无须重新打开连接,而是从连接池中取出已有的连接使用,使用完后不再关闭数据库连接,而是直接将连接归还给连接池.通过使用连接池,将大大提高程序的运行效率. 数据库连接池是Connection 对象的工程.数据库连接池的常用参数

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错

-- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现wait_timeout与该参数意思相近,修改时,一起修改 SHOW VARIABLES WHERE VAriable_name = '%timeout'; -- 解决问题 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Commun

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"

最简单的办法是因为你的my.ini中设定的并发连接数太少或者系统繁忙导致连接数被占满解决方式:打开MySQL安装目录打开MY.INI找到max_connections默认是100 .--实例 WINDOWS解决mysql Errno.: 1040错误XXXX info: Can not connect to MySQL server User: rootTime: 2004-5-20 3:00pmScript: /XXXX/XXXX.PHP Error: Too many connections

nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link fai

今天晚上一个财务子系统上线,部署完成后,测试反馈程序会抽风,一会儿正常,一会儿异常,报如下错误: nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link fai 通过查找资料,可能是wait_timeout设置过小导致的,尝试加大wait_timeout的值后,系统恢复正常. 更改过程如下: ##查看MySQL配置 [email protected]:mysq

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Row size too large (> 8126)

同样是工作中遇到的问题,写下来留个纪念. 一个老系统已经跑了一年多了,最近报出这个问题的,具体问题贴出来给大家看 org.springframework.jdbc.BadSqlGrammarException: ### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Row size too large (> 8126). Changing some colum

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:插入blob数据时出现乱码

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'u–ì€?ú?]qs\0¥?c§?wb????ú??h?A?nC??W·g·ù+\',YóXè2o?