java学生信息管理系统项目实训2

这是一个Java实现的学生信息管理系统,包括登录注册、系统设置、数据操作(增删查改)、查询统计等功能。系统使用了实体类如College、Status、Student、User,并实现了对应的数据访问接口及其实现,通过数据库进行数据存储和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本系统包括登录注册、系统设置(设置学校信息、设置状态栏信息、修改用户密码)、数据操作(增加学生记录、编辑学生记录、删除学生记录(按学号删除、按姓名删除、按班级删除、按系部删除)、浏览学生记录)、查询学生(按学号查询、按姓名查询、按班级查询、按系部查询、按性别查询)、人数统计(按性别统计人数、按班级统计人数、按系部统计人数)等功能。
在这里插入图片描述
src下面包含
net.xsp.student:
在这里插入图片描述
package net.xsp.student.bean;

/**

  • 包名:net.xsp.student.bean
  • 类名:College
  • 描述:学校实体
  • 作者:向仕平
  • 日期:2019年6月17日
    */

import java.util.Date;

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 + '\'' +
            '}';
}

}

package net.xsp.student.bean;

/**

  • 包名:net.xsp.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 + '\'' +
            '}';
}

}

package net.xsp.student.bean;

/**

  • 包名:net.xsp.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 + '\'' +
            '}';
}

}

package net.xsp.student.bean;

/**

  • 包名:net.xsp.student.bean
  • 类名:User
  • 描述:
  • 作者:向仕平
  • 日期:2019年6月17日
    */

import java.util.Date;

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 +
            '}';
}

}

在这里插入图片描述
package net.xsp.student.dao;

import net.xsp.student.bean.College;

/**

  • 功能:学校数据访问接口
  • 作者:向仕平
  • 日期:2019年6月17日
    */
    public interface CollegeDao {
    College findById(int id);
    int update(College college);
    }

package net.xsp.student.dao;

import net.xsp.student.bean.Status;

/**

  • 功能:状态数据访问接口
  • 作者:向仕平
  • 日期:2019年6月17日
    */
    public interface StatusDao {
    Status findById(int id);
    int update(Status status);
    }

package net.xsp.student.dao;

import net.xsp.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();
    }

package net.xsp.student.dao;

import net.xsp.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);
    }

在这里插入图片描述
package net.xsp.student.dbutil;

/**

  • 包名:net.xsp.student.dbuti
  • 类名:ConnectionManager
  • 描述:数据库连接管理类
  • 作者:向仕平
  • 日期:2019年6月17日
    */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

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);
}

}

在这里插入图片描述
package net.xsp.student.dao.impl;

import net.xsp.student.bean.College;
import net.xsp.student.dao.CollegeDao;
import net.xsp.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;
    

    }
    }

package net.xsp.student.dao.impl;

/**

  • 包名:net.xsp.student.dao.impl
  • 类名:StatusDaoImpl
  • 描述:状态数据访问接口实现类
  • 作者:向仕平
  • 日期:2019年6月18日
    */

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

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

public class StatusDaoImpl implements StatusDao {

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

    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_status WHERE id = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setInt(1, id);
        // 执行SQL查询,返回结果集
        ResultSet rs = pstmt.executeQuery();
        // 判断结果集是否有记录
        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"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回状态对象
    return status;
}

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

    // 获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "UPDATE t_status SET college = ?, version = ?, author = ?,"
            + " telephone = ?, address = ?, email = ? WHERE id = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        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());
        // 执行更新操作,更新记录
        count = pstmt.executeUpdate();
        // 关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

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

}

package net.xsp.student.dao.impl;

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

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

