Java实训——学生信息管理系统

本学期的Java基础学习已经告一段落,进入了为期一周多的实训周

本次Java实训的项目 制作一个 学生信息管理系统

                                                             学生信息管理系统具体要求如下

Java实训——学生信息管理系统_第1张图片

Java实训——学生信息管理系统_第2张图片

 

老师给了我们 项目所需要的 student.sql文件 帮助文档.chm   images图片文件  mysql-connector-java-5.1.16-bin.jar

bean 实体类

app 应用程序类

dao 数据访问接口 impl 数据访问接口实现类

gui 窗口界面类(JFrame)

ConnectionManager 数据库连接管理类

service 服务接口 impl 服务接口实现类

text 测试类

Java实训——学生信息管理系统_第3张图片

学校实体College

package net.lyh.student.bean;

import java.util.Date;

/**
 * 功能:College 实体类
 * 作者:李永豪
 * 日期:2019年6月17日
 */


public class College {

    /**
     * 学校标识符
     */
    private int id;
    /**
     * 学校名称
     */
    private String name;
    /**
     * 校长
     */
    private String president;
    /**
     * 建校时间
     */
    private Date startTime;
    /**
     * 联系电话
     */
    private String telephone;
    /**
     * 电子邮箱
     */
    private String email;
    /**
     * 通信地址
     */
    private String address;
    /**
     * 学校简介
     */
    private String profile;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPresident() {
        return president;
    }

    public void setPresident(String president) {
        this.president = president;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    @Override
    public String toString() {
        return "College{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", president='" + president + '\'' +
                ", startTime=" + startTime +
                ", telephone='" + telephone + '\'' +
                ", email='" + email + '\'' +
                ", address='" + address + '\'' +
                ", profile='" + profile + '\'' +
                '}';
    }
}

状态实体Status

package net.lyh.student.bean;

/**
 * 功能:Status 实体类
 * 作者:李永豪
 * 日期:2019年6月17日
 */


public class Status {

    /**
     * 标识符
     */
    private int id;
    /**
     * 校名
     */
    private String college;
    /**
     * 版本
     */
    private String version;
    /**
     * 作者
     */
    private String author;
    /**
     * 联系电话
     */
    private String telephone;
    /**
     * 通信地址
     */
    private String address;
    /**
     * 电子邮箱
     */
    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Status{" +
                "id=" + id +
                ", college='" + college + '\'' +
                ", version='" + version + '\'' +
                ", author='" + author + '\'' +
                ", telephone='" + telephone + '\'' +
                ", address='" + address + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

学生实体Student

package net.lyh.student.bean;

/**
 * 功能:Student 实体类
 * 作者:李永豪
 * 日期:2019年6月17日
 */


public class Student {

    /**
     * 学号
     */
    private String id;
    /**
     * 姓名
     */
    private String name;
    /**
     * 性别
     */
    private String sex;
    /**
     * 年龄
     */
    private int age;
    /**
     * 系部
     */
    private String department;
    /**
     * 班级
     */
    private String clazz;
    /**
     * 联系电话
     */
    private String telephone;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", department='" + department + '\'' +
                ", clazz='" + clazz + '\'' +
                ", telephone='" + telephone + '\'' +
                '}';
    }
}

用户实体User

package net.lyh.student.bean;

import java.util.Date;

/**
 * 功能:User 实体类
 * 作者:李永豪
 * 日期:2019年6月17日
 */

public class User {

    /**
     * 用户标识符
     */
    private int id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 联系电话
     */
    private String telephone;
    /**
     * 注册时间
     */
    private Date registerTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", telephone='" + telephone + '\'' +
                ", registerTime=" + registerTime +
                '}';
    }
}

ConnectionManager 数据库连接管理类

package net.lyh.student.dbutil;

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 功能:数据库连接管理类
 * 作者:李永豪
 * 日期:2019年6月17日
 */


public class ConnectionManager {

