基于阿里数据库连接池Druid和Apache封装工具类DBUtil的JDBC实现对数据库视图的查询和表的增删改查操作

1. Druid

  1. Druid首先是一个数据库连接池。Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。Druid是阿里巴巴开发的号称为监控而生的数据库连接池!

  2. 它有三个特点:

    • 处理的数据量规模较大。
    • 可以进行数据的实时查询展示。
    • 它的查询模式是交互式的,这也说明其查询并发能力有限。
  3. Druid 之所以能够在 OLAP 家族中占据一席之地,主要依赖其强大的 MPP 架构设计,关于它的架构,这里就不展开描述了,感兴趣的同学可以登陆 官网 进行了解。

2. Apache工具类DBUtil

  1. 点此下载
  2. Apache Commons DbUtils工具包使用介绍

具体介绍上面很详细,也可以看我的代码。

3.代码结构

本人的项目并不是在项目的根目录下面建立的,所以里面有些路径是需要修改的。可以把代码看做参考:
基于阿里数据库连接池Druid和Apache封装工具类DBUtil的JDBC实现对数据库视图的查询和表的增删改查操作_第1张图片
分别代码:

  1. Test
package com.student.control;

import com.student.dao.sqlstatements.SqlStatement;
import com.student.dao.studao.CRUD_StudentDao;
import com.student.util.DruidConnectionPool;

import java.sql.Connection;
import java.util.List;

public class Test {
//    DruidConnectionPool druidConnectionPool = new DruidConnectionPool();

    public static void main(String[] args) {
        Connection connection = DruidConnectionPool.getConnection();

        CRUD_StudentDao crud_studentDao = new CRUD_StudentDao();
        List list ;
        int rows = 0;

        rows = crud_studentDao.updateTable(SqlStatement.Insert_Check,2017010105,01,"2020-02-18 11:01:54");

        System.out.println("影响了 "+rows+" 行.");

//        2  查询某个班的 某天的所有考勤信息(查)
//        list = crud_studentDao.selectV_class_check(SqlStatement.Select_Class_Day_All_Check,"计科一班","%2020-02-13%");

//      3   查某个学生 2 月的所有考勤情况(查)
//        list = crud_studentDao.selectV_student_check(SqlStatement.Select_Student_Month_All_Check,"张三","%2020-02%");


//        System.out.println(list);

    }

}

  1. SqlStatement
package com.student.dao.sqlstatements;

//        1  给某个学生记录 某天的考勤信息(增)
//
//        2  查询某个班的 某天的所有考勤信息(查)
//
//        3  查某个学生 2 月的所有考勤情况(查)
//

public interface SqlStatement {

//        1  给某个学生记录 某天的考勤信息(增)
    String Insert_Check = "INSERT INTO checks (s_id,c_id,check_time) VALUES (?,?,?)";


//SELECT * FROM v_class_check WHERE c_name ='计科一班' AND check_time LIKE '%2020-02-17%';
//        2  查询某个班的 某天的所有考勤信息(查)
    String Select_Class_Day_All_Check = "select * from v_class_check where c_name = ? AND check_time LIKE ?";



//        3  查某个学生 2 月的所有考勤情况(查)
    String Select_Student_Month_All_Check = "SELECT * FROM select_stu_2month WHERE s_name = ? AND check_time LIKE ?";

}

  1. CRUD_StudentDao
package com.student.dao.studao;