/**

  • 功能:学生数据访问接口实现类

  • 作者:向仕平

  • 日期:2019年6月18日
    /
    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;

      // 1. 获取数据库连接对象
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_student where id = ?”;
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setString(1, id);
      // 5. 执行SQL,返回结果集
      ResultSet rs = pstmt.executeQuery();
      // 6. 判断结果集是否有记录
      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();

      // 1. 获取数据库连接对象
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_student where name like ?”;
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setString(1, name + “%”);
      // 5. 执行SQL,返回结果集
      ResultSet rs = pstmt.executeQuery();
      // 6. 遍历结果集
      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);
      }
      // 7. 关闭结果集
      rs.close();
      // 8. 关闭预备语句对象
      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();

      // 1. 获取数据库连接对象
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_student where class like ?”;
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setString(1, clazz + “%”);
      // 5. 执行SQL,返回结果集
      ResultSet rs = pstmt.executeQuery();
      // 6. 遍历结果集
      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);
      }
      // 7. 关闭结果集
      rs.close();
      // 8. 关闭预备语句对象
      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();

      // 1. 获取数据库连接对象
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_student where department like ?”;
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setString(1, department + “%”);
      // 5. 执行SQL,返回结果集
      ResultSet rs = pstmt.executeQuery();
      // 6. 遍历结果集
      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);
      }
      // 7. 关闭结果集
      rs.close();
      // 8. 关闭预备语句对象
      pstmt.close();
      } catch (SQLException e) {
      e.printStackTrace();
      } finally {
      // 关闭数据库连接
      ConnectionManager.closeConnection(conn);
      }

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

    /**

    • 查询全部学生记录

    • @return 学生列表
      */
      @Override
      public List findAll() {
      // 声明学生列表
      List students = new ArrayList();

      // 1. 获取数据库连接对象
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_student”;
      try {
      // 3. 创建语句对象
      Statement stmt = conn.createStatement();
      // 4. 执行SQL,返回结果集
      ResultSet rs = stmt.executeQuery(strSQL);
      // 5. 遍历结果集
      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);
      }
      // 6. 关闭结果集
      rs.close();
      // 7. 关闭语句对象
      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;
      }
      }

package net.xsp.student.dao.impl;

import net.xsp.student.bean.Student;
import net.xsp.student.bean.User;
import net.xsp.student.dao.UserDao;
import net.xsp.student.dbutil.ConnectionManager;

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