    /**
     * 数据库驱动程序
     */
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    /**
     * 数据库统一资源标识符
     */
    private static final String URL = "jdbc:mysql://localhost:3306/student";
    /**
     * 数据库用户名
     */
    private static final String USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "1";

    /**
     * 私有化构造方法,拒绝实例化
     */
    private ConnectionManager() {
    }

    /**
     * 获得数据库连接
     *
     * @return 数据库连接对象
     */
    public static Connection getConnection() {
        // 定义数据库连接
        Connection conn = null;
        try {
            // 安装数据库驱动程序
            Class.forName(DRIVER);
            // 获得数据库连接
            conn = DriverManager.getConnection(URL
                    + "?useUnicode=true&characterEncoding=UTF8", USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 返回数据库连接
        return conn;
    }

    /**
     * 关闭数据库连接
     *
     * @param conn
     */
    public static void closeConnection(Connection conn) {
        // 判断数据库连接是否为空
        if (conn != null) {
            // 判断数据库连接是否关闭
            try {
                if (!conn.isClosed()) {
                    // 关闭数据库连接
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试数据库连接是否成功
     *
     * @param args
     */
    public static void main(String[] args) {
        // 获得数据库连接
        Connection conn = getConnection();
        // 判断是否连接成功
        if (conn != null) {
            JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!");
        } else {
            JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!");
        }

        // 关闭数据库连接
        closeConnection(conn);
    }
}

这个数据库连接管理类 需要注意以下两点

Java实训——学生信息管理系统_第4张图片

学校数据访问接口CollegeDao

package net.lyh.student.dao;

import net.lyh.student.bean.College;

/**
 * 功能:学校数据访问接口
 * 作者:李永豪
 * 日期:2019年6月17日
 */

public interface CollegeDao {
    College findById (int id);
    int update (College college);
}

状态数据访问接口StatusDao

package net.lyh.student.dao;

import net.lyh.student.bean.Status;

/**
 * 功能:状态数据访问接口
 * 作者:李永豪
 * 日期:2019年6月17日
 */

public interface StatusDao {
    Status findById(int id);
    int update(Status status);
}

学生数据访问接口StudentDao

package net.lyh.student.dao;

import net.lyh.student.bean.Student;

import java.util.List;
import java.util.Vector;

/**
 * 功能:学生数据访问接口
 * 作者:李永豪
 * 日期:2019年6月17日
 */

public interface StudentDao {
    int insert(Student student);
    int deleteById(String id);
    int deleteByClass(String clazz);
    int deleteByDepartment(String department);
    int update(Student student);
    Student findById(String id);
    List findByName(String name);
    List findByClass(String clazz);
    List findByDepartment(String department);
    List findAll();
    Vector findRowsBySex();
    Vector findRowsByClass();
    Vector findRowsByDepartment();
}

用户数据访问接口UserDao

package net.lyh.student.dao;

import net.lyh.student.bean.User;

import java.util.List;

/**
 * 功能:用户数据访问接口
 * 作者:李永豪
 * 日期:2019年6月17日
 */

public interface UserDao {
    int insert(User user);
    int deleteById(int id);
    int update(User user);
    User findById(int id);
    List findAll();
    User login(String username,String password);
}

学校数据访问接口实现类CollegeDaoImpl

package net.lyh.student.dao.impl;

import net.lyh.student.bean.College;
import net.lyh.student.dao.CollegeDao;
import net.lyh.student.dbutil.ConnectionManager;

import java.sql.*;

/**
 * 功能:学校数据访问接口实现类
 * 作者:李永豪
 * 日期:2019年6月17日
 */

public class CollegeDaoImpl implements CollegeDao {
    @Override
    public College findById(int id) {
        // 声明学校对象
        College college = null;

        //1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        //2.定义SQL字符串
        String strSQL = "select * from t_college where id = ?";
        try {
            //3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            //4.设置占位符的值
            pstmt.setInt(1,id);
            //5.执行SQL,返回结果集
            ResultSet rs = pstmt.executeQuery();
            //6.判断结果集是否有记录
            if (rs.next()){
                // 实例化学校对象
                college = new College();
                // 利用当前记录字段值去设置学校对象的属性
                college.setId(rs.getInt("id"));
                college.setName(rs.getString("name"));
                college.setPresident(rs.getString("president"));
                college.setStartTime(rs.getDate("start_time"));
                college.setTelephone(rs.getString("telephone"));
                college.setEmail(rs.getString("email"));
                college.setAddress(rs.getString("address"));
                college.setProfile(rs.getString("profile"));
            }
            // 7. 关闭预备语句对象
            pstmt.close();
            // 8. 关闭结果集对象
            rs.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回学校对象
        return college;
    }

    @Override
    public int update(College college) {
        // 定义更新记录数
        int count = 0;

        // 1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL字符串
        String strSQL = "update t_college set name = ? , president = ? , start_time = ? ,"
                + "telephone = ? , email = ? ,profile = ? where id = ?";
        try {
            //3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            //4.设置占位符的值
            pstmt.setString(1,college.getName());
            pstmt.setString(2,college.getPresident());
            pstmt.setTimestamp(3,new Timestamp(college.getStartTime().getTime()));
            pstmt.setString(4,college.getTelephone());
            pstmt.setString(5,college.getEmail());
            pstmt.setString(6,college.getProfile());
            pstmt.setInt(7,college.getId());
            // 5.执行SQL,返回更新记录数
            count = pstmt.executeUpdate();
            // 6.关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }


        // 返回更新记录数
        return count;
    }
}

测试类TestCollegeDaoImpl

package net.lyh.student.test;

import net.lyh.student.bean.College;
import net.lyh.student.dao.CollegeDao;
import net.lyh.student.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 功能:测试学校数据访问接口实现类
 * 作者:李永豪
 * 日期:2019年6月17日
 */

public class TestCollegeDaoImpl {
    @Before
    public void beforeTest(){
        System.out.println("单元测试开始了");
    }
    @Test
    public void testFindById(){
        CollegeDao dao = new CollegeDaoImpl();
        College college = dao.findById(1);
        System.out.println(college);
    }

    @Test
    public void tesUpdate(){
        CollegeDao dao = new CollegeDaoImpl();
        College college = dao.findById(1);
        college.setPresident("王洪礼");
        dao.update(college);
        college = dao.findById(1);
        System.out.println(college);
    }

    @After
    public void afterTest(){
        System.out.println("单元测试结束了");
    }
}

利用JUnit4,不仅可以对每一个加了@Test注解的方法单独进行测试,还可以对整个类的所有加了@Test注解的方法一起进行测试。只须将鼠标移到类名的前一行,单击右键即可选择相应的命令:

状态数据访问接口实现类StatusDaoImpl

package net.lyh.student.dao.impl;

import net.lyh.student.bean.Status;
import net.lyh.student.dao.StatusDao;
import net.lyh.student.dbutil.ConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 功能
 * 作者:李永豪
 * 日期:2019年6月18日
 */

public class StatusDaoImpl implements StatusDao {


    @Override
    public Status findById(int id) {
        // 声明状态对象
        Status status = null;

        // 1.获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL字符串
        String strSQL = "select * from t_status where id = ? ";
        try {
            // 3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4.设置占位符的值
            pstmt.setInt(1, id);
            // 5.执行SQl查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 6.判断结果集是否有记录
            if (rs.next()) {
                // 实例化状态
                status = new Status();
                // 利用当前记录字段去设置状态对象的属性
                status.setId(rs.getInt("id"));
                status.setCollege(rs.getString("college"));
                status.setVersion(rs.getString("version"));
                status.setAuthor(rs.getString("author"));
                status.setTelephone(rs.getString("telephone"));
                status.setAddress(rs.getString("address"));
                status.setEmail(rs.getString("email"));
            }
            // 7. 关闭预备语句对象
            pstmt.close();
            // 8. 关闭结果集对象
            rs.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回状态对象
        return status;
    }

    @Override
    public int update(Status status) {
        // 定义更新记录数
        int count = 0;

        // 1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL字符串
        String strSQL = "update t_status set college = ? , version = ? ,author = ?,"
                + "telephone = ? , address = ? , email = ? where id = ?";
        try {
            // 3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4.设置占位符的值
            pstmt.setString(1,status.getCollege());
            pstmt.setString(2,status.getVersion());
            pstmt.setString(3,status.getAuthor());
            pstmt.setString(4,status.getTelephone());
            pstmt.setString(5,status.getAddress());
            pstmt.setString(6,status.getEmail());
            pstmt.setInt(7,status.getId());
            // 5. 执行更新操作,更新记录
            count = pstmt.executeUpdate();
            // 6. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回更新记录数
        return count;
    }
}

测试类TestStatusDaoImpl

package net.lyh.student.test;

import net.lyh.student.bean.Status;
import net.lyh.student.dao.StatusDao;
import net.lyh.student.dao.impl.StatusDaoImpl;
import org.junit.Test;

/**
 * 功能测试状态数据访问接口实现类
 * 作者:李永豪
 * 日期:2019年6月18日
 */

public class TestStatusDaoImpl {
    @Test
    public void testFindById(){
        StatusDao dao = new StatusDaoImpl();
        Status status = dao.findById(1);
        System.out.println(status);
    }

    @Test
    public void tesUpdate(){
        StatusDao dao = new StatusDaoImpl();
        Status status = dao.findById(1);
        status.setAuthor("无心剑");
        status.setTelephone("13956783456");
        status.setEmail("[email protected]");
        dao.update(status);
        status = dao.findById(1);
        System.out.println(status);
    }
}

学生数据访问接口实现类StudentDaoImpl

package net.lyh.student.dao.impl;

/**
 * 功能:学生数据访问接口实现类
 * 作者:李永豪
 * 日期:2019年6月18日
 */

import net.lyh.student.bean.Student;
import net.lyh.student.dao.StudentDao;
import net.lyh.student.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;


public class StudentDaoImpl implements StudentDao {
    /**
     * 插入学生记录
     *
     * @param student
     * @return 插入记录数
     */
    @Override
    public int insert(Student student) {
        //定义插入记录数
        int count = 0;

        // 1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL字符串
        String strSQL = "insert into t_student (id, name, sex, age, department, class, telephone)"
                + "values(?,?,?,?,?,?,?)";
        try {
            // 3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4.设置占位符的值
            pstmt.setString(1, student.getId());
            pstmt.setString(2, student.getName());
            pstmt.setString(3, student.getSex());
            pstmt.setInt(4, student.getAge());
            pstmt.setString(5, student.getDepartment());
            pstmt.setString(6, student.getClazz());
            pstmt.setString(7, student.getTelephone());
            // 5.执行SQL返回插入记录数
            count = pstmt.executeUpdate();
            // 6.关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        //返回插入记录数
        return count;
    }

    /**
     * 按学号删除学生记录
     *
     * @param id
     * @return 删除记录数
     */
    @Override
    public int deleteById(String id) {
        // 定义删除记录数
        int count = 0;

        // 1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL字符串
        String strSQL = "delete from t_student where id = ? ";
        try {
            // 3. 创建预备对象语句
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符
            pstmt.setString(1, id);
            // 5.执行SQL,删除记录
            count = pstmt.executeUpdate();
            // 6. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回删除记录数
        return count;
    }

    /**
     * 按班级删除学生记录
     *
     * @param clazz
     * @return 删除记录数
     */
    @Override
    public int deleteByClass(String clazz) {

        // 定义删除记录数
        int count = 0;

        // 1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL字符串
        String strSQL = "delete from t_student where class = ? ";
        try {
            // 3.创建预备对象语句
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4.设置占位符的值
            pstmt.setString(1, clazz);
            // 5.执行SQL,删除记录
            count = pstmt.executeUpdate();
            // 6.关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }


        // 返回删除记录数
        return count;
    }

    /**
     * 按系部删除学生记录
     *
     * @param department
     * @return 删除记录数
     */
    @Override
    public int deleteByDepartment(String department) {

        //定义删除记录数
        int count = 0;

        // 1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL字符串
        String strSQL = "delete from t_student where department = ? ";
        try {
            // 3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4.设置占位符的值
            pstmt.setString(1, department);
            // 5.执行SQL语句,删除记录
            count = pstmt.executeUpdate();
            // 6.关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回删除记录数
        return count;
    }

    /**
     * 更新学生记录
     *
     * @param student
     * @return 更新记录数
     */
    @Override
    public int update(Student student) {
        // 定义更新记录数
        int count = 0;

        // 1.获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2.定义SQL语句
        String strSQL = "update t_student set name = ? , sex = ? , age = ? , "
                + "department = ? ,class = ? ,telephone = ? where id = ? ";
        try {
            // 3.创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4.设置占位符的值
            pstmt.setString(1, student.getName());
            pstmt.setString(2, student.getSex());
            pstmt.setInt(3, student.getAge());
            pstmt.setString(4, student.getDepartment());
            pstmt.setString(5, student.getClazz());
            pstmt.setString(6, student.getTelephone());
            pstmt.setString(7, student.getId());
            // 5.执行SQL语句,更新记录
            count = pstmt.executeUpdate();
            // 6.关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回更新记录数
        return count;
    }

    /**
     * 按学号查询学生记录
     *
     * @param id
     * @return 学生实体
     */
    @Override
    public Student findById(String id) {

        // 声明学生
        Student student = null;

        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "select * from t_student where id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, id);
            // 执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 判断结果集是否有记录
            if (rs.next()) {
                // 创建学生实体
                student = new Student();
                // 设置实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        // 返回学生
        return student;
    }

    /**
     * 按学号查询学生记录
     *
     * @param name
     * @return 学生列表
     */
    @Override
    public List findByName(String name) {

        // 声明用户列表
        List students = new ArrayList();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "select * from t_student where name like ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, name + "%");
            // 执行SQL语句,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 设置实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回学生列表
        return students;
    }

    /**
     *按班级查询学生记录
     *
     * @param clazz
     * @return 学生列表
     */
    @Override
    public List findByClass(String clazz) {

        // 声明用户列表
        List students = new ArrayList();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "select * from t_student where class like ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, clazz + "%");
            // 执行SQL语句,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 设置实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回学生列表
        return students;
    }

    /**
     * 按系部查询学生记录
     * @param department
     * @return
     */
    @Override
    public List findByDepartment(String department) {
        // 声明用户列表
        List students = new ArrayList();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "select * from t_student where department like ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, department + "%");
            // 执行SQL语句,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 设置实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回学生列表
        return students;
    }

    /**
     * 查询全部学生记录
     *
     * @return 学生列表
     */
    @Override
    public List findAll() {
        // 声明用户列表
        List students = new ArrayList();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "select * from t_student";
        try {
            // 创建语句对象
            Statement stmt = conn.createStatement();
            // 执行SQL语句,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 设置实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回学生列表
        return students;
    }


    /**
     * 按性别统计学生人数
     *
     * @return 统计结果向量
     */
    @Override
    public Vector findRowsBySex() {
        // 定义行集向量
        Vector rows = new Vector();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select sex as '性别', count(*) as '人数'"
                + " from t_student group by sex order by sex desc";
        try {
            // 3. 创建语句对象
            Statement stmt = conn.createStatement();
            // 4. 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 5. 遍历结果集
            while (rs.next()) {
                // 定义当前行向量
                Vector currentRow = new Vector();
                // 利用当前记录字段值设置当前行向量的元素值
                currentRow.addElement(rs.getString("性别"));
                currentRow.addElement(rs.getInt("人数") + "");
                // 将当前行向量添加到行集向量
                rows.addElement(currentRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回行集向量
        return rows;
    }

    /**
     * 按班级统计学生人数
     *
     * @return 统计结果向量
     */
    @Override
    public Vector findRowsByClass() {
        // 定义行集向量
        Vector rows = new Vector();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select class as '班级', count(*) as '人数'"
                + " from t_student group by class order by class desc";
        try {
            // 3. 创建语句对象
            Statement stmt = conn.createStatement();
            // 4. 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 5. 遍历结果集
            while (rs.next()) {
                // 定义当前行向量
                Vector currentRow = new Vector();
                // 利用当前记录字段值设置当前行向量的元素值
                currentRow.addElement(rs.getString("班级"));
                currentRow.addElement(rs.getInt("人数") + "");
                // 将当前行向量添加到行集向量
                rows.addElement(currentRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回行集向量
        return rows;
    }

    /**
     * 按系部统计学生人数
     *
     * @return 统计结果向量
     */
    @Override
    public Vector findRowsByDepartment() {
        // 定义行集向量
        Vector rows = new Vector();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select department as '系部', count(*) as '人数'"
                + " from t_student group by department order by department desc";
        try {
            // 3. 创建语句对象
            Statement stmt = conn.createStatement();
            // 4. 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 5. 遍历结果集
            while (rs.next()) {
                // 定义当前行向量
                Vector currentRow = new Vector();
                // 利用当前记录字段值设置当前行向量的元素值
                currentRow.addElement(rs.getString("系部"));
                currentRow.addElement(rs.getInt("人数") + "");
                // 将当前行向量添加到行集向量
                rows.addElement(currentRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回行集向量
        return rows;
    }
}

测试类TestStudentDaoImpl

package net.lyh.student.test;

import net.lyh.student.bean.Student;
import net.lyh.student.dao.StudentDao;
import net.lyh.student.dao.impl.StudentDaoImpl;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * 功能:测试学生数据访问接口实现类
 * 作者:李永豪
 * 日期:2019年6月18日
 */

public class TestStudentDaoImpl {
    @Test
    public void TestInsert() {
        Student student = new Student();
        student.setId("18242029");
        student.setName("李永豪");
        student.setSex("男");
        student.setAge(19);
        student.setDepartment("信息工程系");
        student.setClazz("大数据一班");
        student.setTelephone("18121978354");

        StudentDao dao = new StudentDaoImpl();
        int count = dao.insert(student);
        if (count > 0) {
            System.out.println("恭喜,学生记录插入成功!");
        } else {
            System.out.println("遗憾,学生记录插入失败!");
        }
    }

    @Test
    public void TestDeleteById() {
        StudentDao dao = new StudentDaoImpl();
        String id = "18242029";
        int count = dao.deleteById(id);
        if (count > 0) {
            System.out.println("恭喜,学生记录删除成功!");
        } else {
            System.out.println("遗憾,学生记录删除失败!");
        }
    }

    @Test
    public void TestDeleteByClass() {
        StudentDao dao = new StudentDaoImpl();
        String clazz = "大数据一班";
        int count = dao.deleteByClass(clazz);
        if (count > 0) {
            System.out.println("恭喜,[" + clazz + "] 学生记录删除成功!");
        } else {
            System.out.println("遗憾,[" + clazz + "] 学生记录删除失败!");
        }
    }

    @Test
    public void TestDeleteByDepartment() {
        StudentDao dao = new StudentDaoImpl();
        String department = "信息工程系";
        int count = dao.deleteByDepartment(department);
        if (count > 0) {
            System.out.println("恭喜,[" + department + "] 学生记录删除成功!");
        } else {
            System.out.println("遗憾,[" + department + "] 学生记录删除失败!");
        }
    }

    @Test
    public void TestUpDateStudent(){
        StudentDao dao = new StudentDaoImpl();
        Student student = dao.findById("18242029");
        student.setName("罗利");
        student.setTelephone("18121978064");
        student.setClazz("会计三班");
        student.setAge(20);
        student.setSex("女");
        student.setDepartment("商学院");
        int count = dao.update(student);
        if (count > 0){
            System.out.println("恭喜,学生记录更新成功!");
            student = dao.findById("18242029");
            System.out.println(student);
        }else{
            System.out.println("遗憾,学生记录更新失败!");
        }

    }


    @Test
    public void TestFindByName() {
        StudentDao dao = new StudentDaoImpl();
        String name = "李";
        List students = dao.findByName(name);
        if (students.size() > 0) {
            for (Student student : students) {
                System.out.println(student);
            }
        } else {
            System.out.println("温馨提示:查无此人!");
        }
    }

    @Test
    public void TestFindByClass() {
        StudentDao dao = new StudentDaoImpl();
        String clazz = "大数据一班";
        List students = dao.findByClass(clazz);
        if (students.size() > 0) {
            for (Student student : students) {
                System.out.println(student);
            }
        } else {
            System.out.println("温馨提示:查无此人!");
        }
    }

    @Test
    public void FindByDepartment() {
        StudentDao dao = new StudentDaoImpl();
        String department = "信息工程系";
        List students = dao.findByDepartment(department);
        if (students.size() > 0) {
            for (Student student : students) {
                System.out.println(student);
            }
        } else {
            System.out.println("温馨提示:查无此人!");
        }
    }

    @Test
    public void TestFindAll() {

    }

    @Test
    public void TestFindRowsBySex() {
        StudentDao dao = new StudentDaoImpl();
        Vector rows = dao.findRowsBySex();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    @Test
    public void TestFindRowsByClass() {
        StudentDao dao = new StudentDaoImpl();
        Vector rows = dao.findRowsByClass();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    @Test
    public void TestFindRowsByDepartment() {
        StudentDao dao = new StudentDaoImpl();
        Vector rows = dao.findRowsByDepartment();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

 

Java实训——学生信息管理系统_第5张图片

 

测试类写完,进入对GUI包的功能的写,代码就不放在这里了。

 

Java实训——学生信息管理系统_第6张图片

gui 包里写完后,运行LoginFrame,打开学生信息管理系统。

首先我们运行登录窗口,进行注册。

发现第一个问题:注册时,用户名与密码为空也可以注册。

然后我们对非管理员用户(罗利)进行了登录,

发现了第二个问题:对于非管理员用户(罗利)可以更改学校信息

然后我们登录管理员用户(李永豪),

发现了第三个问题::关闭添加学生信息时,学生信息管理系统也随之关闭。

针对发现的三个问题:我们对代码进行了更改。

第一个问题:添加一个判断语句,判断用户名与密码是否为空

Java实训——学生信息管理系统_第7张图片

第二个问题:非管理员用户可以修改学校信息。

Java实训——学生信息管理系统_第8张图片

对非管理员用户进行对学校信息修改的限制

第三个问题:关闭子类窗口,父类窗口也随之关闭。

Java实训——学生信息管理系统_第9张图片

将gui包里子类中的窗口默认关闭操作中的EXIT_ON_CLOSE改为DISPOSE_ON_CLOSE。

现在再次运行学生信息管理系统:

再次运行代码,注册时,当用户名与密码为空,会提示你用户名与密码不能为空。非管理员用户(罗利)也不能修改学校信息。关闭子类窗口,也不会关闭父类窗口了。

总结:本次实训出勤率100%。上课也很认真的听讲。

在这为期一周的实训中,自己的收获很大,使自己对JAVA编程这门语言对文件的命名的规范、对程序控制结构、面向对象编程、图形用户界面、Java数据库应用、MySQL数据库等有了更深更系统的认识。在这个实训期间,遇到很多问题与错误,通过在网上查,请教老师,请问同学。在与同学的交流、讨论中增长了知识也增加了友谊。

 

你可能感兴趣的:(Java实训——学生信息管理系统)