import com.student.util.DruidConnectionPool;
import com.student.vo.V_class_check;
import com.student.vo.V_student_check;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CRUD_StudentDao {

    Connection connection = DruidConnectionPool.getConnection();

    int rows = 0;

    List list ;


    /**
     * 根据sql语句,进行对应的表中的字段的更新
     * @param sql
     * 预处理好的PreperedStatement
     * @param args
     * 传递的参数值,不过是以数组的另一种写法
     * @return
     */
    public int updateTable(String sql,Object ...args){
        try {
            QueryRunner queryRunner = new QueryRunner();
            rows = queryRunner.update(connection,sql,args);

            System.out.println("affacts "+rows+" rows.");

            connection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return rows;
    }



    /**
     * 查询某个班的 某天的所有考勤信息(查)
     * 根据sql语句进行表查询,并返回结果集
     * @param sql
     * @param args
     * @return
     */
    public List selectV_class_check(String sql,String ...args){

        try {
            QueryRunner queryRunner = new QueryRunner();

            list = queryRunner.query(connection,sql,new BeanListHandler<V_class_check>(V_class_check.class),args);

            connection.close();

        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }



    /**
     * 查某个学生 2 月的所有考勤情况(查)
     * @param sql
     * @param args
     * @return
     */
    public List selectV_student_check (String sql,String ...args){

        try {
            QueryRunner queryRunner = new QueryRunner();

            list = queryRunner.query(connection,sql,new BeanListHandler<V_student_check>(V_student_check.class),args);

            connection.close();

        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }



}


  1. DruidConnectionPool
package com.student.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;

public class DruidConnectionPool {
    static Connection connection;
   public static Connection getConnection() {
       try {
           FileInputStream fileInputStream = new FileInputStream("./src/com/student/druid.properties");
           Properties properties = new Properties();
           properties.load(fileInputStream);

           DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            connection= dataSource.getConnection();

           System.out.println(connection);


       } catch (Exception e) {
           e.printStackTrace();
       }
        return connection;
   }

}

  1. V_class_check
package com.student.vo;

import java.sql.Timestamp;

public class V_class_check {
    private int c_id,s_id;
    private String c_name,s_name;
    private Timestamp check_time;


    public V_class_check() {

    }

    public V_class_check(int c_id, int s_id, String c_name, String s_name, Timestamp check_time) {
        this.c_id = c_id;
        this.s_id = s_id;
        this.c_name = c_name;
        this.s_name = s_name;
        this.check_time = check_time;
    }

    public int getC_id() {
        return c_id;
    }

    public void setC_id(int c_id) {
        this.c_id = c_id;
    }

    public int getS_id() {
        return s_id;
    }

    public void setS_id(int s_id) {
        this.s_id = s_id;
    }

    public String getC_name() {
        return c_name;
    }

    public void setC_name(String c_name) {
        this.c_name = c_name;
    }

    public String getS_name() {
        return s_name;
    }

    public void setS_name(String s_name) {
        this.s_name = s_name;
    }

    public Timestamp getCheck_time() {
        return check_time;
    }

    public void setCheck_time(Timestamp check_time) {
        this.check_time = check_time;
    }


    @Override
    public String toString() {
        return "V_class_check{" +
                "c_id=" + c_id +
                ", s_id=" + s_id +
                ", c_name='" + c_name + '\'' +
                ", s_name='" + s_name + '\'' +
                ", check_time=" + check_time +
                '}';
    }
}

  1. V_student_check

package com.student.vo;

import java.sql.Timestamp;

public class V_student_check {
    private int s_id;
    private String s_name,c_name;
    private Timestamp check_time;

    public V_student_check() {

    }

    public V_student_check(int s_id, String s_name, String c_name, Timestamp check_time) {
        this.s_id = s_id;
        this.s_name = s_name;
        this.c_name = c_name;
        this.check_time = check_time;
    }

    public int getS_id() {
        return s_id;
    }

    public void setS_id(int s_id) {
        this.s_id = s_id;
    }

    public String getS_name() {
        return s_name;
    }

    public void setS_name(String s_name) {
        this.s_name = s_name;
    }

    public String getC_name() {
        return c_name;
    }

    public void setC_name(String c_name) {
        this.c_name = c_name;
    }

    public Timestamp getCheck_time() {
        return check_time;
    }

    public void setCheck_time(Timestamp check_time) {
        this.check_time = check_time;
    }

    @Override
    public String toString() {
        return "V_student_check{" +
                "s_id=" + s_id +
                ", s_name='" + s_name + '\'' +
                ", c_name='" + c_name + '\'' +
                ", check_time=" + check_time +
                '}';
    }
}

  1. druid.properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/student_check?characterEncoding=utf8&useSSL=false
username=root
password=Admin888.
#初始连接数
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000
#最大空闲数
# maxIdle=8
#最小空闲数
# minIdle=3


#下面的代码用来解决一个报错的,说是Spingboot配置什么的,但是目前并没有进行那么复杂的操作。所以等以后再来看具体的原因

# com.alibaba.druid.pool.DruidDataSource error
# 严重: testWhileIdle is true, validationQuery not set

timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200

#   validationQuery = "SELECT 1"  验证连接是否可用,使用的SQL语句

#   testWhileIdle = "true"      指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.

#   testOnBorrow = "false"   借出连接时不要测试,否则很影响性能

#   timeBetweenEvictionRunsMillis = "30000"30秒运行一次空闲连接回收器

#   minEvictableIdleTimeMillis = "1800000"  池中的连接空闲30分钟后被回收,默认值就是30分钟。

#   numTestsPerEvictionRun="3" 在每次空闲连接回收器线程(如果有)运行时检查的连接数量,默认值就是3.


4. SQL脚本和数据库结构


/*
 Navicat Premium Data Transfer

 Source Server         : 本地连接
 Source Server Type    : MySQL
 Source Server Version : 80019
 Source Host           : localhost:3306
 Source Schema         : student_check

 Target Server Type    : MySQL
 Target Server Version : 80019
 File Encoding         : 65001

 Date: 18/02/2020 19:34:33
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for checks
-- ----------------------------
DROP TABLE IF EXISTS `checks`;
CREATE TABLE `checks` (
  `chno` int NOT NULL AUTO_INCREMENT,
  `s_id` int NOT NULL,
  `c_id` int NOT NULL,
  `check_time` datetime DEFAULT NULL,
  PRIMARY KEY (`chno`,`s_id`,`c_id`),
  UNIQUE KEY `uq_chno` (`chno`) USING BTREE,
  KEY `fk_checks_students_1` (`s_id`),
  KEY `fk_checks_classes_1` (`c_id`),
  CONSTRAINT `fk_checks_classes_1` FOREIGN KEY (`c_id`) REFERENCES `classes` (`c_id`),
  CONSTRAINT `fk_checks_students_1` FOREIGN KEY (`s_id`) REFERENCES `students` (`s_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of checks
-- ----------------------------
BEGIN;
INSERT INTO `checks` VALUES (1, 2017010101, 1, '2020-02-10 11:01:54');
INSERT INTO `checks` VALUES (2, 2017010101, 1, '2020-02-11 11:02:18');
INSERT INTO `checks` VALUES (3, 2017010101, 1, '2020-02-12 11:02:34');
INSERT INTO `checks` VALUES (4, 2017010101, 1, '2020-02-13 11:02:52');
INSERT INTO `checks` VALUES (5, 2017010102, 1, '2020-02-10 11:03:14');
INSERT INTO `checks` VALUES (6, 2017010102, 1, '2020-02-11 11:03:27');
INSERT INTO `checks` VALUES (7, 2017010102, 1, '2020-02-12 11:03:39');
INSERT INTO `checks` VALUES (8, 2017010102, 1, '2020-02-13 11:03:51');
INSERT INTO `checks` VALUES (9, 2017010103, 1, '2020-02-10 11:04:02');
INSERT INTO `checks` VALUES (10, 2017010103, 1, '2020-02-11 11:04:15');
INSERT INTO `checks` VALUES (11, 2017010103, 1, '2020-02-12 11:04:26');
INSERT INTO `checks` VALUES (12, 2017010103, 1, '2020-02-13 11:04:45');
INSERT INTO `checks` VALUES (13, 2017010104, 1, '2020-02-10 11:04:56');
INSERT INTO `checks` VALUES (14, 2017010104, 1, '2020-02-11 11:05:05');
INSERT INTO `checks` VALUES (15, 2017010104, 1, '2020-02-12 11:05:15');
INSERT INTO `checks` VALUES (16, 2017010104, 1, '2020-02-13 11:05:24');
INSERT INTO `checks` VALUES (17, 2017010101, 1, '2020-02-17 11:45:53');
INSERT INTO `checks` VALUES (18, 2017010101, 1, '2020-01-22 11:01:54');
INSERT INTO `checks` VALUES (19, 2017010101, 1, '2020-02-18 11:01:54');
COMMIT;

-- ----------------------------
-- Table structure for classes
-- ----------------------------
DROP TABLE IF EXISTS `classes`;
CREATE TABLE `classes` (
  `c_id` int NOT NULL AUTO_INCREMENT,
  `t_id` int DEFAULT NULL,
  `c_name` varchar(25) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`c_id`),
  KEY `fk_classes_teachers_1` (`t_id`),
  CONSTRAINT `fk_classes_teachers_1` FOREIGN KEY (`t_id`) REFERENCES `teachers` (`t_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of classes
-- ----------------------------
BEGIN;
INSERT INTO `classes` VALUES (1, 1, '计科一班');
INSERT INTO `classes` VALUES (2, 2, '医信班');
INSERT INTO `classes` VALUES (3, 3, '信管班');
INSERT INTO `classes` VALUES (4, 4, '计科二班');
COMMIT;

-- ----------------------------
-- Table structure for sdept
-- ----------------------------
DROP TABLE IF EXISTS `sdept`;
CREATE TABLE `sdept` (
  `sdept_id` int NOT NULL AUTO_INCREMENT,
  `sdept_name` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `sdept_loc` varchar(100) COLLATE utf8_bin DEFAULT NULL,
  `sdept_tel` varchar(11) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`sdept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of sdept
-- ----------------------------
BEGIN;
INSERT INTO `sdept` VALUES (1, '信息科学与工程学院', '三教三楼', '88485568');
INSERT INTO `sdept` VALUES (2, '人文与管理学院', '三教五楼', '88485567');
COMMIT;

-- ----------------------------
-- Table structure for sdept_class
-- ----------------------------
DROP TABLE IF EXISTS `sdept_class`;
CREATE TABLE `sdept_class` (
  `sdc_id` int NOT NULL AUTO_INCREMENT,
  `sdept_id` int DEFAULT NULL,
  `c_id` int DEFAULT NULL,
  PRIMARY KEY (`sdc_id`),
  KEY `fk_sdept_class_sdept_1` (`sdept_id`),
  KEY `fk_sdept_class_classes_1` (`c_id`),
  CONSTRAINT `fk_sdept_class_classes_1` FOREIGN KEY (`c_id`) REFERENCES `classes` (`c_id`),
  CONSTRAINT `fk_sdept_class_sdept_1` FOREIGN KEY (`sdept_id`) REFERENCES `sdept` (`sdept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of sdept_class
-- ----------------------------
BEGIN;
INSERT INTO `sdept_class` VALUES (1, 1, 1);
INSERT INTO `sdept_class` VALUES (2, 1, 2);
INSERT INTO `sdept_class` VALUES (3, 1, 3);
INSERT INTO `sdept_class` VALUES (4, 1, 4);
COMMIT;

-- ----------------------------
-- Table structure for select_stu_2month
-- ----------------------------
DROP TABLE IF EXISTS `select_stu_2month`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `select_stu_2month` AS select `students`.`s_id` AS `s_id`,`students`.`s_name` AS `s_name`,`c`.`c_name` AS `c_name`,`checks`.`check_time` AS `check_time` from (((`students` join `stu_class` `sc` on((`students`.`s_id` = `sc`.`s_id`))) join `classes` `c` on((`sc`.`c_id` = `c`.`c_id`))) join `checks` on(((`checks`.`s_id` = `students`.`s_id`) and (`checks`.`c_id` = `c`.`c_id`))));

-- ----------------------------
-- Records of select_stu_2month
-- ----------------------------
BEGIN;
INSERT INTO `select_stu_2month` VALUES (2017010101, '张三', '计科一班', '2020-02-10 11:01:54');
INSERT INTO `select_stu_2month` VALUES (2017010101, '张三', '计科一班', '2020-02-11 11:02:18');
INSERT INTO `select_stu_2month` VALUES (2017010101, '张三', '计科一班', '2020-02-12 11:02:34');
INSERT INTO `select_stu_2month` VALUES (2017010101, '张三', '计科一班', '2020-02-13 11:02:52');
INSERT INTO `select_stu_2month` VALUES (2017010101, '张三', '计科一班', '2020-02-17 11:45:53');
INSERT INTO `select_stu_2month` VALUES (2017010101, '张三', '计科一班', '2020-01-22 11:01:54');
INSERT INTO `select_stu_2month` VALUES (2017010101, '张三', '计科一班', '2020-02-18 11:01:54');
INSERT INTO `select_stu_2month` VALUES (2017010102, '李四', '计科一班', '2020-02-10 11:03:14');
INSERT INTO `select_stu_2month` VALUES (2017010102, '李四', '计科一班', '2020-02-11 11:03:27');
INSERT INTO `select_stu_2month` VALUES (2017010102, '李四', '计科一班', '2020-02-12 11:03:39');
INSERT INTO `select_stu_2month` VALUES (2017010102, '李四', '计科一班', '2020-02-13 11:03:51');
INSERT INTO `select_stu_2month` VALUES (2017010103, '王五', '计科一班', '2020-02-10 11:04:02');
INSERT INTO `select_stu_2month` VALUES (2017010103, '王五', '计科一班', '2020-02-11 11:04:15');
INSERT INTO `select_stu_2month` VALUES (2017010103, '王五', '计科一班', '2020-02-12 11:04:26');
INSERT INTO `select_stu_2month` VALUES (2017010103, '王五', '计科一班', '2020-02-13 11:04:45');
INSERT INTO `select_stu_2month` VALUES (2017010104, '赵柳', '计科一班', '2020-02-10 11:04:56');
INSERT INTO `select_stu_2month` VALUES (2017010104, '赵柳', '计科一班', '2020-02-11 11:05:05');
INSERT INTO `select_stu_2month` VALUES (2017010104, '赵柳', '计科一班', '2020-02-12 11:05:15');
INSERT INTO `select_stu_2month` VALUES (2017010104, '赵柳', '计科一班', '2020-02-13 11:05:24');
COMMIT;

-- ----------------------------
-- Table structure for stu_class
-- ----------------------------
DROP TABLE IF EXISTS `stu_class`;
CREATE TABLE `stu_class` (
  `sc_id` int NOT NULL AUTO_INCREMENT,
  `s_id` int DEFAULT NULL,
  `c_id` int DEFAULT NULL,
  PRIMARY KEY (`sc_id`),
  KEY `fk_stu_class_students_1` (`s_id`),
  KEY `fk_stu_class_classes_1` (`c_id`),
  CONSTRAINT `fk_stu_class_classes_1` FOREIGN KEY (`c_id`) REFERENCES `classes` (`c_id`),
  CONSTRAINT `fk_stu_class_students_1` FOREIGN KEY (`s_id`) REFERENCES `students` (`s_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of stu_class
-- ----------------------------
BEGIN;
INSERT INTO `stu_class` VALUES (1, 2017010101, 1);
INSERT INTO `stu_class` VALUES (2, 2017010102, 1);
INSERT INTO `stu_class` VALUES (3, 2017010103, 1);
INSERT INTO `stu_class` VALUES (4, 2017010104, 1);
COMMIT;

-- ----------------------------
-- Table structure for stu_class_sdept
-- ----------------------------
DROP TABLE IF EXISTS `stu_class_sdept`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_class_sdept` AS select `students`.`s_id` AS `学号`,`students`.`s_name` AS `姓名`,`c`.`c_name` AS `班级`,`sdept`.`sdept_name` AS `院系` from ((((`students` join `stu_class` `sc` on((`students`.`s_id` = `sc`.`s_id`))) join `classes` `c` on((`c`.`c_id` = `sc`.`c_id`))) join `sdept_class` on((`sdept_class`.`c_id` = `c`.`c_id`))) join `sdept` on((`sdept`.`sdept_id` = `sdept_class`.`sdept_id`)));

-- ----------------------------
-- Records of stu_class_sdept
-- ----------------------------
BEGIN;
INSERT INTO `stu_class_sdept` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院');
INSERT INTO `stu_class_sdept` VALUES (2017010102, '李四', '计科一班', '信息科学与工程学院');
INSERT INTO `stu_class_sdept` VALUES (2017010103, '王五', '计科一班', '信息科学与工程学院');
INSERT INTO `stu_class_sdept` VALUES (2017010104, '赵柳', '计科一班', '信息科学与工程学院');
COMMIT;

-- ----------------------------
-- Table structure for stu_class_sdept_check
-- ----------------------------
DROP TABLE IF EXISTS `stu_class_sdept_check`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_class_sdept_check` AS select `students`.`s_id` AS `学号`,`students`.`s_name` AS `姓名`,`c`.`c_name` AS `班级`,`sdept`.`sdept_name` AS `院系`,`checks`.`check_time` AS `打卡时间` from (((((`students` join `stu_class` `sc` on((`students`.`s_id` = `sc`.`s_id`))) join `classes` `c` on((`c`.`c_id` = `sc`.`c_id`))) join `sdept_class` on((`sdept_class`.`c_id` = `c`.`c_id`))) join `sdept` on((`sdept`.`sdept_id` = `sdept_class`.`sdept_id`))) join `checks` on(((`students`.`s_id` = `checks`.`s_id`) and (`c`.`c_id` = `checks`.`c_id`))));

-- ----------------------------
-- Records of stu_class_sdept_check
-- ----------------------------
BEGIN;
INSERT INTO `stu_class_sdept_check` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院', '2020-02-10 11:01:54');
INSERT INTO `stu_class_sdept_check` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院', '2020-02-11 11:02:18');
INSERT INTO `stu_class_sdept_check` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院', '2020-02-12 11:02:34');
INSERT INTO `stu_class_sdept_check` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院', '2020-02-13 11:02:52');
INSERT INTO `stu_class_sdept_check` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院', '2020-02-17 11:45:53');
INSERT INTO `stu_class_sdept_check` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院', '2020-01-22 11:01:54');
INSERT INTO `stu_class_sdept_check` VALUES (2017010101, '张三', '计科一班', '信息科学与工程学院', '2020-02-18 11:01:54');
INSERT INTO `stu_class_sdept_check` VALUES (2017010102, '李四', '计科一班', '信息科学与工程学院', '2020-02-10 11:03:14');
INSERT INTO `stu_class_sdept_check` VALUES (2017010102, '李四', '计科一班', '信息科学与工程学院', '2020-02-11 11:03:27');
INSERT INTO `stu_class_sdept_check` VALUES (2017010102, '李四', '计科一班', '信息科学与工程学院', '2020-02-12 11:03:39');
INSERT INTO `stu_class_sdept_check` VALUES (2017010102, '李四', '计科一班', '信息科学与工程学院', '2020-02-13 11:03:51');
INSERT INTO `stu_class_sdept_check` VALUES (2017010103, '王五', '计科一班', '信息科学与工程学院', '2020-02-10 11:04:02');
INSERT INTO `stu_class_sdept_check` VALUES (2017010103, '王五', '计科一班', '信息科学与工程学院', '2020-02-11 11:04:15');
INSERT INTO `stu_class_sdept_check` VALUES (2017010103, '王五', '计科一班', '信息科学与工程学院', '2020-02-12 11:04:26');
INSERT INTO `stu_class_sdept_check` VALUES (2017010103, '王五', '计科一班', '信息科学与工程学院', '2020-02-13 11:04:45');
INSERT INTO `stu_class_sdept_check` VALUES (2017010104, '赵柳', '计科一班', '信息科学与工程学院', '2020-02-10 11:04:56');
INSERT INTO `stu_class_sdept_check` VALUES (2017010104, '赵柳', '计科一班', '信息科学与工程学院', '2020-02-11 11:05:05');
INSERT INTO `stu_class_sdept_check` VALUES (2017010104, '赵柳', '计科一班', '信息科学与工程学院', '2020-02-12 11:05:15');
INSERT INTO `stu_class_sdept_check` VALUES (2017010104, '赵柳', '计科一班', '信息科学与工程学院', '2020-02-13 11:05:24');
COMMIT;

-- ----------------------------
-- Table structure for students
-- ----------------------------
DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
  `s_id` int NOT NULL AUTO_INCREMENT,
  `s_name` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `s_sex` varchar(5) COLLATE utf8_bin DEFAULT NULL,
  `s_grade` int DEFAULT NULL,
  `s_loc` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`s_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2017010105 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of students
-- ----------------------------
BEGIN;
INSERT INTO `students` VALUES (2017010101, '张三', '男', 2017, '湖南长沙');
INSERT INTO `students` VALUES (2017010102, '李四', '男', 2017, '湖南常德');
INSERT INTO `students` VALUES (2017010103, '王五', '女', 2017, '湖南岳阳');
INSERT INTO `students` VALUES (2017010104, '赵柳', '女', 2017, '湖南衡阳');
COMMIT;

-- ----------------------------
-- Table structure for teachers
-- ----------------------------
DROP TABLE IF EXISTS `teachers`;
CREATE TABLE `teachers` (
  `t_id` int NOT NULL AUTO_INCREMENT,
  `t_name` varchar(10) COLLATE utf8_bin DEFAULT NULL,
  `t_tel` varchar(11) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`t_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of teachers
-- ----------------------------
BEGIN;
INSERT INTO `teachers` VALUES (1, '王老师', '13107342356');
INSERT INTO `teachers` VALUES (2, '刘老师', '13107314569');
INSERT INTO `teachers` VALUES (3, '罗老师', '12456892356');
INSERT INTO `teachers` VALUES (4, '陈老师', '12545784521');
COMMIT;

-- ----------------------------
-- Table structure for v_class_check
-- ----------------------------
DROP TABLE IF EXISTS `v_class_check`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_class_check` AS select `c`.`c_id` AS `c_id`,`c`.`c_name` AS `c_name`,`students`.`s_id` AS `s_id`,`students`.`s_name` AS `s_name`,`checks`.`check_time` AS `check_time` from (((`classes` `c` join `stu_class` `sc` on((`sc`.`c_id` = `c`.`c_id`))) join `students` on((`students`.`s_id` = `sc`.`s_id`))) join `checks` on(((`checks`.`c_id` = `c`.`c_id`) and (`checks`.`s_id` = `students`.`s_id`))));

-- ----------------------------
-- Records of v_class_check
-- ----------------------------
BEGIN;
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010101, '张三', '2020-02-10 11:01:54');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010101, '张三', '2020-02-11 11:02:18');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010101, '张三', '2020-02-12 11:02:34');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010101, '张三', '2020-02-13 11:02:52');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010101, '张三', '2020-02-17 11:45:53');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010101, '张三', '2020-01-22 11:01:54');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010101, '张三', '2020-02-18 11:01:54');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010102, '李四', '2020-02-10 11:03:14');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010102, '李四', '2020-02-11 11:03:27');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010102, '李四', '2020-02-12 11:03:39');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010102, '李四', '2020-02-13 11:03:51');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010103, '王五', '2020-02-10 11:04:02');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010103, '王五', '2020-02-11 11:04:15');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010103, '王五', '2020-02-12 11:04:26');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010103, '王五', '2020-02-13 11:04:45');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010104, '赵柳', '2020-02-10 11:04:56');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010104, '赵柳', '2020-02-11 11:05:05');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010104, '赵柳', '2020-02-12 11:05:15');
INSERT INTO `v_class_check` VALUES (1, '计科一班', 2017010104, '赵柳', '2020-02-13 11:05:24');
COMMIT;

-- ----------------------------
-- View structure for select_stu_2month
-- ----------------------------
DROP VIEW IF EXISTS `select_stu_2month`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `select_stu_2month` AS select `students`.`s_id` AS `s_id`,`students`.`s_name` AS `s_name`,`c`.`c_name` AS `c_name`,`checks`.`check_time` AS `check_time` from (((`students` join `stu_class` `sc` on((`students`.`s_id` = `sc`.`s_id`))) join `classes` `c` on((`sc`.`c_id` = `c`.`c_id`))) join `checks` on(((`checks`.`s_id` = `students`.`s_id`) and (`checks`.`c_id` = `c`.`c_id`))));

-- ----------------------------
-- View structure for stu_class_sdept
-- ----------------------------
DROP VIEW IF EXISTS `stu_class_sdept`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_class_sdept` AS select `students`.`s_id` AS `学号`,`students`.`s_name` AS `姓名`,`c`.`c_name` AS `班级`,`sdept`.`sdept_name` AS `院系` from ((((`students` join `stu_class` `sc` on((`students`.`s_id` = `sc`.`s_id`))) join `classes` `c` on((`c`.`c_id` = `sc`.`c_id`))) join `sdept_class` on((`sdept_class`.`c_id` = `c`.`c_id`))) join `sdept` on((`sdept`.`sdept_id` = `sdept_class`.`sdept_id`)));

-- ----------------------------
-- View structure for stu_class_sdept_check
-- ----------------------------
DROP VIEW IF EXISTS `stu_class_sdept_check`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_class_sdept_check` AS select `students`.`s_id` AS `学号`,`students`.`s_name` AS `姓名`,`c`.`c_name` AS `班级`,`sdept`.`sdept_name` AS `院系`,`checks`.`check_time` AS `打卡时间` from (((((`students` join `stu_class` `sc` on((`students`.`s_id` = `sc`.`s_id`))) join `classes` `c` on((`c`.`c_id` = `sc`.`c_id`))) join `sdept_class` on((`sdept_class`.`c_id` = `c`.`c_id`))) join `sdept` on((`sdept`.`sdept_id` = `sdept_class`.`sdept_id`))) join `checks` on(((`students`.`s_id` = `checks`.`s_id`) and (`c`.`c_id` = `checks`.`c_id`))));

-- ----------------------------
-- View structure for v_class_check
-- ----------------------------
DROP VIEW IF EXISTS `v_class_check`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_class_check` AS select `c`.`c_id` AS `c_id`,`c`.`c_name` AS `c_name`,`students`.`s_id` AS `s_id`,`students`.`s_name` AS `s_name`,`checks`.`check_time` AS `check_time` from (((`classes` `c` join `stu_class` `sc` on((`sc`.`c_id` = `c`.`c_id`))) join `students` on((`students`.`s_id` = `sc`.`s_id`))) join `checks` on(((`checks`.`c_id` = `c`.`c_id`) and (`checks`.`s_id` = `students`.`s_id`))));

SET FOREIGN_KEY_CHECKS = 1;


创建视图方便查询:具体代码可以结合Java项目中DAO包的代码。

--1  给某个学生记录 某天的考勤信息(增)
INSERT INTO checks (s_id,c_id,check_time) VALUES (2017010101,01,'2020-01-22 11:01:54');

SELECT CURRENT_TIME();

SELECT NOW();

--2  查询某个班的 某天的所有考勤信息(查)
SELECT c.c_id,c.c_name,students.s_id,students.s_name,checks.check_time
FROM classes c
INNER JOIN stu_class sc ON sc.c_id = c.c_id
INNER JOIN students ON students.s_id = sc.s_id
INNER JOIN checks on checks.c_id = c.c_id AND checks.s_id = students.s_id
WHERE c.c_name='计科一班' AND checks.check_time LIKE '%2020-02-13%';

CREATE VIEW v_class_check AS
SELECT c.c_id,c.c_name,students.s_id,students.s_name,checks.check_time
FROM classes c
INNER JOIN stu_class sc ON sc.c_id = c.c_id
INNER JOIN students ON students.s_id = sc.s_id
INNER JOIN checks on checks.c_id = c.c_id AND checks.s_id = students.s_id

SELECT * FROM v_class_check WHERE c_name ='计科一班' AND check_time LIKE '%2020-02-17%';


--3  查某个学生 2 月的所有考勤情况(查)
SELECT students.s_id, students.s_name, c.c_name, checks.check_time
FROM students
INNER JOIN stu_class sc ON students.s_id = sc.s_id
INNER JOIN classes c ON sc.c_id = c.c_id
INNER JOIN checks ON checks.s_id = students.s_id AND checks.c_id = c.c_id

CREATE VIEW select_stu_2month AS 
SELECT students.s_id, students.s_name, c.c_name, checks.check_time
FROM students
INNER JOIN stu_class sc ON students.s_id = sc.s_id
INNER JOIN classes c ON sc.c_id = c.c_id
INNER JOIN checks ON checks.s_id = students.s_id AND checks.c_id = c.c_id


SELECT * FROM select_stu_2month WHERE s_name = '张三' AND check_time LIKE '%2020-02%';


SQL表结构
基于阿里数据库连接池Druid和Apache封装工具类DBUtil的JDBC实现对数据库视图的查询和表的增删改查操作_第2张图片基于阿里数据库连接池Druid和Apache封装工具类DBUtil的JDBC实现对数据库视图的查询和表的增删改查操作_第3张图片

5. 结语

如果读者有什么不明白的可以私聊或者评论,看到了都会耐心仔细回复。
学而不思则罔,思而不学则殆。共勉!  

具体项目代码可以移步我的码云

你可能感兴趣的:(Java)