/**

  • 功能:用户数据访问接口实现类

  • 作者:向仕平

  • 日期:2019年6月19日
    /
    public class UserDaoImpl implements UserDao {
    /
    *

    • 插入用户记录

    • @param user

    • @return 插入记录数
      */
      @Override
      public int insert(User user) {
      // 定义插入记录数
      int count = 0;

      // 1. 获得数据库连接
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “insert into t_user (username, password, telephone, register_time)”
      + " values (?, ?, ?, ?)";
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setString(1, user.getUsername());
      pstmt.setString(2, user.getPassword());
      pstmt.setString(3,user.getTelephone());
      pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
      // 5. 执行SQL,返回插入记录数
      count = pstmt.executeUpdate();
      // 6. 关闭预备语句对象
      pstmt.close();
      } catch (SQLException e) {
      e.printStackTrace();
      } finally {
      // 关闭数据库连接
      ConnectionManager.closeConnection(conn);
      }

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

    /**

    • 按id删除用户记录

    • @param id

    • @return 删除记录数
      */
      @Override
      public int deleteById(int id) {
      // 定义删除记录数
      int count = 0;

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

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

    /**

    • 更新用户记录

    • @param user

    • @return 更新记录数
      */
      @Override
      public int update(User user) {
      // 定义更新记录数
      int count = 0;

      // 1. 获得数据库连接
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “update t_user set username = ?, password = ?, telephone = ?,”
      + " register_time = ? where id = ?";
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setString(1, user.getUsername());
      pstmt.setString(2, user.getPassword());
      pstmt.setString(3, user.getTelephone());
      pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
      pstmt.setInt(5, user.getId());
      // 5. 执行SQL,返回更新记录数
      count = pstmt.executeUpdate();
      // 6. 关闭预备语句对象
      pstmt.close();
      } catch (SQLException e) {
      e.printStackTrace();
      } finally {
      // 关闭数据库连接
      ConnectionManager.closeConnection(conn);
      }

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

    /**

    • 按id查询用户

    • @param id

    • @return 用户实体
      */
      @Override
      public User findById(int id) {
      // 声明用户对象
      User user = null;

      // 1. 获取数据库连接对象
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_user where id = ?”;
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setInt(1, id);
      // 5. 执行SQL,返回结果集
      ResultSet rs = pstmt.executeQuery();
      // 6. 判断结果集是否有记录
      if (rs.next()) {
      // 创建用户实体
      user = new User();
      // 利用当前记录各字段值设置用户实体属性
      user.setId(rs.getInt(“id”));
      user.setUsername(rs.getString(“username”));
      user.setPassword(rs.getString(“password”));
      user.setTelephone(rs.getString(“telephone”));
      user.setRegisterTime(rs.getTimestamp(“register_time”));
      }
      } catch (SQLException e) {
      e.printStackTrace();
      } finally {
      // 关闭数据库连接
      ConnectionManager.closeConnection(conn);
      }

      // 返回用户对象
      return user;
      }

    /**

    • 查询所有用户

    • @return 用户列表
      */
      @Override
      public List findAll() {
      // 声明用户列表
      List users = new ArrayList();

      // 1. 获取数据库连接对象
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_user”;
      try {
      // 3. 创建语句对象
      Statement stmt = conn.createStatement();
      // 4. 执行SQL,返回结果集
      ResultSet rs = stmt.executeQuery(strSQL);
      // 5. 遍历结果集
      while (rs.next()) {
      // 创建用户实体
      User user = new User();
      // 利用当前记录各字段值设置用户实体属性
      user.setId(rs.getInt(“id”));
      user.setUsername(rs.getString(“username”));
      user.setPassword(rs.getString(“password”));
      user.setTelephone(rs.getString(“telephone”));
      user.setRegisterTime(rs.getTimestamp(“register_time”));
      // 将实体添加到用户列表
      users.add(user);
      }
      // 6. 关闭结果集
      rs.close();
      // 7. 关闭语句对象
      stmt.close();
      } catch (SQLException e) {
      e.printStackTrace();
      } finally {
      // 关闭数据库连接
      ConnectionManager.closeConnection(conn);
      }

      // 返回用户列表
      return users;
      }

    /**

    • 用户登录

    • @param username

    • @param password

    • @return 登录用户实体
      */
      @Override
      public User login(String username, String password) {
      // 声明用户对象
      User user = null;

      // 1. 获取数据库连接
      Connection conn = ConnectionManager.getConnection();
      // 2. 定义SQL字符串
      String strSQL = “select * from t_user where username = ? and password = ?”;
      try {
      // 3. 创建预备语句对象
      PreparedStatement pstmt = conn.prepareStatement(strSQL);
      // 4. 设置占位符的值
      pstmt.setString(1, username);
      pstmt.setString(2, password);
      // 5. 执行SQL,返回结果集
      ResultSet rs = pstmt.executeQuery();
      // 6. 判断结果集是否有记录
      if (rs.next()) {
      // 实例化用户
      user = new User();
      // 利用当前记录各字段值设置用户实体属性
      user.setId(rs.getInt(“id”));
      user.setUsername(rs.getString(“username”));
      user.setPassword(rs.getString(“password”));
      user.setTelephone(rs.getString(“telephone”));
      user.setRegisterTime(rs.getTimestamp(“register_time”));
      }
      } catch (SQLException e) {
      e.printStackTrace();
      } finally {
      // 关闭数据库连接
      ConnectionManager.closeConnection(conn);
      }

      // 返回用户对象
      return user;
      }
      }

测试类:
测试类
测试类
测试类
package net.xsp.student.test;

import net.xsp.student.bean.User;
import net.xsp.student.dao.UserDao;
import net.xsp.student.dao.impl.UserDaoImpl;
import org.junit.Test;

/**

  • 功能:测试用户数据访问接口实现类

  • 作者:向仕平

  • 日期:2019年6月19日
    */
    public class TestUserDaoImpl {
    @Test
    public void testFindById() {
    UserDao dao = new UserDaoImpl();
    User user = dao.findById(1);
    System.out.println(user);
    }

    @Test
    public void testLogin() {
    UserDao dao = new UserDaoImpl();
    String username, password;

     username = "admin";
     password = "12345";
    
     User user = dao.login(username, password);
    
     if (user != null) {
         System.out.println("恭喜,用户名与密码正确,登录成功!");
     } else {
         System.out.println("遗憾,用户名或密码错误,登录失败!");
     }
    

    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值