实训第一步
利用百度脑图构建总体框架。

第二步创建数据库

创建相关的表


第三步在表中插入数据!

第四实现步骤

在lib里添加链接MySQL数据库的jar包

在images中添加图片

第五步创建相关的类

学校实体College
/**

  • 学校实体

  • yh

  • */
    package net.yh.student.dbutil.bean;
    import java.util.Date;
    public class College {
    private int id;
    // 学校标识符
    private String name;
    // 学校名称
    private String president;
    // 校长
    private Date startTime;
    // 建校时间
    private String address;
    //地址
    private String telephone;
    // 联系电话
    private String email;
    // 邮箱
    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 getAddress() {
    return address;
    }

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

    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 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.yh.student.dbutil.bean;
    //状态实体
    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;}@Overridepublic  String toString(){return "Status [id="+id+",college="+college+",version="+version+",author="+author+",telephone="+telephone+",address="+address+",email="+email+"]";}
    

    }

学生类Student
package net.yh.student.dbutil.bean;
//学生实体
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+",departmen=t"+department+",clazz="+clazz+",telephone="+telephone+"]";}

}
用户类User
/**

  • */
    package net.yh.student.dbutil.bean;

import java.util.Date;

public class User {
private int id;
//用户标识符
private String usernname;
//用户名
private String password;
//密码
private String telephone;
//电话
private Date registerTime;
//注册时间

public int getId() {return id;
}public void setId(int id) {this.id = id;
}public String getUsernname() {return usernname;
}public void setUsernname(String usernname) {this.usernname = usernname;
}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+",usernname="+usernname+",password="+password+",telephone="+telephone+",registerTime"+registerTime+"]";}

}

第六步数据访问接口

CollegeDao
package net.yh.student.dbutil.dao;

import net.yh.student.dbutil.bean.College;
public interface CollegeDao {
College findById(int id);
int update(College college);

}

StatusDao
package net.yh.student.dbutil.dao;
import net.yh.student.dbutil.bean.Status;
public interface StatusDao {
Status findById(int id);
int update(Status status);
}

StudentDao
package net.yh.student.dbutil.dao;
import net.yh.student.dbutil.bean.Student;

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

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<Student> findByName(String name);
List<Student> findByClass(String Clazz);
List<Student> findByDepartment(String department);
List<Student> findAll();
Vector findRowsBysex();Vector findRowsBySex();Vector findRowsByClass();
Vector findRowsByDepartment();

}

UserDao

package net.yh.student.dbutil.dao;

import net.yh.student.dbutil.bean.User;

import java.util.List;

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

}
第七步创建端口类

CollegeDaoImpl

package net.yh.student.dbutil.dao.impl;

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

import java.sql.*;

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

}

StatusDaoImpl

package net.yh.student.dbutil.dao.impl;

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

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

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

}

StudentdaoImpl

package net.yh.student.dbutil.dao.impl;

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

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

/**

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

  • 作者:yh

  • 日期: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;
      }

    @Override
    public Vector findRowsBysex() {
    return null;
    }

    /**

    • 按性别统计学生人数

    • @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;
      }

}

UserDaoImpl

package net.yh.student.dbutil.dao.impl;

import net.yh.student.dbutil.bean.User;
import net.yh.student.dbutil.dao.UserDao;
import net.yh.student.dbutil.ConnectionManager;

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

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

/**

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

  • 作者:yh

  • 日期: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.getUsernname());
      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.getUsernname());
      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.setUsernname(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.setUsernname(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.setUsernname(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.yh.student.dbutil;

/**

  • 描述:数据库连接管理类

*/

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

}

创建服务类

CollegeService

package net.yh.student.dbutil.service;

import net.yh.student.dbutil.bean.College;

public interface CollegeService{
College findCollegeById(int id);
int updateCollege(College college);
}

StatusService

package net.yh.student.dbutil.service;

import net.yh.student.dbutil.bean.Status;

public interface StatusService{
Status findStatusById(int id);
int updateStatus(Status status);
}

StudentService

package net.yh.student.dbutil.service;
import net.yh.student.dbutil.bean.Student;

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

public interface StudentService{

int addStudent(Student student);
int deleteStudentById(String id);
int deleteStudentsByClass(String clazz);
int deleteStudentsByDepartment(String department);
int updateStudent(Student student);
Student findStudentById(String id);
List<Student>findStudentsByName(String name);
List<Student>findStudentsByClass(String clazz);
List<Student>findStudentsByDepartment(String Department);
List<Student>findAllStudents();Vector findRowByClass();Vector findRowByDepartment();Vector findRowsBySex();
Vector findRowsByClass();Vector findRowsByDepartment();

}

UserService

package net.yh.student.dbutil.service;

import net.yh.student.dbutil.bean.User;

import java.util.List;

public interface UserService{
int addUser(User user);
int deleteUserById(int id);
int updateUser(User user);
User findUserById(int id);
List findAllUsers();
User login(String username, String password);
}

创建测试类


package net.yh.student.dbutil.test;

import net.yh.student.dbutil.dao.CollegeDao;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestCollegeDaoImpl {
@Before
public void beforeTest(){
System.out.println(“单元测试开始了~”);
}

@Test
public void testFindByID(){CollegeDao dao =new net.yh.student.dbutil.dao.impl.CollegeDaoImpl();net.yh.student.dbutil.bean.College college=dao.findById(1);System.out.println(college);
}
@Test
public void testUpdate(){CollegeDao dao =new net.yh.student.dbutil.dao.impl.CollegeDaoImpl();net.yh.student.dbutil.bean.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("单元测试结束~");
}

}
测试结果



package net.yh.student.dbutil.test;

import net.yh.student.dbutil.bean.College;
import net.yh.student.dbutil.service.CollegeService;
import net.yh.student.dbutil.service.impl.CollegeServiceImpl;
import org.junit.Test;

public class TestCollegeServiceImpl {
@Test
public void testFindCollegeById(){
CollegeService service=new CollegeServiceImpl();
College college=service.findCollegeById(1);
System.out.println(college);

}
@Test
public void testUpdateCollege(){CollegeService service=new CollegeServiceImpl();College college=service.findCollegeById(1);college.setPresident("王洪礼");college.setTelephone("3152639");int count=service.updateCollege(college);if (count>0){System.out.println("恭喜,学校记录更新成功");college=service.findCollegeById(1);System.out.println(college);}else {System.out.println("遗憾,学校记录更新失败");}
}

}



package net.yh.student.dbutil.test;

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

public class TestStatusDaoImpl {
@Test

public void testFindById(){StatusDao dao=new net.yh.student.dbutil.dao.impl.StatusDaoImpl();Status status=dao.findById(1);System.out.println(status);
}
@Test
public void testUpdate(){StatusDao dao =new StatusDaoImpl();Status status=dao.findById(1);status.setAuthor("yh");status.setTelephone("1558388****");status.setEmail("wuxinjian@163.com");dao.update(status);status=dao.findById(1);System.out.println(status);
}

}


package net.yh.student.dbutil.test;

import net.yh.student.dbutil.bean.Status;
import net.yh.student.dbutil.service.StatusService;
import net.yh.student.dbutil.service.impl.StatusServiceImpl;
import org.junit.Test;

public class TestStatusServicelmpl {
@Test
public void testFindStatuById(){
StatusService service=new StatusServiceImpl();
Status status=service.findStatusById(1);
System.out.println(status);
}

@Test
public void testUpdateStatus(){StatusService service=new StatusServiceImpl();Status status=service.findStatusById(1);status.setAuthor("YH");status.setTelephone("1500001111");int count =service.updateStatus(status);if(count>0){System.out.println("恭喜,状态记录更新成功");status=service.findStatusById(1);System.out.println(status);}else {System.out.println("遗憾,状态信息更新失败");}
}

}

package net.yh.student.dbutil.test;

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

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

public class TestStudentDaoImpl {
@Test
public void testInsert(){
Student student=new Student();
student.setId(“18101001”);
student.setName(“唐鹏”);
student.setSex(“男”);
student.setAge(19);
student.setDepartment(“信息工程系”);
student.setClazz(“18大数据1班”);
student.setTelephone(“12345678901”);

    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="18101001";int count=dao.deleteById(id);if (count>0){System.out.println("恭喜,学生记录删除成功");}else{System.out.println("遗憾,学生记录删除失败");}
}
@Test
public void testDeleteByClass(){StudentDao dao =new StudentDaoImpl() {@Overridepublic List<Student> findByDepartment(String department) {return null;}};String clazz="10英教1班";int count=dao.deleteByClass(clazz);if (count>0){System.out.println("恭喜,["+clazz+"]学生记录删除成功");}else {System.out.println("遗憾,["+clazz+"]学生记录删除失败");}
}@Test
public void testFindByName(){StudentDao dao = new StudentDaoImpl() {};String name="很";List<Student>students=dao.findByName(name);if (students.size()>0){for (Student student:students){System.out.println(student);}}   else {System.out.println("温馨提示:查无此人");}
}
@Test
public void testFindRowsBysex(){StudentDao dao=new StudentDaoImpl();Vector rows=dao.findRowsBysex();Iterator iterator=rows.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}
}

}


package net.yh.student.dbutil.test;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;
import org.junit.Test;

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

public class TestStudentServiceImpl {
@Test
public void TestInsert(){
Student student = new Student();
student.setId(“18201013”);
student.setName(“刘瑶”);
student.setSex(“女”);
student.setAge(20);
student.setClazz(“18软件3班”);
student.setDepartment(“信息工程学院”);
student.setTelephone(“15228054257”);

    StudentServiceImpl service = new StudentServiceImpl();int count = service.addStudent(student);if (count > 0){System.out.println("记录插入成功");}else {System.out.println("记录插入失败");}}@Test
public void TestDeleteById(){StudentServiceImpl service = new StudentServiceImpl();String id = "11040202";int count = service.deleteStudentById(id);if (count > 0){System.out.println("删除失败");}else {System.out.println("删除成功");}
}@Test
public void TestDeleteByClass(){StudentServiceImpl service = new StudentServiceImpl();String class01 = "11040202";int count = service.deleteStudentsByClass(class01);if (count > 0){System.out.println("删除失败");}else {System.out.println("删除成功");}
}@Testpublic void TestFIndById(){StudentService service = new StudentServiceImpl();Student student = service.findStudentById("11040204");System.out.println(student);}@Test
public void TestFindByName(){StudentServiceImpl service = new StudentServiceImpl();String name = "李";List<Student> students = service.findStudentsByName(name);if (students.size()>0){for (Student student:students){System.out.println(student);}}else {System.out.println("查无此人");}
}@Test
public void TestFindByDeoartment(){StudentServiceImpl service = new StudentServiceImpl();String department = "信息工程系";List<Student> students = service.findStudentsByDepartment(department);if (students.size() > 0) {for (Student student : students) {System.out.println(student);}} elseSystem.out.println("查找失败");
}@Test
public void testUpdate(){StudentServiceImpl service = new StudentServiceImpl();Student student = service.findStudentById("10080301");student.setName("huang");service.updateStudent(student);student = service.findStudentById("10080301");System.out.println(student);
}@Test
public void TestFindRowsBySex(){StudentServiceImpl service = new StudentServiceImpl();Vector rows = service.findRowsBySex();Iterator iterator = rows.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}@Test
public void findByClass() {StudentServiceImpl service = new StudentServiceImpl();String class01 = "11建工";List<Student> students = service.findStudentsByClass(class01);if (students.size() > 0) {for (Student student : students) {System.out.println(student);}} elseSystem.out.println("查无此班");
}@Test
public void findAll() {StudentServiceImpl service = new StudentServiceImpl();List<Student> students = service.findAllStudents();if (students.size() > 0) {for (Student student : students) {System.out.println(student);}} elseSystem.out.println("查找失败");
}@Test
public  void TestFindRowsByClass(){StudentServiceImpl service = new StudentServiceImpl();Vector rows = service.findRowsByClass();Iterator iterator = rows.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}@Test
public  void TestFindRowsByDepartment(){StudentServiceImpl service = new StudentServiceImpl();Vector rows = service.findRowsByDepartment();Iterator iterator = rows.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}private class Vectors {
}

}




package net.yh.student.dbutil.test;

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

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(“遗憾,用户名或密码错误,登录失败”);
// }
// }

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

}

package net.yh.student.dbutil.test;

import net.yh.student.dbutil.bean.User;
import net.yh.student.dbutil.service.UserService;
import net.yh.student.dbutil.service.impl.UserServiceImpl;
import org.junit.Test;

public class TestUserServiceImpl {
@Test
public void testLogin(){
UserService service=new UserServiceImpl();
String username,password;

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

}

GUI 界面部分

AddStudentFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;
import java.util.List;

public class AddStudentFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;

/*** 声明标签*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;/*** 声明文本框*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;/*** 声明按钮*/
private JButton btnExit;
private JButton btnOK;
private JButton btnCancel;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;/*** 构造方法** @param title*/
public AddStudentFrame(String title) {super(title);initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建组件panel = (JPanel) getContentPane();pnlCenter = new JPanel();pnlSouth = new JPanel();pnlRow1 = new JPanel();pnlRow2 = new JPanel();pnlRow3 = new JPanel();pnlRow4 = new JPanel();pnlRow5 = new JPanel();pnlRow6 = new JPanel();pnlRow7 = new JPanel();pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));lblId = new JLabel("学号:");lblName = new JLabel("姓名:");lblSex = new JLabel("性别:");lblAge = new JLabel("年龄:");lblDepartment = new JLabel("系部:");lblClass = new JLabel("班级:");lblTelephone = new JLabel("电话:");txtId = new JTextField(20);txtName = new JTextField(20);txtSex = new JTextField(20);txtAge = new JTextField(20);txtDepartment = new JTextField(20);txtClass = new JTextField(20);txtTelephone = new JTextField(20);btnOK = new JButton("确定[O]");btnCancel = new JButton("取消[C]");btnExit = new JButton("退出[X]");btnOK.setMnemonic(KeyEvent.VK_O);btnCancel.setMnemonic(KeyEvent.VK_C);btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.setLayout(new BorderLayout());panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlSouth, BorderLayout.SOUTH);pnlCenter.setLayout(new GridLayout(7, 1));pnlCenter.add(pnlRow1);pnlCenter.add(pnlRow2);pnlCenter.add(pnlRow3);pnlCenter.add(pnlRow4);pnlCenter.add(pnlRow5);pnlCenter.add(pnlRow6);pnlCenter.add(pnlRow7);pnlRow1.add(lblId);pnlRow1.add(txtId);pnlRow2.add(lblName);pnlRow2.add(txtName);pnlRow3.add(lblSex);pnlRow3.add(txtSex);pnlRow4.add(lblAge);pnlRow4.add(txtAge);pnlRow5.add(lblDepartment);pnlRow5.add(txtDepartment);pnlRow6.add(lblClass);pnlRow6.add(txtClass);pnlRow7.add(lblTelephone);pnlRow7.add(txtTelephone);pnlSouth.add(btnOK);pnlSouth.add(btnCancel);pnlSouth.add(btnExit);// 设置窗口属性setResizable(false);pack();setLocationRelativeTo(null);setVisible(true);// 【确定】按钮单击事件btnOK.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 创建学生对象Student student = new Student();student.setId(txtId.getText().trim());student.setName(txtName.getText().trim());student.setSex(txtSex.getText().trim());student.setAge(Integer.parseInt(txtAge.getText()));student.setDepartment(txtDepartment.getText().trim());student.setClazz(txtClass.getText().trim());student.setTelephone(txtTelephone.getText().trim());// 创建学生服务对象StudentService studentService = new StudentServiceImpl() {};// 添加学生记录int count = studentService.addStudent(student);// 判断是否添加成功if (count > 0) {JOptionPane.showMessageDialog(null, "添加记录成功!", "增加学生记录", JOptionPane.INFORMATION_MESSAGE);txtId.setText("");txtName.setText("");txtSex.setText("");txtAge.setText("");txtDepartment.setText("");txtClass.setText("");txtTelephone.setText("");txtId.requestFocus();} else {JOptionPane.showMessageDialog(null, "添加记录失败!", "增加学生记录", JOptionPane.ERROR_MESSAGE);}}});txtId.addFocusListener(new FocusListener() {@Overridepublic void focusLost(FocusEvent e) {if (txtId.getText().trim().equals("")) {JOptionPane.showMessageDialog(null, "学号不能为空!", "增加学生记录", JOptionPane.WARNING_MESSAGE);txtId.requestFocus();}}@Overridepublic void focusGained(FocusEvent e) {}});txtAge.addFocusListener(new FocusListener() {@Overridepublic void focusLost(FocusEvent e) {if (!isNumber(txtAge.getText().trim())) {JOptionPane.showMessageDialog(null, "注意:年龄全由数字构成!", "增加学生记录", JOptionPane.WARNING_MESSAGE);txtAge.setText("");txtAge.requestFocus();}}@Overridepublic void focusGained(FocusEvent e) {}});// 【取消】按钮单击事件btnCancel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {txtId.setText("");txtName.setText("");txtSex.setText("");txtAge.setText("");txtDepartment.setText("");txtClass.setText("");txtTelephone.setText("");txtId.requestFocus();}});// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();}});// 文本框按键事件txtId.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {if (!txtId.getText().trim().equals("")) {txtName.requestFocus();}}}});txtName.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtSex.requestFocus();}}});txtSex.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtAge.requestFocus();}}});txtAge.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtDepartment.requestFocus();}}});txtDepartment.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtClass.requestFocus();}}});txtClass.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtTelephone.requestFocus();}}});
}/*** 判断一个字符串是否全是数字** @param str* @return*/
boolean isNumber(String str) {for (int i = 0; i < str.length(); i++) {if (str.charAt(i) < '0' || str.charAt(i) > '9') {return false;}}return true;
}public static void main(String[] args) {new AddStudentFrame("");
}

}

BrowseStudentsFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Vector;

public class BrowseStudentsFrame extends JFrame {
/**
* 声明面板
/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
/
*
* 声明标签
/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/
*
* 声明文本框
/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/
*
* 声明按钮
*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;

/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;
/*** 创建学生服务对象*/
StudentService studentService;/*** 构造方法** @param title*/
public BrowseStudentsFrame(String title) {super(title);initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建组件panel = (JPanel) getContentPane();pnlCenter = new JPanel();pnlSouth = new JPanel();pnlRow1 = new JPanel();pnlRow2 = new JPanel();pnlRow3 = new JPanel();pnlRow4 = new JPanel();pnlRow5 = new JPanel();pnlRow6 = new JPanel();pnlRow7 = new JPanel();pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));lblId = new JLabel("学号:");lblName = new JLabel("姓名:");lblSex = new JLabel("性别:");lblAge = new JLabel("年龄:");lblDepartment = new JLabel("系部:");lblClass = new JLabel("班级:");lblTelephone = new JLabel("电话:");txtId = new JTextField(40);txtName = new JTextField(40);txtSex = new JTextField(40);txtAge = new JTextField(40);txtDepartment = new JTextField(40);txtClass = new JTextField(40);txtTelephone = new JTextField(40);txtId.setEditable(false);txtName.setEditable(false);txtSex.setEditable(false);txtAge.setEditable(false);txtDepartment.setEditable(false);txtClass.setEditable(false);txtTelephone.setEditable(false);btnTop = new JButton("第一条[T]");btnPrevious = new JButton("上一条[P]");btnNext = new JButton("下一条[N]");btnBottom = new JButton("最后一条[B]");btnExit = new JButton("退出[X]");btnTop.setMnemonic(KeyEvent.VK_T);btnPrevious.setMnemonic(KeyEvent.VK_P);btnNext.setMnemonic(KeyEvent.VK_N);btnBottom.setMnemonic(KeyEvent.VK_B);btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.setLayout(new BorderLayout());panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlSouth, BorderLayout.SOUTH);pnlCenter.setLayout(new GridLayout(7, 1));pnlCenter.add(pnlRow1);pnlCenter.add(pnlRow2);pnlCenter.add(pnlRow3);pnlCenter.add(pnlRow4);pnlCenter.add(pnlRow5);pnlCenter.add(pnlRow6);pnlCenter.add(pnlRow7);pnlRow1.add(lblId);pnlRow1.add(txtId);pnlRow2.add(lblName);pnlRow2.add(txtName);pnlRow3.add(lblSex);pnlRow3.add(txtSex);pnlRow4.add(lblAge);pnlRow4.add(txtAge);pnlRow5.add(lblDepartment);pnlRow5.add(txtDepartment);pnlRow6.add(lblClass);pnlRow6.add(txtClass);pnlRow7.add(lblTelephone);pnlRow7.add(txtTelephone);pnlSouth.add(btnTop);pnlSouth.add(btnPrevious);pnlSouth.add(btnNext);pnlSouth.add(btnBottom);pnlSouth.add(btnExit);// 设置窗口属性setSize(500, 300);// 设置窗口不可调整大小setResizable(false);// 设置窗口刚好容纳组件pack();// 设置窗口屏幕居中setLocationRelativeTo(null);// 设置窗口可见setVisible(true);// 设置窗口默认关闭操作setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 创建学生服务对象studentService = new StudentServiceImpl();// 获取全部学生列表students = studentService.findAllStudents();// 判断是否有学生记录if (students.size() > 0) {// 设置当前记录号currentRow = 1;// 设置窗口标题setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);// 填充窗口各文本框数据fillFrameData(currentRow);} else {JOptionPane.showMessageDialog(null, "表中没有记录!", "浏览学生表记录", JOptionPane.ERROR_MESSAGE);btnTop.setEnabled(false);btnPrevious.setEnabled(false);btnNext.setEnabled(false);btnBottom.setEnabled(false);}// 【第一条】按钮单击事件btnTop.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 设置当前记录号currentRow = 1;// 填充当前记录数据fillFrameData(currentRow);}});// 【上一条】按钮单击事件btnPrevious.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (currentRow > 1) {// 设置当前记录号currentRow--;// 填充当前记录数据fillFrameData(currentRow);} else {JOptionPane.showMessageDialog(null, "已到第一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);}}});// 【下一条】按钮单击事件btnNext.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (currentRow < students.size() - 1) {// 设置当前记录号currentRow++;// 填充当前记录数据fillFrameData(currentRow);} else {JOptionPane.showMessageDialog(null, "已到最后一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);}}});// 【最后一条】按钮单击事件btnBottom.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 设置当前记录号currentRow = students.size() - 1;// 填充当前记录数据fillFrameData(currentRow);}});// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();}});
}/*** 将当前记录数据填充窗口各文本框** @param currentRow*/
private void fillFrameData(int currentRow) {if (currentRow > 0) {setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);txtId.setText(students.get(currentRow).getId());txtName.setText(students.get(currentRow).getName());txtSex.setText(students.get(currentRow).getSex());txtAge.setText(students.get(currentRow).getAge() + "");txtDepartment.setText(students.get(currentRow).getDepartment());txtClass.setText(students.get(currentRow).getClazz());txtTelephone.setText(students.get(currentRow).getTelephone());}
}/*** 主方法** @param args*/
public static void main(String[] args) {new BrowseStudentsFrame("");
}

}

ChangePasswordFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.app.Application;
import net.yh.student.dbutil.bean.User;
import net.yh.student.dbutil.service.UserService;
import net.yh.student.dbutil.service.impl.UserServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;

public class ChangePasswordFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;

/*** 标签*/
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblNewPassword1;
private JLabel lblNewPassword2;/*** 文本框*/
private JTextField txtUsername;
private JPasswordField txtPassword;
private JPasswordField txtNewPassword1;
private JPasswordField txtNewPassword2;/*** 按钮*/
private JButton btnOK;
private JButton btnCancel;/*** 学生管理应用程序*/
private static Application app;
/*** 创建用户服务对象*/
private UserService userService;public ChangePasswordFrame(String title) {super(title);// 初始化用户界面initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建对象panel = (JPanel) getContentPane();pnlRow1 = new JPanel();pnlRow2 = new JPanel();pnlRow3 = new JPanel();pnlRow4 = new JPanel();pnlRow5 = new JPanel();lblUsername = new JLabel("用户名:");lblPassword = new JLabel("旧密码:");lblNewPassword1 = new JLabel("新密码:");lblNewPassword2 = new JLabel("确    认:");txtUsername = new JTextField(20);txtUsername.setEditable(false);txtPassword = new JPasswordField(20);txtNewPassword1 = new JPasswordField(20);txtNewPassword2 = new JPasswordField(20);btnOK = new JButton("确定[O]");btnOK.setMnemonic(KeyEvent.VK_O);btnCancel = new JButton("取消[C]");btnCancel.setMnemonic(KeyEvent.VK_C);// 添加组件panel.setLayout(new GridLayout(5, 1));panel.add(pnlRow1);panel.add(pnlRow2);panel.add(pnlRow3);panel.add(pnlRow4);panel.add(pnlRow5);pnlRow1.add(lblUsername);pnlRow1.add(txtUsername);pnlRow2.add(lblPassword);pnlRow2.add(txtPassword);pnlRow3.add(lblNewPassword1);pnlRow3.add(txtNewPassword1);pnlRow4.add(lblNewPassword2);pnlRow4.add(txtNewPassword2);pnlRow5.add(btnOK);pnlRow5.add(btnCancel);// 设置窗口属性pack();setVisible(true);setResizable(false);pack();setLocationRelativeTo(null);// 设置控件属性txtUsername.setEditable(false);txtUsername.setText(Application.username);// 注册监听器,实现监听器接口,编写事件处理代码// 取消按钮单击事件btnCancel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();}});// 确定按钮单击事件btnOK.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {changePassword();}});addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {dispose();}});txtUsername.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtPassword.requestFocus();}}});txtPassword.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtNewPassword1.requestFocus();}}});txtNewPassword1.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtNewPassword2.requestFocus();}}});txtNewPassword2.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {btnOK.requestFocus();}}});btnOK.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {changePassword();}}});
}/*** 修改密码的方法*/
private void changePassword() {int id = Application.id;String username = Application.username;String password = new String(txtPassword.getPassword());String newPassword1 = new String(txtNewPassword1.getPassword());String newPassword2 = new String(txtNewPassword2.getPassword());// 创建学生服务对象userService = new UserServiceImpl();if (userService.login(username, password) == null) {JOptionPane.showMessageDialog(null, "旧密码错误,请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);txtPassword.requestFocus();txtPassword.selectAll();} else if (newPassword1.equals("")) {JOptionPane.showMessageDialog(null, "新密码不能为空!", "错误提示", JOptionPane.ERROR_MESSAGE);txtNewPassword1.requestFocus();} else if (newPassword2.equals("")) {JOptionPane.showMessageDialog(null, "确认密码不能为空!", "错误提示", JOptionPane.ERROR_MESSAGE);txtNewPassword2.requestFocus();} else if (!newPassword1.equals(newPassword2)) {JOptionPane.showMessageDialog(null, "两次密码不一致,请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);txtNewPassword1.setText("");txtNewPassword2.setText("");txtNewPassword1.requestFocus();} else {// 按标识符获取用户User user = userService.findUserById(id);System.out.println(user);// 修改密码user.setPassword(newPassword1);// 更新用户信息int count = userService.updateUser(user);if (count > 0) {JOptionPane.showMessageDialog(null, "密码修改成功!", "设置密码", JOptionPane.INFORMATION_MESSAGE);dispose();} else {JOptionPane.showMessageDialog(null, "密码修改失败!", "设置密码", JOptionPane.WARNING_MESSAGE);}}
}public static void main(String[] args) {Application.id=1;Application.username="李刚";new ChangePasswordFrame("");
}

}

CountStudentsByClassFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;

public class CountStudentsByClassFrame extends JFrame {
/**
* 面板
/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/
*
* 按钮
/
private JButton btnPrint;
private JButton btnExit;
/
*
* 记录行集
/
private Vector rows;
/
*
* 表格列标题
/
private Vector colHead;
/
*
* 表格
/
private JTable table;
/
*
* 滚动面板
/
private JScrollPane scroller;
/
*
* 创建学生服务对象
*/
private StudentService studentService;

public CountStudentsByClassFrame(String title) {super(title);initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建组件panel = (JPanel) getContentPane();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();btnPrint = new JButton("打印[P]");btnPrint.setMnemonic(KeyEvent.VK_P);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnPrint);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());TitledBorder tb = new TitledBorder("统计结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl();// 获取按班级统计结果记录行集rows = studentService.findRowsByClass();// 设置表头colHead.add("班级");colHead.add("人数");// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);pnlCenter.add(scroller, BorderLayout.CENTER);repaint(); // 重绘窗体if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);}// 设置窗口大小setSize(300, 200);// 设置窗口不可调整大小setResizable(false);// 设置窗口屏幕居中setLocationRelativeTo(null);// 设置窗口标题setTitle("按班级统计学生人数");// 设置窗口可见setVisible(true);// 设置窗口默认关闭操作setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {dispose();}});// 【打印】按钮单击事件btnPrint.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {table.print();} catch (PrinterException e1) {e1.printStackTrace();}}});
}/*** 主方法** @param args*/
public static void main(String[] args) {new CountStudentsByClassFrame("");
}}

CountStudentsByDepartmentFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;

public class CountStudentsByDepartmentFrame extends JFrame {
/**
* 面板
/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/
*
* 按钮
/
private JButton btnPrint;
private JButton btnExit;
/
*
* 记录行集
/
private Vector rows;
/
*
* 表格列标题
/
private Vector colHead;
/
*
* 表格
/
private JTable table;
/
*
* 滚动面板
/
private JScrollPane scroller;
/
*
* 创建学生服务对象
*/
private StudentService studentService;

public CountStudentsByDepartmentFrame(String title) {super(title);initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建组件panel = (JPanel) getContentPane();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();btnPrint = new JButton("打印[P]");btnPrint.setMnemonic(KeyEvent.VK_P);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnPrint);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());TitledBorder tb = new TitledBorder("统计结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl();// 获取按系部统计结果记录行集rows = studentService.findRowsByDepartment();// 设置表头colHead.add("系部");colHead.add("人数");// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);pnlCenter.add(scroller, BorderLayout.CENTER);repaint(); // 重绘窗体if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);}// 设置窗口大小setSize(300, 200);// 设置窗口不可调整大小setResizable(false);// 设置窗口屏幕居中setLocationRelativeTo(null);// 设置窗口标题setTitle("按班级统计学生人数");// 设置窗口可见setVisible(true);// 设置窗口默认关闭操作setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {dispose();}});// 【打印】按钮单击事件btnPrint.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {table.print();} catch (PrinterException e1) {e1.printStackTrace();}}});
}/*** 主方法** @param args*/
public static void main(String[] args) {new CountStudentsByDepartmentFrame("");
}

}

CountStudentsBySexFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;

public class CountStudentsBySexFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;

/*** 按钮*/
private JButton btnPrint;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;
/*** 创建学生服务对象*/
private StudentService studentService;public CountStudentsBySexFrame(String title) {super(title);initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建对象panel = (JPanel) getContentPane();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();btnPrint = new JButton("打印[P]");btnPrint.setMnemonic(KeyEvent.VK_P);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnPrint);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());TitledBorder tb = new TitledBorder("统计结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取按性别统计结果记录行集rows = studentService.findRowsBySex();// 设置表头colHead.add("性别");colHead.add("人数");// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);pnlCenter.add(scroller, BorderLayout.CENTER);repaint(); // 重绘窗体if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);}// 设置窗口属性setSize(300, 200);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {dispose();}});// 【打印】按钮单击事件btnPrint.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {table.print();} catch (PrinterException e1) {e1.printStackTrace();}}});
}public static void main(String[] args) {new CountStudentsBySexFrame("");
}

}

DeleteStudentByidFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;

public class DeleteStudentByIdFrame extends JFrame {
/**
* 学号标签
/
private JLabel lblInputId;
/
*
* 学号文本框
*/
private JTextField txtId;

/*** 面板*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;/*** 按钮*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;
/*** 创建学生服务对象*/
private StudentService studentService;/*** 构造方法** @param title*/
public DeleteStudentByIdFrame(String title) {super(title);intiGUI();
}private void intiGUI() {// 创建对象panel = (JPanel) getContentPane();pnlNorth = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();lblInputId = new JLabel("输入学号:");txtId = new JTextField(10);txtId.setHorizontalAlignment(JTextField.CENTER);btnQuery = new JButton("查询[Q]");btnQuery.setMnemonic(KeyEvent.VK_Q);btnBrowseAll = new JButton("显示全部记录[A]");btnBrowseAll.setMnemonic(KeyEvent.VK_A);btnDelete = new JButton("删除查询的记录[D]");btnDelete.setEnabled(false);// 删除按钮不可用btnDelete.setMnemonic(KeyEvent.VK_D);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlNorth, BorderLayout.NORTH);pnlNorth.add(lblInputId);pnlNorth.add(txtId);pnlNorth.add(btnQuery);pnlNorth.add(btnBrowseAll);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnDelete);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());// 创建标题边框对象TitledBorder tb = new TitledBorder("查询结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 填充表格数据fillTableData();// 设置窗口属性setSize(600, 400);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {dispose();}});// 【删除】按钮单击事件btnDelete.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (!rows.isEmpty()) {long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");if (choice == JOptionPane.OK_OPTION) {// 获取待删学生学号String id = txtId.getText().trim();// 按学号删除学生int count = studentService.deleteStudentById(id);if (count > 0) {JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);// 重新获取全部学生列表students = studentService.findAllStudents();// 清空待删学生学号文本框txtId.setText("");// 填充数据fillTableData();// 删除按钮不可用btnDelete.setEnabled(false);} else {JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);}}}}});// 【查询】按钮单击事件btnQuery.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {doQuery();}});// 【显示全部记录】按钮单击事件btnBrowseAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 获取全部学生记录students = studentService.findAllStudents();// 填充表格数据fillTableData();// 删除按钮不可用btnDelete.setEnabled(false);}});// 文本框按键事件txtId.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {doQuery();}}});// JTable单击事件table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {// 获取当前行的行数int row = table.rowAtPoint(e.getPoint());// 选中鼠标单击的行table.setRowSelectionInterval(row, row);// 设置文本框内容txtId.setText(table.getValueAt(row, 0).toString());}});
}/*** 查询方法*/
private void doQuery() {// 获取查询学号String id = txtId.getText().trim();if (!id.equals("")) {students.clear();Student student = studentService.findStudentById(id);if (student != null) {// 将查询到的学生添加到列表students.add(student);// 让删除按钮可用btnDelete.setEnabled(true);}// 填充表格fillTableData();} else {JOptionPane.showMessageDialog(this, "请输入待查学生学号!", "警告", JOptionPane.WARNING_MESSAGE);txtId.requestFocus();}
}/*** 填充表格方法*/
private void fillTableData() {// 填充表头colHead.clear();colHead.add("学号");colHead.add("姓名");colHead.add("性别");colHead.add("年龄");colHead.add("系部");colHead.add("班级");colHead.add("电话");// 填充表记录rows.clear();for (Student student : students) {Vector<String> currentRow = new Vector<String>();currentRow.addElement(student.getId());currentRow.addElement(student.getName());currentRow.addElement(student.getSex());currentRow.addElement(student.getAge() + "");currentRow.addElement(student.getDepartment());currentRow.addElement(student.getClazz());currentRow.addElement(student.getTelephone());// 将当前行添加到记录行集rows.add(currentRow);}// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);// 定义滚动面板scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 将滚动面板添加到中心面板pnlCenter.add(scroller, BorderLayout.CENTER);// 重绘窗体repaint();// 判断是否有记录行if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);txtId.setText("");} else {// 让滚动条移到最上方scroller.getVerticalScrollBar().setValue(0);}
}public static void main(String[] args) {new DeleteStudentByIdFrame("");
}

}

deletestudentByclassFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;

public class DeleteStudentsByClassFrame extends JFrame {
/**
* 班级标签
/
private JLabel lblInputClass;
/
*
* 班级文本框
*/
private JTextField txtClass;

/*** 面板*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;/*** 按钮*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;
/*** 创建学生服务对象*/
private StudentService studentService;/*** 构造方法** @param title*/
public DeleteStudentsByClassFrame(String title) {super(title);intiGUI();
}private void intiGUI() {// 创建对象panel = (JPanel) getContentPane();pnlNorth = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();lblInputClass = new JLabel("输入班级:");txtClass = new JTextField(10);txtClass.setHorizontalAlignment(JTextField.CENTER);btnQuery = new JButton("查询[Q]");btnQuery.setMnemonic(KeyEvent.VK_Q);btnBrowseAll = new JButton("显示全部记录[A]");btnBrowseAll.setMnemonic(KeyEvent.VK_A);btnDelete = new JButton("删除查询的记录[D]");btnDelete.setEnabled(false);// 删除按钮不可用btnDelete.setMnemonic(KeyEvent.VK_D);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlNorth, BorderLayout.NORTH);pnlNorth.add(lblInputClass);pnlNorth.add(txtClass);pnlNorth.add(btnQuery);pnlNorth.add(btnBrowseAll);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnDelete);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());// 创建标题边框对象TitledBorder tb = new TitledBorder("查询结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 填充表格数据fillTableData();// 设置窗口属性setSize(600, 400);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {dispose();}});// 【删除】按钮单击事件btnDelete.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (!rows.isEmpty()) {long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");if (choice == JOptionPane.OK_OPTION) {// 获取待删班级String clazz = txtClass.getText().trim();// 按班级删除学生int count = studentService.deleteStudentsByClass(clazz);if (count > 0) {JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);// 重新获取全部学生列表students = studentService.findAllStudents();// 清空待删班级文本框txtClass.setText("");// 填充数据fillTableData();// 删除按钮不可用btnDelete.setEnabled(false);} else {JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);}}}}});// 【查询】按钮单击事件btnQuery.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {doQuery();}});// 【显示全部记录】按钮单击事件btnBrowseAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 获取全部学生记录students = studentService.findAllStudents();// 填充表格数据fillTableData();// 删除按钮不可用btnDelete.setEnabled(false);}});// 文本框按键事件txtClass.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {doQuery();}}});// JTable单击事件table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {// 获取当前行的行数int row = table.rowAtPoint(e.getPoint());// 选中鼠标单击的行table.setRowSelectionInterval(row, row);// 设置文本框内容txtClass.setText(table.getValueAt(row, 5).toString());}});
}/*** 查询方法*/
private void doQuery() {// 获取查询班级String clazz = txtClass.getText().trim();if (!clazz.equals("")) {students = studentService.findStudentsByClass(clazz);if (students.size() > 0) {// 让删除按钮可用btnDelete.setEnabled(true);}// 填充表格fillTableData();} else {JOptionPane.showMessageDialog(this, "请输入待查班级!", "警告", JOptionPane.WARNING_MESSAGE);txtClass.requestFocus();}
}/*** 填充表格方法*/
private void fillTableData() {// 填充表头colHead.clear();colHead.add("学号");colHead.add("姓名");colHead.add("性别");colHead.add("年龄");colHead.add("系部");colHead.add("班级");colHead.add("电话");// 填充表记录rows.clear();for (Student student : students) {Vector<String> currentRow = new Vector<String>();currentRow.addElement(student.getId());currentRow.addElement(student.getName());currentRow.addElement(student.getSex());currentRow.addElement(student.getAge() + "");currentRow.addElement(student.getDepartment());currentRow.addElement(student.getClazz());currentRow.addElement(student.getTelephone());// 将当前行添加到记录行集rows.add(currentRow);}// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);// 定义滚动面板scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 将滚动面板添加到中心面板pnlCenter.add(scroller, BorderLayout.CENTER);// 重绘窗体repaint();// 判断是否有记录行if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);txtClass.setText("");} else {// 让滚动条移到最上方scroller.getVerticalScrollBar().setValue(0);}
}public static void main(String[] args) {new DeleteStudentsByClassFrame("");
}

}

deleteStudentBydepartmentFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;

public class DeleteStudentsByDepartmentFrame extends JFrame {
/**
* 系部标签
/
private JLabel lblInputDepartment;
/
*
* 系部文本框
*/
private JTextField txtDepartment;

/*** 面板*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;/*** 按钮*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;
/*** 创建学生服务对象*/
private StudentService studentService;/*** 构造方法** @param title*/
public DeleteStudentsByDepartmentFrame(String title) {super(title);intiGUI();
}private void intiGUI() {// 创建对象panel = (JPanel) getContentPane();pnlNorth = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();lblInputDepartment = new JLabel("输入系部:");txtDepartment = new JTextField(10);txtDepartment.setHorizontalAlignment(JTextField.CENTER);btnQuery = new JButton("查询[Q]");btnQuery.setMnemonic(KeyEvent.VK_Q);btnBrowseAll = new JButton("显示全部记录[A]");btnBrowseAll.setMnemonic(KeyEvent.VK_A);btnDelete = new JButton("删除查询的记录[D]");btnDelete.setEnabled(false);// 删除按钮不可用btnDelete.setMnemonic(KeyEvent.VK_D);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlNorth, BorderLayout.NORTH);pnlNorth.add(lblInputDepartment);pnlNorth.add(txtDepartment);pnlNorth.add(btnQuery);pnlNorth.add(btnBrowseAll);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnDelete);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());// 创建标题边框对象TitledBorder tb = new TitledBorder("查询结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 填充表格数据fillTableData();// 设置窗口属性setSize(600, 400);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {dispose();}});// 【删除】按钮单击事件btnDelete.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (!rows.isEmpty()) {long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");if (choice == JOptionPane.OK_OPTION) {// 获取待删系部String department = txtDepartment.getText().trim();// 按系部删除学生int count = studentService.deleteStudentsByDepartment(department);if (count > 0) {JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);// 重新获取全部学生列表students = studentService.findAllStudents();// 清空待删班级文本框txtDepartment.setText("");// 填充数据fillTableData();// 删除按钮不可用btnDelete.setEnabled(false);} else {JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);}}}}});// 【查询】按钮单击事件btnQuery.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {doQuery();}});// 【显示全部记录】按钮单击事件btnBrowseAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 获取全部学生记录students = studentService.findAllStudents();// 填充表格数据fillTableData();// 删除按钮不可用btnDelete.setEnabled(false);}});// 文本框按键事件txtDepartment.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {doQuery();}}});// JTable单击事件table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {// 获取当前行的行数int row = table.rowAtPoint(e.getPoint());// 选中鼠标单击的行table.setRowSelectionInterval(row, row);// 设置文本框内容txtDepartment.setText(table.getValueAt(row, 4).toString());}});
}/*** 查询方法*/
private void doQuery() {// 获取查询系部String department = txtDepartment.getText().trim();if (!department.equals("")) {students = studentService.findStudentsByDepartment(department);if (students.size() > 0) {// 让删除按钮可用btnDelete.setEnabled(true);}// 填充表格fillTableData();} else {JOptionPane.showMessageDialog(this, "请输入待查系部!", "警告", JOptionPane.WARNING_MESSAGE);txtDepartment.requestFocus();}
}/*** 填充表格方法*/
private void fillTableData() {// 填充表头colHead.clear();colHead.add("学号");colHead.add("姓名");colHead.add("性别");colHead.add("年龄");colHead.add("系部");colHead.add("班级");colHead.add("电话");// 填充表记录rows.clear();for (Student student : students) {Vector<String> currentRow = new Vector<String>();currentRow.addElement(student.getId());currentRow.addElement(student.getName());currentRow.addElement(student.getSex());currentRow.addElement(student.getAge() + "");currentRow.addElement(student.getDepartment());currentRow.addElement(student.getClazz());currentRow.addElement(student.getTelephone());// 将当前行添加到记录行集rows.add(currentRow);}// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);// 定义滚动面板scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 将滚动面板添加到中心面板pnlCenter.add(scroller, BorderLayout.CENTER);// 重绘窗体repaint();// 判断是否有记录行if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);txtDepartment.setText("");} else {// 让滚动条移到最上方scroller.getVerticalScrollBar().setValue(0);}
}public static void main(String[] args) {new DeleteStudentsByDepartmentFrame("");
}

}

EditStudentFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EditStudentFrame extends JFrame {

/*** 声明面板*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
private JPanel pnlSouth1;
private JPanel pnlSouth2;/*** 声明标签*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;/*** 声明文本框*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;/*** 声明按钮*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
private JButton btnEdit;
private JButton btnOK;
private JButton btnCancel;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student>students;
/*** 创建学生服务对象*/
StudentService studentService;// 构造方法
public EditStudentFrame(String title) {super(title);initGUI();
}private void initGUI() {// 创建组件panel = (JPanel) getContentPane();pnlCenter = new JPanel();pnlSouth = new JPanel();pnlSouth.setLayout(new GridLayout(2, 1));pnlRow1 = new JPanel();pnlRow2 = new JPanel();pnlRow3 = new JPanel();pnlRow4 = new JPanel();pnlRow5 = new JPanel();pnlRow6 = new JPanel();pnlRow7 = new JPanel();pnlSouth1 = new JPanel();pnlSouth2 = new JPanel();pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));lblId = new JLabel("学号:");lblName = new JLabel("姓名:");lblSex = new JLabel("性别:");lblAge = new JLabel("年龄:");lblDepartment = new JLabel("系部:");lblClass = new JLabel("班级:");lblTelephone = new JLabel("电话:");txtId = new JTextField(40);txtName = new JTextField(40);txtSex = new JTextField(40);txtAge = new JTextField(40);txtDepartment = new JTextField(40);txtClass = new JTextField(40);txtTelephone = new JTextField(40);txtId.setEditable(false);txtName.setEditable(false);txtSex.setEditable(false);txtAge.setEditable(false);txtDepartment.setEditable(false);txtClass.setEditable(false);txtTelephone.setEditable(false);btnTop = new JButton("第一条[T]");btnPrevious = new JButton("上一条[P]");btnNext = new JButton("下一条[N]");btnBottom = new JButton("最后一条[B]");btnExit = new JButton("退出[X]");btnEdit = new JButton("编辑[E]");btnOK = new JButton("确定[O]");btnCancel = new JButton("取消[C]");btnOK.setEnabled(false);btnCancel.setEnabled(false);btnTop.setMnemonic(KeyEvent.VK_T);btnPrevious.setMnemonic(KeyEvent.VK_P);btnNext.setMnemonic(KeyEvent.VK_N);btnBottom.setMnemonic(KeyEvent.VK_B);btnExit.setMnemonic(KeyEvent.VK_X);btnEdit.setMnemonic(KeyEvent.VK_E);btnOK.setMnemonic(KeyEvent.VK_O);btnCancel.setMnemonic(KeyEvent.VK_C);// 添加组件panel.setLayout(new BorderLayout());panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlSouth, BorderLayout.SOUTH);pnlCenter.setLayout(new GridLayout(7, 1));pnlCenter.add(pnlRow1);pnlCenter.add(pnlRow2);pnlCenter.add(pnlRow3);pnlCenter.add(pnlRow4);pnlCenter.add(pnlRow5);pnlCenter.add(pnlRow6);pnlCenter.add(pnlRow7);pnlRow1.add(lblId);pnlRow1.add(txtId);pnlRow2.add(lblName);pnlRow2.add(txtName);pnlRow3.add(lblSex);pnlRow3.add(txtSex);pnlRow4.add(lblAge);pnlRow4.add(txtAge);pnlRow5.add(lblDepartment);pnlRow5.add(txtDepartment);pnlRow6.add(lblClass);pnlRow6.add(txtClass);pnlRow7.add(lblTelephone);pnlRow7.add(txtTelephone);pnlSouth.add(pnlSouth1);pnlSouth.add(pnlSouth2);pnlSouth1.add(btnTop);pnlSouth1.add(btnPrevious);pnlSouth1.add(btnNext);pnlSouth1.add(btnBottom);pnlSouth1.add(btnExit);pnlSouth2.add(btnEdit);pnlSouth2.add(btnOK);pnlSouth2.add(btnCancel);// 设置窗口属性setSize(500, 300);setResizable(false);pack();setLocationRelativeTo(null);setVisible(true);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 判断是否有学生记录if (students.size() > 0) {// 设置当前记录号currentRow = 1;// 设置窗口标题setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);// 填充窗口各文本框数据fillFrameData(currentRow);} else {JOptionPane.showMessageDialog(null, "表中没有记录!", "浏览学生表记录", JOptionPane.ERROR_MESSAGE);btnTop.setEnabled(false);btnPrevious.setEnabled(false);btnNext.setEnabled(false);btnBottom.setEnabled(false);}// 【第一条】按钮单击事件btnTop.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 设置当前记录号currentRow = 1;// 填充当前记录数据fillFrameData(currentRow);}});// 【上一条】按钮单击事件btnPrevious.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (currentRow > 1) {// 设置当前记录号currentRow--;// 填充当前记录数据fillFrameData(currentRow);} else {JOptionPane.showMessageDialog(null, "已到第一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);}}});// 【下一条】按钮单击事件btnNext.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (currentRow < students.size() - 1) {// 设置当前记录号currentRow++;// 填充当前记录数据fillFrameData(currentRow);} else {JOptionPane.showMessageDialog(null, "已到最后一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);}}});// 【最后一条】按钮单击事件btnBottom.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 设置当前记录号currentRow = students.size() - 1;// 填充当前记录数据fillFrameData(currentRow);}});// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();}});// 【编辑】按钮单击事件btnEdit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {txtName.setEditable(true);txtSex.setEditable(true);txtAge.setEditable(true);txtDepartment.setEditable(true);txtClass.setEditable(true);txtTelephone.setEditable(true);btnOK.setEnabled(true);btnCancel.setEnabled(true);}});// 【确定】按钮单击事件btnOK.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取当前学生实体Student student = students.get(currentRow);if (isNumber(txtAge.getText())) {if (isLegalTelephone(txtTelephone.getText())) {// 修改学生实体属性student.setName(txtName.getText());student.setSex(txtSex.getText());student.setAge(Integer.parseInt(txtAge.getText()));student.setDepartment(txtDepartment.getText());student.setClazz(txtClass.getText());student.setTelephone(txtTelephone.getText());// 更新学生信息int count = studentService.updateStudent(student);// 判断是否更新成功if (count > 0) {JOptionPane.showMessageDialog(null, "更新记录成功!", "编辑学生记录", JOptionPane.INFORMATION_MESSAGE);btnOK.setEnabled(false);btnCancel.setEnabled(false);btnEdit.setEnabled(true);txtName.setEditable(false);txtSex.setEditable(false);txtAge.setEditable(false);txtDepartment.setEditable(false);txtClass.setEditable(false);txtTelephone.setEditable(false);// 重新获取全部学生列表students = studentService.findAllStudents();} else {JOptionPane.showMessageDialog(null, "更新记录失败!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);}} else {JOptionPane.showMessageDialog(null, "非法手机号!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);txtTelephone.selectAll();txtTelephone.requestFocus();}} else {JOptionPane.showMessageDialog(null, "年龄必须是数字!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);txtAge.selectAll();txtAge.requestFocus();}}});// 【取消】按钮单击事件btnCancel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {btnOK.setEnabled(false);btnCancel.setEnabled(false);btnEdit.setEnabled(true);txtName.setEditable(false);txtSex.setEditable(false);txtAge.setEditable(false);txtDepartment.setEditable(false);txtClass.setEditable(false);txtTelephone.setEditable(false);// 恢复文本框修改前的值txtName.setText(students.get(currentRow).getName());txtSex.setText(students.get(currentRow).getSex());txtAge.setText(students.get(currentRow).getAge() + "");txtDepartment.setText(students.get(currentRow).getDepartment());txtClass.setText(students.get(currentRow).getClazz());txtTelephone.setText(students.get(currentRow).getTelephone());}});// 文本框按键事件txtId.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtName.requestFocus();}}});txtName.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtSex.requestFocus();}}});txtSex.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtAge.requestFocus();}}});txtAge.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtDepartment.requestFocus();}}});txtDepartment.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtClass.requestFocus();}}});txtClass.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtTelephone.requestFocus();}}});
}/*** 将当前记录数据填充窗口各文本框** @param currentRow*/
private void fillFrameData(int currentRow) {if (currentRow > 0) {setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);txtId.setText(students.get(currentRow).getId());txtName.setText(students.get(currentRow).getName());txtSex.setText(students.get(currentRow).getSex());txtAge.setText(students.get(currentRow).getAge() + "");txtDepartment.setText(students.get(currentRow).getDepartment());txtClass.setText(students.get(currentRow).getClazz());txtTelephone.setText(students.get(currentRow).getTelephone());}
}// 判断一个字符串是否全是数字
private boolean isNumber(String str) {for (int i = 0; i < str.length(); i++) {if (str.charAt(i) < '0' || str.charAt(i) > '9') {return false;}}return true;
}/*** 判断是否合法手机号** @param telephone* @return*/
private boolean isLegalTelephone(String telephone) {Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");Matcher m = p.matcher(telephone);return m.matches();
}public static void main(String[] args) {new EditStudentFrame("");
}

}

FindStudentByIdFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;

public class FindStudentByIdFrame extends JFrame {
/**
* 学号标签
/
private JLabel lblInputId;
/
*
* 学号文本框
*/
private JTextField txtId;

/*** 面板*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;/*** 按钮*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;
/*** 创建学生服务对象*/
private StudentService studentService;/*** 构造方法** @param title*/
public FindStudentByIdFrame(String title) {super(title);intiGUI();
}/*** 初始化用户界面*/
private void intiGUI() {// 创建对象panel = (JPanel) getContentPane();pnlNorth = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();lblInputId = new JLabel("输入学号:");txtId = new JTextField(10);txtId.setHorizontalAlignment(JTextField.CENTER);btnQuery = new JButton("查询[Q]");btnQuery.setMnemonic(KeyEvent.VK_Q);btnBrowseAll = new JButton("显示全部记录[A]");btnBrowseAll.setMnemonic(KeyEvent.VK_A);btnPrint = new JButton("打印[P]");btnPrint.setMnemonic(KeyEvent.VK_D);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlNorth, BorderLayout.NORTH);pnlNorth.add(lblInputId);pnlNorth.add(txtId);pnlNorth.add(btnQuery);pnlNorth.add(btnBrowseAll);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnPrint);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());// 创建标题边框对象TitledBorder tb = new TitledBorder("查询结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 填充表格数据fillTableData();// 设置窗口属性setSize(600, 400);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {dispose();}});// 【打印】按钮单击事件btnPrint.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {table.print();} catch (PrinterException e1) {e1.printStackTrace();}}});// 【查询】按钮单击事件btnQuery.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {doQuery();}});// 【显示全部记录】按钮单击事件btnBrowseAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 获取全部学生记录students = studentService.findAllStudents();// 填充表格数据fillTableData();// 删除按钮不可用btnPrint.setEnabled(false);}});// 文本框按键事件txtId.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {doQuery();}}});// JTable单击事件table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {// 获取当前行的行数int row = table.rowAtPoint(e.getPoint());// 选中鼠标单击的行table.setRowSelectionInterval(row, row);// 设置文本框内容txtId.setText(table.getValueAt(row, 0).toString());}});
}/*** 查询方法*/
private void doQuery() {// 获取查询学号String id = txtId.getText().trim();if (!id.equals("")) {students.clear();Student student = studentService.findStudentById(id);if (student != null) {// 将查询到的学生添加到列表students.add(student);}// 填充表格fillTableData();} else {JOptionPane.showMessageDialog(this, "请输入待查学生学号!", "警告", JOptionPane.WARNING_MESSAGE);txtId.requestFocus();}
}/*** 填充表格方法*/
private void fillTableData() {// 填充表头colHead.clear();colHead.add("学号");colHead.add("姓名");colHead.add("性别");colHead.add("年龄");colHead.add("系部");colHead.add("班级");colHead.add("电话");// 填充表记录rows.clear();for (Student student : students) {Vector<String> currentRow = new Vector<String>();currentRow.addElement(student.getId());currentRow.addElement(student.getName());currentRow.addElement(student.getSex());currentRow.addElement(student.getAge() + "");currentRow.addElement(student.getDepartment());currentRow.addElement(student.getClazz());currentRow.addElement(student.getTelephone());// 将当前行添加到记录行集rows.add(currentRow);}// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);// 定义滚动面板scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 将滚动面板添加到中心面板pnlCenter.add(scroller, BorderLayout.CENTER);// 重绘窗体repaint();// 判断是否有记录行if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);txtId.setText("");} else {// 让滚动条移到最上方scroller.getVerticalScrollBar().setValue(0);}
}public static void main(String[] args) {new FindStudentByIdFrame("");
}

}

FindStudentByNameFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;

public class FindStudentByNameFrame extends JFrame {
/**
* 姓名标签
/
private JLabel lblInputName;
/
*
* 姓名文本框
*/
private JTextField txtName;

/*** 面板*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;/*** 按钮*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;
/*** 创建学生服务对象*/
private StudentService studentService;/*** 构造方法** @param title*/
public FindStudentByNameFrame(String title) {super(title);intiGUI();
}/*** 初始化用户界面*/
private void intiGUI() {// 创建对象panel = (JPanel) getContentPane();pnlNorth = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();lblInputName = new JLabel("输入姓名:");txtName = new JTextField(10);txtName.setHorizontalAlignment(JTextField.CENTER);btnQuery = new JButton("查询[Q]");btnQuery.setMnemonic(KeyEvent.VK_Q);btnBrowseAll = new JButton("显示全部记录[A]");btnBrowseAll.setMnemonic(KeyEvent.VK_A);btnPrint = new JButton("打印[P]");btnPrint.setMnemonic(KeyEvent.VK_D);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlNorth, BorderLayout.NORTH);pnlNorth.add(lblInputName);pnlNorth.add(txtName);pnlNorth.add(btnQuery);pnlNorth.add(btnBrowseAll);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnPrint);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());// 创建标题边框对象TitledBorder tb = new TitledBorder("查询结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 填充表格数据fillTableData();// 设置窗口属性setSize(600, 400);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {dispose();}});// 【打印】按钮单击事件btnPrint.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {table.print();} catch (PrinterException e1) {e1.printStackTrace();}}});// 【查询】按钮单击事件btnQuery.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {doQuery();}});// 【显示全部记录】按钮单击事件btnBrowseAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 获取全部学生记录students = studentService.findAllStudents();// 填充表格数据fillTableData();}});// 文本框按键事件txtName.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {doQuery();}}});// JTable单击事件table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {// 获取当前行的行数int row = table.rowAtPoint(e.getPoint());// 选中鼠标单击的行table.setRowSelectionInterval(row, row);// 设置文本框内容txtName.setText(table.getValueAt(row, 1).toString());}});
}/*** 查询方法*/
private void doQuery() {// 获取查询姓名String name = txtName.getText().trim();if (!name.equals("")) {// 按姓名查询获取学生列表students = studentService.findStudentsByName(name);// 填充表格fillTableData();} else {JOptionPane.showMessageDialog(this, "请输入待查学生姓名!", "警告", JOptionPane.WARNING_MESSAGE);txtName.requestFocus();}
}/*** 填充表格方法*/
private void fillTableData() {// 填充表头colHead.add("学号");colHead.add("姓名");colHead.add("性别");colHead.add("年龄");colHead.add("系部");colHead.add("班级");colHead.add("电话");// 填充表记录rows.clear();for (Student student : students) {Vector<String> currentRow = new Vector<String>();currentRow.addElement(student.getId());currentRow.addElement(student.getName());currentRow.addElement(student.getSex());currentRow.addElement(student.getAge() + "");currentRow.addElement(student.getDepartment());currentRow.addElement(student.getClazz());currentRow.addElement(student.getTelephone());// 将当前行添加到记录行集rows.add(currentRow);}// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);// 定义滚动面板scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 将滚动面板添加到中心面板pnlCenter.add(scroller, BorderLayout.CENTER);// 重绘窗体repaint();// 判断是否有记录行if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);txtName.setText("");} else {// 让滚动条移到最上方scroller.getVerticalScrollBar().setValue(0);}
}public static void main(String[] args) {new FindStudentByNameFrame("");
}

}

FindStudentsByClassFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;

public class FindStudentsByClassFrame extends JFrame {
/**
* 班级标签
/
private JLabel lblInputClass;
/
*
* 班级文本框
*/
private JTextField txtClass;

/*** 面板*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;/*** 按钮*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student>students;
/*** 创建学生服务对象*/
private StudentService studentService;/*** 构造方法** @param title*/
public FindStudentsByClassFrame(String title) {super(title);intiGUI();
}/*** 初始化用户界面*/
private void intiGUI() {// 创建对象panel = (JPanel) getContentPane();pnlNorth = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();lblInputClass = new JLabel("输入班级:");txtClass = new JTextField(10);txtClass.setHorizontalAlignment(JTextField.CENTER);btnQuery = new JButton("查询[Q]");btnQuery.setMnemonic(KeyEvent.VK_Q);btnBrowseAll = new JButton("显示全部记录[A]");btnBrowseAll.setMnemonic(KeyEvent.VK_A);btnPrint = new JButton("打印[P]");btnPrint.setMnemonic(KeyEvent.VK_D);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlNorth, BorderLayout.NORTH);pnlNorth.add(lblInputClass);pnlNorth.add(txtClass);pnlNorth.add(btnQuery);pnlNorth.add(btnBrowseAll);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnPrint);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());// 创建标题边框对象TitledBorder tb = new TitledBorder("查询结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 填充表格数据fillTableData();// 设置窗口属性setSize(600, 400);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {dispose();}});// 【打印】按钮单击事件btnPrint.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {table.print();} catch (PrinterException e1) {e1.printStackTrace();}}});// 【查询】按钮单击事件btnQuery.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {doQuery();}});// 【显示全部记录】按钮单击事件btnBrowseAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 获取全部学生记录students = studentService.findAllStudents();// 填充表格数据fillTableData();}});// 文本框按键事件txtClass.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {doQuery();}}});// JTable单击事件table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {// 获取当前行的行数int row = table.rowAtPoint(e.getPoint());// 选中鼠标单击的行table.setRowSelectionInterval(row, row);// 设置文本框内容txtClass.setText(table.getValueAt(row, 5).toString());}});
}/*** 查询方法*/
private void doQuery() {// 获取查询班级String clazz = txtClass.getText().trim();if (!clazz.equals("")) {// 按班级查询获取学生列表students = studentService.findStudentsByClass(clazz);// 填充表格fillTableData();} else {JOptionPane.showMessageDialog(this, "请输入待查班级!", "警告", JOptionPane.WARNING_MESSAGE);txtClass.requestFocus();}
}/*** 填充表格方法*/
private void fillTableData() {// 填充表头colHead.clear();colHead.add("学号");colHead.add("姓名");colHead.add("性别");colHead.add("年龄");colHead.add("系部");colHead.add("班级");colHead.add("电话");// 填充表记录rows.clear();for (Student student : students) {Vector<String> currentRow = new Vector<String>();currentRow.addElement(student.getId());currentRow.addElement(student.getName());currentRow.addElement(student.getSex());currentRow.addElement(student.getAge() + "");currentRow.addElement(student.getDepartment());currentRow.addElement(student.getClazz());currentRow.addElement(student.getTelephone());// 将当前行添加到记录行集rows.add(currentRow);}// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);// 定义滚动面板scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 将滚动面板添加到中心面板pnlCenter.add(scroller, BorderLayout.CENTER);// 重绘窗体repaint();// 判断是否有记录行if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);txtClass.setText("");} else {// 让滚动条移到最上方scroller.getVerticalScrollBar().setValue(0);}
}public static void main(String[] args) {new FindStudentsByClassFrame("");
}

}


FindStudentsBydepartmentFrame

package net.yh.student.dbutil.gui;
import net.yh.student.dbutil.bean.Student;
import net.yh.student.dbutil.service.StudentService;
import net.yh.student.dbutil.service.impl.StudentServiceImpl;

import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;

public class FindStudentsByDepartmentFrame extends JFrame {
/**
* 系部标签
/
private JLabel lblInputDepartment;
/
*
* 系部文本框
*/
private JTextField txtDepartment;

/*** 面板*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;/*** 按钮*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;/*** 记录行集*/
private Vector rows;
/*** 表格列标题*/
private Vector<String> colHead;
/*** 表格*/
private JTable table;
/*** 滚动面板*/
private JScrollPane scroller;/*** 当前记录行号*/
private int currentRow;
/*** 学生列表*/
private List<Student> students;
/*** 创建学生服务对象*/
private StudentService studentService;/*** 构造方法** @param title*/
public FindStudentsByDepartmentFrame(String title) {super(title);intiGUI();
}/*** 初始化用户界面*/
private void intiGUI() {// 创建对象panel = (JPanel) getContentPane();pnlNorth = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();rows = new Vector();colHead = new Vector();lblInputDepartment = new JLabel("输入系部:");txtDepartment = new JTextField(10);txtDepartment.setHorizontalAlignment(JTextField.CENTER);btnQuery = new JButton("查询[Q]");btnQuery.setMnemonic(KeyEvent.VK_Q);btnBrowseAll = new JButton("显示全部记录[A]");btnBrowseAll.setMnemonic(KeyEvent.VK_A);btnPrint = new JButton("打印[P]");btnPrint.setMnemonic(KeyEvent.VK_D);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.add(pnlSouth, BorderLayout.SOUTH);panel.add(pnlCenter, BorderLayout.CENTER);panel.add(pnlNorth, BorderLayout.NORTH);pnlNorth.add(lblInputDepartment);pnlNorth.add(txtDepartment);pnlNorth.add(btnQuery);pnlNorth.add(btnBrowseAll);pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pnlSouth.add(btnPrint);pnlSouth.add(btnExit);pnlCenter.setLayout(new BorderLayout());// 创建标题边框对象TitledBorder tb = new TitledBorder("查询结果");pnlCenter.setBorder(tb);// 创建学生服务对象studentService = new StudentServiceImpl() {@Overridepublic Vector findRowsByClass() {return null;}@Overridepublic Vector findRowsByDepartment() {return null;}};// 获取全部学生列表students = studentService.findAllStudents();// 填充表格数据fillTableData();// 设置窗口属性setSize(600, 400);setLocationRelativeTo(null);setResizable(false);setVisible(true);// 【退出】按钮单击事件btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {dispose();}});// 【打印】按钮单击事件btnPrint.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {table.print();} catch (PrinterException e1) {e1.printStackTrace();}}});// 【查询】按钮单击事件btnQuery.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {doQuery();}});// 【显示全部记录】按钮单击事件btnBrowseAll.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 获取全部学生记录students = studentService.findAllStudents();// 填充表格数据fillTableData();}});// 文本框按键事件txtDepartment.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {doQuery();}}});// JTable单击事件table.addMouseListener(new MouseAdapter() {/* (non-Javadoc)* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)*//* (non-Javadoc)* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)*/public void mouseClicked(MouseEvent e) {// 获取当前行的行数int row = table.rowAtPoint(e.getPoint());// 选中鼠标单击的行table.setRowSelectionInterval(row, row);// 设置文本框内容txtDepartment.setText(table.getValueAt(row, 4).toString());}});
}/*** 查询方法*/
private void doQuery() {// 获取查询系部String department = txtDepartment.getText().trim();if (!department.equals("")) {// 按系部查询获取学生列表students = studentService.findStudentsByDepartment(department);// 填充表格fillTableData();} else {JOptionPane.showMessageDialog(this, "请输入待查系部!", "警告", JOptionPane.WARNING_MESSAGE);txtDepartment.requestFocus();}
}/*** 填充表格方法*/
private void fillTableData() {// 填充表头colHead.clear();colHead.add("学号");colHead.add("姓名");colHead.add("性别");colHead.add("年龄");colHead.add("系部");colHead.add("班级");colHead.add("电话");// 填充表记录rows.clear();for (Student student : students) {Vector<String> currentRow = new Vector<String>();currentRow.addElement(student.getId());currentRow.addElement(student.getName());currentRow.addElement(student.getSex());currentRow.addElement(student.getAge() + "");currentRow.addElement(student.getDepartment());currentRow.addElement(student.getClazz());currentRow.addElement(student.getTelephone());// 将当前行添加到记录行集rows.add(currentRow);}// 创建表格(参数1:记录集;参数2:表头)table = new JTable(rows, colHead);// 定义滚动面板scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 将滚动面板添加到中心面板pnlCenter.add(scroller, BorderLayout.CENTER);// 重绘窗体repaint();// 判断是否有记录行if (rows.isEmpty()) {JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);txtDepartment.setText("");} else {// 让滚动条移到最上方scroller.getVerticalScrollBar().setValue(0);}
}public static void main(String[] args) {new FindStudentsByDepartmentFrame("");
}

}


LoginFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.app.Application;
import net.yh.student.dbutil.bean.User;
import net.yh.student.dbutil.service.StatusService;
import net.yh.student.dbutil.service.UserService;
import net.yh.student.dbutil.service.impl.StatusServiceImpl;
import net.yh.student.dbutil.service.impl.UserServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LoginFrame extends JFrame {
/**
* 用户名
/
private String username;
/
*
* 密码
/
private String password;
/
*
* 用户名标签
/
private JLabel lblUsername;
/
*
* 密码标签
/
private JLabel lblPassword;
/
*
* 用户名文本框
/
private JTextField txtUsername;
/
*
* 密码文本框
/
private JPasswordField txtPassword;
/
*
* 确定按钮
/
private JButton btnOK;
/
*
* 取消按钮
/
private JButton btnCancel;
/
*
* 注册按钮
/
private JButton btnRegister;
/
*
* 主面板
/
private JPanel panel;
/
*
* 第一行面板
/
private JPanel panel1;
/
*
* 第二行面板
/
private JPanel panel2;
/
*
* 第三行面板
*/
private JPanel panel3;

/*** 构造方法** @param title*/
public LoginFrame(String title) {super(title);// 调用初始化界面方法initGUI();
}/*** 初始化界面*/
private void initGUI() {// 实例化控件lblUsername = new JLabel("用户名:");lblPassword = new JLabel("密    码:");txtUsername = new JTextField("", 15);txtPassword = new JPasswordField("", 15);btnOK = new JButton("确定[O]");btnCancel = new JButton("取消[C]");btnRegister = new JButton("注册[R]");panel = (JPanel) getContentPane();panel1 = new JPanel();panel2 = new JPanel();panel3 = new JPanel();// 设置主面板为网格布局panel.setLayout(new GridLayout(3, 1));// 将三行面板添加到主面板panel.add(panel1);panel.add(panel2);panel.add(panel3);// 将控件分别添加到三行面板panel1.add(lblUsername);panel1.add(txtUsername);panel2.add(lblPassword);panel2.add(txtPassword);panel3.add(btnOK);panel3.add(btnCancel);panel3.add(btnRegister);// 设置窗口与控件属性setSize(250, 200); // 设置窗口大小setLocationRelativeTo(null);// 让窗口居中setResizable(false); // 窗口不可调整大小pack(); // 使窗口恰好容纳组件setVisible(true); // 让窗口可见btnOK.setMnemonic(KeyEvent.VK_O); // 设置热键字母btnCancel.setMnemonic(KeyEvent.VK_C);// 设置热键字母btnRegister.setMnemonic(KeyEvent.VK_R);// 设置热键字母// txtPassword.setEchoChar('*');//设置回显字符/** 注册监听器,编写事件处理代码 采用匿名内部类方式来实现*/// 确定按钮单击事件btnOK.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {login();}});// 确定按钮按键事件btnOK.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {login();}}});// 取消按钮单击事件btnCancel.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.exit(0);}});// 注册按钮单击事件btnRegister.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {// 隐藏登录窗口Application.loginFrame.setVisible(false);// 实例化注册窗口Application.registerFrame = new RegisterFrame("注册");}});// 用户名文本框按键事件txtUsername.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtPassword.requestFocus();}}});// 密码文本框按键事件txtPassword.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {login();}}});
}/*** 登录方法*/
private void login() {// 获取用户名username = txtUsername.getText().trim();// 获取密码password = new String(txtPassword.getPassword());// 创建用户服务对象UserService userService = new UserServiceImpl();// 用户登录User user = userService.login(username, password);// 判断是否登录成功if (user != null) {// 隐藏登录窗口Application.loginFrame.setVisible(false);// 定义状态服务对象StatusService statusService = new StatusServiceImpl();// 保存用户标识Application.id = user.getId();// 保存用户名Application.username = user.getUsernname();// 提示用户登录成功JOptionPane.showMessageDialog(null, "欢迎使用学生信息管理系统" + statusService.findStatusById(1).getVersion() + "!","学生信息管理系统", JOptionPane.INFORMATION_MESSAGE);// 显示系统主窗口Application.mainFrame = new MainFrame("学生信息管理系统" + statusService.findStatusById(1).getVersion());// 释放登录窗口Application.loginFrame.dispose();} else {// 隐藏登录窗口Application.loginFrame.setVisible(false);// 提示用户登录失败JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入!", "学生信息管理系统", JOptionPane.ERROR_MESSAGE);// 显示登录窗口Application.loginFrame.setVisible(true);// 用户名文本全部选中txtUsername.selectAll();// 密码文本全部选中txtPassword.selectAll();// 用户名文本框获取焦点txtUsername.requestFocus();}
}/*** 主方法** @param args*/
public static void main(String[] args) {// 设置应用程序登录窗口Application.loginFrame = new LoginFrame("登录");
}

}

MainFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.app.Application;
import net.yh.student.dbutil.bean.Status;
import net.yh.student.dbutil.service.StatusService;
import net.yh.student.dbutil.service.impl.StatusServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;
import java.io.IOException;

/**

  • 功能:主界面窗口
  •  通过菜单系统
    
  •  调用各个功能模块
    
  • 作者:杨华
  • 日期:2019年6月22日
    */

public class MainFrame extends JFrame {
/**
* 菜单部分
/
private JMenuBar mnbMain;
/
*
* 设置菜单
/
private JMenu mnuSet;
private JMenuItem mniSetCollegeInfo;
private JMenuItem mniSetStatusBar;
private JMenuItem mniChangePassword;
private JMenuItem mniExit;
/
*
* 操作菜单
/
private JMenu mnuOperate;
private JMenuItem mniAddStudent;
private JMenuItem mniBrowseStudent;
private JMenuItem mniEditStudent;
/
*
* 删除菜单
/
private JMenu mnuDelStu;
private JMenuItem mniDelStudentById;
private JMenuItem mniDelStudentsByClass;
private JMenuItem mniDelStudentsByDepartment;
/
*
* 查询菜单
/
private JMenu mnuFind;
private JMenuItem mniFindStudentById;
private JMenuItem mniFindStudentsByName;
private JMenuItem mniFindStudentsByClass;
private JMenuItem mniFindStudentsByDepartment;
/
*
* 统计菜单
/
private JMenu mnuCount;
private JMenuItem mniCountStudentsBySex;
private JMenuItem mniCountStudentsByClass;
private JMenuItem mniCountStudentsByDepartment;
/
*
* 帮助菜单
/
private JMenu mnuHelp;
private JMenuItem mniHelp;
private JMenuItem mniAbout;
/
*
* 面板
/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlSouth;
/
*
* 状态栏标签
/
private JLabel lblStatusBar;
/
*
* 背景标签
*/
private JLabel lblBackground;

/*** 图标对象*/
private ImageIcon imgCollege;
private ImageIcon imgExit;
private ImageIcon imgPassword;
private ImageIcon imgQuery;
private ImageIcon imgBrowse;
private ImageIcon imgCount;
private ImageIcon imgBackground;
/*** 工具栏*/
private JToolBar toolbar;
/*** 按钮*/
private JButton btnSetCollege;
private JButton btnChangePassword;
private JButton btnFindStudentById;
private JButton btnExit;
private JButton btnBrowseStudent;
private JButton btnCountByDepartment;
/*** 状态对象*/
private Status status;
/*** 状态服务对象*/
private StatusService statusService;/*** 构造方法** @param title*/
public MainFrame(String title) {super(title);initGUI();
}/*** 初始化图形用户界面*/
private void initGUI() {// 创建主菜单mnbMain = new JMenuBar();// 创建【设置】菜单及其菜单项mnuSet = new JMenu("系统设置[S]");mnuSet.setMnemonic(KeyEvent.VK_S);mniSetCollegeInfo = new JMenuItem("学校信息");mniSetStatusBar = new JMenuItem("状态栏信息");mniChangePassword = new JMenuItem("修改密码");mniExit = new JMenuItem("退出系统");// 创建【操作】菜单及其菜单项mnuOperate = new JMenu("数据操作[O]");mnuOperate.setMnemonic(KeyEvent.VK_O);mniAddStudent = new JMenuItem("增加学生表记录");mnuDelStu = new JMenu("删除学生表记录");mniEditStudent = new JMenuItem("编辑学生表记录");mniBrowseStudent = new JMenuItem("浏览学生表记录");// 创建【删除学生表记录】的子菜单mniDelStudentById = new JMenuItem("按学号删除");mniDelStudentsByClass = new JMenuItem("按班级删除");mniDelStudentsByDepartment = new JMenuItem("按系部删除");// 创建【查询】菜单及其菜单项mnuFind = new JMenu("查询学生[Q]");mnuFind.setMnemonic(KeyEvent.VK_Q);mniFindStudentById = new JMenuItem("按学号查询");mniFindStudentsByName = new JMenuItem("按姓名查询");mniFindStudentsByClass = new JMenuItem("按班级查询");mniFindStudentsByDepartment = new JMenuItem("按系部查询");// 创建【统计】菜单及其菜单项mnuCount = new JMenu("人数统计[C]");mnuCount.setMnemonic(KeyEvent.VK_C);mniCountStudentsBySex = new JMenuItem("按性别统计");mniCountStudentsByClass = new JMenuItem("按班级统计");mniCountStudentsByDepartment = new JMenuItem("按系部统计");// 创建【帮助】菜单及其菜单项mnuHelp = new JMenu("帮助[H]");mnuHelp.setMnemonic(KeyEvent.VK_H);mniHelp = new JMenuItem("帮助");mniAbout = new JMenuItem("关于");// 创建图标对象imgCollege = new ImageIcon("images/college.png");imgPassword = new ImageIcon("images/password.png");imgQuery = new ImageIcon("images/query.png");imgBrowse = new ImageIcon("images/browse.png");imgCount = new ImageIcon("images/count.png");imgExit = new ImageIcon("images/exit.png");// 创建工具栏toolbar = new JToolBar();btnSetCollege = new JButton("设置学校", imgCollege);btnSetCollege.setToolTipText("设置学校信息");btnSetCollege.setVerticalTextPosition(AbstractButton.BOTTOM);btnSetCollege.setHorizontalTextPosition(AbstractButton.CENTER);btnChangePassword = new JButton("修改密码", imgPassword);btnChangePassword.setToolTipText("修改用户密码");btnChangePassword.setVerticalTextPosition(AbstractButton.BOTTOM);btnChangePassword.setHorizontalTextPosition(AbstractButton.CENTER);btnBrowseStudent = new JButton("浏览学生", imgBrowse);btnBrowseStudent.setToolTipText("浏览学生记录");btnBrowseStudent.setVerticalTextPosition(AbstractButton.BOTTOM);btnBrowseStudent.setHorizontalTextPosition(AbstractButton.CENTER);btnFindStudentById = new JButton("查询学生", imgQuery);btnFindStudentById.setToolTipText("按学号查询学生记录");btnFindStudentById.setVerticalTextPosition(AbstractButton.BOTTOM);btnFindStudentById.setHorizontalTextPosition(AbstractButton.CENTER);btnCountByDepartment = new JButton("统计人数", imgCount);btnCountByDepartment.setToolTipText("按系部统计学生人数");btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);btnCountByDepartment.setHorizontalTextPosition(AbstractButton.CENTER);btnExit = new JButton("退出系统", imgExit);btnExit.setToolTipText("退出系统");btnExit.setVerticalTextPosition(AbstractButton.BOTTOM);btnExit.setHorizontalTextPosition(AbstractButton.CENTER);toolbar.add(btnSetCollege);toolbar.add(btnChangePassword);toolbar.add(btnBrowseStudent);toolbar.add(btnFindStudentById);toolbar.add(btnCountByDepartment);toolbar.add(btnExit);// 创建面板panel = (JPanel) getContentPane();pnlCenter = new JPanel();pnlSouth = new JPanel();pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));// 创建背景图片imgBackground = new ImageIcon("images/background.jpg");// 创建背景标签lblBackground = new JLabel(imgBackground);// 创建状态栏标签lblStatusBar = new JLabel();// 设置菜单栏setJMenuBar(mnbMain);// 添加【设置】菜单mnbMain.add(mnuSet);mnuSet.add(mniSetCollegeInfo);mnuSet.add(mniSetStatusBar);mnuSet.add(mniChangePassword);mnuSet.addSeparator();mnuSet.add(mniExit);// 添加【删除学生表记录】菜单mnuDelStu.add(mniDelStudentById);mnuDelStu.add(mniDelStudentsByClass);mnuDelStu.add(mniDelStudentsByDepartment);// 添加【操作】菜单mnbMain.add(mnuOperate);mnuOperate.add(mniAddStudent);mnuOperate.add(mniEditStudent);mnuOperate.add(mnuDelStu);mnuOperate.add(mniBrowseStudent);// 添加【查询】菜单mnbMain.add(mnuFind);mnuFind.add(mniFindStudentById);mnuFind.add(mniFindStudentsByName);mnuFind.add(mniFindStudentsByClass);mnuFind.add(mniFindStudentsByDepartment);// 添加【统计】菜单mnbMain.add(mnuCount);mnuCount.add(mniCountStudentsBySex);mnuCount.add(mniCountStudentsByClass);mnuCount.add(mniCountStudentsByDepartment);// 添加【帮助】菜单mnbMain.add(mnuHelp);mnuHelp.add(mniHelp);mnuHelp.add(mniAbout);// 添加面板panel.setLayout(new BorderLayout());panel.add(toolbar, "North");panel.add(pnlCenter, "Center");panel.add(pnlSouth, "South");pnlCenter.add(lblBackground);pnlSouth.add(lblStatusBar);// 非管理员不能设置状态栏if (!Application.username.equals("admin")) {mniSetStatusBar.setEnabled(false);mniAddStudent.setEnabled(false);mnuDelStu.setEnabled(false);mniEditStudent.setEnabled(false);}// 设置状态栏信息setStatusBar();// 创建状态服务对象statusService = new StatusServiceImpl();// 获取状态对象status = statusService.findStatusById(1);// 设置窗口尺寸setSize(800, 640);// 设置窗口可见setVisible(true);// 设置窗口屏幕居中setLocationRelativeTo(null);// 设置窗口标题setTitle("学生信息管理系统" + status.getVersion());// 注册窗口监听器,继承窗口适配器,编写事件处理方法addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {exitSystem();}});// 设置菜单// 【设置学校信息】菜单项单击事件mniSetCollegeInfo.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new SetCollegeInformationFrame("");}});// 【设置状态栏信息】菜单项单击事件mniSetStatusBar.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new SetStatusBarFrame("");}});// 【修改密码】菜单项单击事件mniChangePassword.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new ChangePasswordFrame("");}});// 【退出系统】菜单项单击事件mniExit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {exitSystem();}});// 查询菜单// 【按学号查询】菜单项单击事件mniFindStudentById.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new FindStudentByIdFrame("");}});// 【按姓名查询】菜单项单击事件FindStudentsByNameFramemniFindStudentsByName.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new FindStudentByNameFrame("");}});// 【按班级查询】菜单项单击事件mniFindStudentsByClass.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new FindStudentsByClassFrame("");}});// 【按系部查询】菜单项单击事件mniFindStudentsByDepartment.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new FindStudentsByDepartmentFrame("");}});// 统计菜单// 【按性别统计人数】菜单项单击事件mniCountStudentsBySex.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new CountStudentsBySexFrame("");}});// 【按班级统计人数】菜单项单击事件mniCountStudentsByClass.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new CountStudentsByClassFrame("");}});// 【按系部统计人数】菜单项单击事件mniCountStudentsByDepartment.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new CountStudentsByDepartmentFrame("");}});// 【增加学生记录】菜单项单击事件mniAddStudent.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {new AddStudentFrame("");}});// 【按学号删除学生记录】菜单项单击事件mniDelStudentById.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new DeleteStudentByIdFrame("");}});// 【按班级删除学生记录】菜单项单击事件mniDelStudentsByClass.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new DeleteStudentsByClassFrame("");}});// 【按系部删除学生记录】菜单项单击事件mniDelStudentsByDepartment.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new DeleteStudentsByDepartmentFrame("");}});// 【编辑学生记录】菜单项单击事件mniEditStudent.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {new EditStudentFrame("");}});// 【浏览学生记录】菜单项单击事件mniBrowseStudent.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {new BrowseStudentsFrame("");}});// 【帮助】菜单单击事件mniHelp.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {try {Runtime.getRuntime().exec("cmd /c start help/帮助文档.chm");} catch (IOException e1) {JOptionPane.showMessageDialog(null, e1.getMessage(), "学生信息管理系统", JOptionPane.ERROR_MESSAGE);}}});// 【关于】菜单单击事件mniAbout.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(null,"开发人员:" + status.getCollege() + "_" + status.getAuthor() + "\n联系电话:" + status.getTelephone()+ "\n电子邮箱:" + status.getEmail(),"学生信息管理系统" + status.getVersion(), JOptionPane.INFORMATION_MESSAGE);}});// 工具栏按钮单击事件// 【设置学校信息】按钮btnSetCollege.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new SetCollegeInformationFrame("");}});// 【修改密码】按钮btnChangePassword.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new ChangePasswordFrame("");}});// 【浏览】按钮btnBrowseStudent.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new BrowseStudentsFrame("");}});// 【查询】按钮btnFindStudentById.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new FindStudentByIdFrame("");}});// 【统计】按钮btnCountByDepartment.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new CountStudentsByDepartmentFrame("");}});// 【退出】按钮btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {exitSystem();}});
}/*** 退出系统(询问用户是否要退出)*/
private void exitSystem() {int choice = JOptionPane.showConfirmDialog(this,"您是否要退出系统?", "学生信息管理系统", JOptionPane.YES_NO_OPTION);if (choice == JOptionPane.YES_OPTION) {System.exit(0);} else {// 卸载当前窗口dispose();// 重新显示主窗口Application.mainFrame = new MainFrame("学生信息管理系统" + status.getVersion());}
}/*** 设置状态栏信息*/
public void setStatusBar() {// 创建状态服务对象statusService = new StatusServiceImpl();// 获取状态栏对象status = statusService.findStatusById(1);// 设置状态栏标签lblStatusBar.setText(status.getCollege() + "学生信息管理系统" + status.getVersion() + "      作者:" + status.getAuthor() + "      地址:"+ status.getAddress() + "      电话:" + status.getTelephone() + "      邮箱:" + status.getEmail());
}/*** 主方法** @param args*/
public static void main(String[] args) {Application.mainFrame = new MainFrame("");
}

}

ResisterFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.app.Application;
import net.yh.student.dbutil.bean.User;
import net.yh.student.dbutil.service.UserService;
import net.yh.student.dbutil.service.impl.UserServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;
import java.sql.Timestamp;
import java.util.Date;

public class RegisterFrame extends JFrame {

/*** 标签*/
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblTelephone;/*** 文本框*/
private JTextField txtUsername;
private JTextField txtTelephone;
private JPasswordField txtPassword;/*** 按钮*/
private JButton btnSubmit;
private JButton btnCancel;
private JButton btnLogin;/*** 面板*/
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;private String username;
private String password;
private String telephone;
private Date registerTime;/*** 学生管理应用程序*/
private static Application app;/*** 构造方法** @param title*/
public RegisterFrame(String title) {super(title);// 创建学生管理应用程序app = new Application();initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建对象lblUsername = new JLabel("用户名:");lblPassword = new JLabel("密    码:");lblTelephone = new JLabel("电   话:");txtUsername = new JTextField("", 15);txtPassword = new JPasswordField("", 15);txtTelephone = new JTextField("", 15);btnSubmit = new JButton("提交[S]");btnCancel = new JButton("取消[C]");btnLogin = new JButton("登录[L]");panel = (JPanel) getContentPane();panel1 = new JPanel();panel2 = new JPanel();panel3 = new JPanel();panel4 = new JPanel();// 添加组件panel.setLayout(new GridLayout(4, 1));panel.add(panel1);panel.add(panel2);panel.add(panel3);panel.add(panel4);panel1.add(lblUsername);panel1.add(txtUsername);panel2.add(lblPassword);panel2.add(txtPassword);panel3.add(lblTelephone);panel3.add(txtTelephone);panel4.add(btnSubmit);panel4.add(btnCancel);panel4.add(btnLogin);// 设置属性setSize(250, 200); // 设置窗口大小setLocationRelativeTo(null);// 让窗口居中setResizable(false); // 窗口不可调整大小pack(); // 使窗口恰好容纳组件setVisible(true); // 让窗口可见btnSubmit.setMnemonic(KeyEvent.VK_O); // 设置热键字母btnCancel.setMnemonic(KeyEvent.VK_C);// 设置热键字母btnLogin.setMnemonic(KeyEvent.VK_R);// 设置热键字母txtPassword.setEchoChar('*');// 设置回显字符/** 注册监听器,编写事件处理代码 采用匿名内部类方式来实现*/// 提交按钮单击事件btnSubmit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {register();}});// 提交按钮按键事件btnSubmit.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {register();}}});// 取消按钮单击事件btnCancel.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 显示登录窗口Application.loginFrame.setVisible(true);// 关闭当前窗口dispose();}});// 登录按钮按键事件btnLogin.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {// 显示登录窗口Application.loginFrame.setVisible(true);// 关闭当前窗口dispose();}});// 用户名文本框按键事件txtUsername.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtPassword.requestFocus();}}});// 密码文本框按键事件txtPassword.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {txtTelephone.requestFocus();}}});// 电话文本框按键事件txtTelephone.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 10) {btnSubmit.requestFocus();}}});
}/*** 注册方法*/
private void register() {// 获取用户名username = txtUsername.getText().trim();// 获取密码password = new String(txtPassword.getPassword());// 获取电话telephone = txtTelephone.getText().trim();// 定义当前时间为注册时间registerTime = new Timestamp(System.currentTimeMillis());// 定义用户服务对象UserService userService = new UserServiceImpl();// 创建用户User user = new User();user.setUsernname(username);user.setPassword(password);user.setTelephone(telephone);user.setRegisterTime(registerTime);// 添加用户int count = userService.addUser(user);// 判断是否添加成功if (count > 0) {setVisible(false);JOptionPane.showMessageDialog(null, "恭喜!注册成功!", "学生信息管理系统", JOptionPane.INFORMATION_MESSAGE);setVisible(true);} else {JOptionPane.showMessageDialog(null, "遗憾!注册失败!", "学生信息管理系统", JOptionPane.INFORMATION_MESSAGE);}
}

}

SetCollegeInformationFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.bean.College;
import net.yh.student.dbutil.service.CollegeService;
import net.yh.student.dbutil.service.impl.CollegeServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SetCollegeInformationFrame extends JFrame {
/**
* 面板
/
JPanel panel;
JPanel pnlNorth;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JPanel panel5;
JPanel pnlCenter;
JPanel pnlSouth;
/
*
* 标签
/
JLabel lblName;
JLabel lblPresident;
JLabel lblStartTime;
JLabel lblTelephone;
JLabel lblEmail;
JLabel lblAddress;
JLabel lblProfile;
/
*
* 按钮
/
JButton btnSave;
JButton btnExit;
/
*
* 文本框与文本区
/
JTextField txtName;
JTextField txtPresident;
JTextField txtStartTime;
JTextField txtTelephone;
JTextField txtEMail;
JTextField txtAddress;
JTextArea txtProfile;
/
*
* 滚动面板
/
JScrollPane scrollPane;
/
*
* 学校服务
*/
private CollegeService collegeService;
private College college;
private int id = 1;

/*** 构造方法** @param title*/
public SetCollegeInformationFrame(String title) {super(title);initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建组件(面板与控件)panel = (JPanel) getContentPane();pnlNorth = new JPanel();panel1 = new JPanel();panel2 = new JPanel();panel3 = new JPanel();panel4 = new JPanel();panel5 = new JPanel();pnlCenter = new JPanel();pnlSouth = new JPanel();lblName = new JLabel("学校名称:");lblPresident = new JLabel("校         长:");lblStartTime = new JLabel("建校时间:");lblTelephone = new JLabel("联系电话:");lblEmail = new JLabel("电子邮箱:");lblAddress = new JLabel("通讯地址:");lblProfile = new JLabel("学校简介:");txtName = new JTextField(30);txtPresident = new JTextField(11);txtStartTime = new JTextField(12);txtTelephone = new JTextField(11);txtEMail = new JTextField(12);txtAddress = new JTextField(30);txtProfile = new JTextArea(5, 37);txtProfile.setLineWrap(true);// 让文本区自动换行scrollPane = new JScrollPane(txtProfile, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);btnSave = new JButton("保存[S]");btnSave.setMnemonic(KeyEvent.VK_S);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 设置大面板布局,将北面板、中面板与南面板添加到大面板panel.setLayout(new BorderLayout());panel.add(pnlNorth, "North");panel.add(pnlCenter, "Center");panel.add(pnlSouth, "South");// 设置北面板布局,添加五个小面板pnlNorth.setLayout(new GridLayout(5, 1));pnlNorth.add(panel1);pnlNorth.add(panel2);pnlNorth.add(panel3);pnlNorth.add(panel4);pnlNorth.add(panel5);// 将控件依次添加到五个小面板panel1.add(lblName);panel1.add(txtName);panel2.add(lblPresident);panel2.add(txtPresident);panel2.add(lblStartTime);panel2.add(txtStartTime);panel3.add(lblTelephone);panel3.add(txtTelephone);panel3.add(lblEmail);panel3.add(txtEMail);panel4.add(lblAddress);panel4.add(txtAddress);panel5.add(lblProfile);// 将滚动面板添加到中面板pnlCenter.add(scrollPane);// 将两个按钮添加到南面板pnlSouth.add(btnSave);pnlSouth.add(btnExit);// 创建学校服务对象collegeService = new CollegeServiceImpl();// 获取学校对象college = collegeService.findCollegeById(id);// 利用学校对象属性值设置相应控件的内容txtName.setText(college.getName());txtPresident.setText(college.getPresident());txtStartTime.setText(college.getStartTime().toString());txtTelephone.setText(college.getTelephone());txtEMail.setText(college.getEmail());txtAddress.setText(college.getAddress());txtProfile.setText(college.getProfile());// 设置窗口大小setSize(550, 450);// 设置窗口屏幕居中setLocationRelativeTo(null);// 设置窗口不可调整大小setResizable(false);// 设置窗口刚好容纳组件pack();// 设置窗口标题setTitle("设置学校信息");// 设置窗口可见setVisible(true);// 设置窗口默认关闭操作setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 【关闭】按钮单击事件处理btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// 关闭窗口dispose();}});// 【保存】按钮单击事件处理btnSave.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {try {// 修改学校对象属性college.setName(txtName.getText());college.setPresident(txtPresident.getText());college.setStartTime(new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(txtStartTime.getText().toString()).getTime()));college.setTelephone(txtTelephone.getText());college.setEmail(txtEMail.getText());college.setAddress(txtAddress.getText());college.setProfile(txtProfile.getText());// 更新学校信息collegeService.updateCollege(college);// 提示用户更新成功JOptionPane.showMessageDialog(null, "更新数据成功!");} catch (ParseException e) {// 提示用户更新失败JOptionPane.showMessageDialog(null, "更新数据失败!");}}});
}/*** 主方法** @param args*/
public static void main(String[] args) {new SetCollegeInformationFrame("");
}

}

SetStatuBarFrame

package net.yh.student.dbutil.gui;

import net.yh.student.dbutil.app.Application;
import net.yh.student.dbutil.bean.Status;
import net.yh.student.dbutil.service.StatusService;
import net.yh.student.dbutil.service.impl.StatusServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class SetStatusBarFrame extends JFrame {
/**
* 面板
/
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
/
*
* 标签
/
private JLabel lblCollege;
private JLabel lblVersion;
private JLabel lblAuthor;
private JLabel lblTelephone;
private JLabel lblAddress;
private JLabel lblEmail;
/
*
* 文本框
/
private JTextField txtCollege;
private JTextField txtVersion;
private JTextField txtAuthor;
private JTextField txtTelephone;
private JTextField txtAddress;
private JTextField txtEmail;
/
*
* 按钮
/
private JButton btnSave;
private JButton btnExit;
/
*
* 状态服务对象
*/
private StatusService statusService;

/*** 构造方法** @param title*/
public SetStatusBarFrame(String title) {super(title);initGUI();
}/*** 初始化用户界面*/
private void initGUI() {// 创建组件panel = (JPanel) getContentPane();panel1 = new JPanel();panel2 = new JPanel();panel3 = new JPanel();panel4 = new JPanel();panel5 = new JPanel();lblCollege = new JLabel("校名:");lblVersion = new JLabel("版本:");lblAuthor = new JLabel("作者:");lblTelephone = new JLabel("电话:");lblAddress = new JLabel("地址:");lblEmail = new JLabel("邮件:");txtCollege = new JTextField(12);txtVersion = new JTextField(12);txtAuthor = new JTextField(12);txtTelephone = new JTextField(12);txtAddress = new JTextField(29);txtEmail = new JTextField(29);btnSave = new JButton("保存[S]");btnSave.setMnemonic(KeyEvent.VK_S);btnExit = new JButton("退出[X]");btnExit.setMnemonic(KeyEvent.VK_X);// 添加组件panel.setLayout(new GridLayout(5, 1));panel.add(panel1);panel.add(panel2);panel.add(panel3);panel.add(panel4);panel.add(panel5);panel1.add(lblCollege);panel1.add(txtCollege);panel1.add(lblVersion);panel1.add(txtVersion);panel2.add(lblAuthor);panel2.add(txtAuthor);panel2.add(lblTelephone);panel2.add(txtTelephone);panel3.add(lblAddress);panel3.add(txtAddress);panel4.add(lblEmail);panel4.add(txtEmail);panel5.add(btnSave);panel5.add(btnExit);// 创建状态服务对象statusService = new StatusServiceImpl();// 按标识符获取状态对象Status status = statusService.findStatusById(1);if (status != null) {txtCollege.setText(status.getCollege());txtVersion.setText(status.getVersion());txtAuthor.setText(status.getAuthor());txtAddress.setText(status.getAuthor());txtTelephone.setText(status.getTelephone());txtAddress.setText(status.getAddress());txtEmail.setText(status.getEmail());}// 设置窗口属性setResizable(false);pack();setLocationRelativeTo(null);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 【关闭】按钮事件处理btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {dispose();}});// 【保存】按钮事件处理btnSave.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取状态信息对象Status status = statusService.findStatusById(1);// 修改属性值status.setCollege(txtCollege.getText());status.setVersion(txtVersion.getText());status.setAuthor(txtAuthor.getText());status.setTelephone(txtTelephone.getText());status.setAddress(txtAddress.getText());status.setEmail(txtEmail.getText());// 更新状态记录int count = statusService.updateStatus(status);// 判断是否更新成功if (count > 0) {Application.mainFrame.setStatusBar();Application.mainFrame.setTitle("学生信息管理系统" + status.getVersion());}}});
}

}

测试



实训(学生管理系统)相关推荐

  1. Django实训-学生管理系统

    文章目录 项目总述 1,功能分析 增加学生记录,查询学生记录,修改学生记录,删除学生记录 2,需求分析 可以查询个人学生信息,包括根据学号查询学习基本情况和选课信息 可以更省时间的查询信息 一.创建D ...

  2. C语言报告书学生信息管理系统,C语言实训 学生信息管理系统

    C语言实训 学生信息管理系统 实 训 报 告 实训名称 C语言编程开发实训 专业班级 物联1541 姓 名 张禄泽 学 号 指导教师 黄标兵.王丽平 实训时间 2016.2.29-2016.3.11 ...

  3. 大一项目实训—学生成绩管理系统

    大一项目实训-学生成绩管理系统 项目实训总结 由于自己大一在Java课中没有好好听讲,导致项目实训中完全是一边学习一边敲代码.但自己付出了很多努力,早上8.30起床,晚上2,3点才睡甚至通宵,虽然做的 ...

  4. Java实训学生信息_(java实训)学生信息管理系统.doc

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp计算机&nbsp>&nbspJava (java实训)学生信息管理系统.doc9页 本文档一 ...

  5. 中职计算机实训室管理规定,中高职智慧实训室管理系统

    中高职智慧实训室管理系统 利用物联网.互联网技术,通过智能数据控制终端设备获取前端数据资源,结合实训室管控软件平台的支撑,将实训室智能门禁.实训室视频监控.实训室远程电控.实训室温湿度检测.智慧电子门 ...

  6. 2021-6-28 项目实训-研究生管理系统

    2021-6-28 项目实训-研究生管理系统 完成了以下工作: 1.配置Springboot+uni-app环境 2.安装MySQL数据库 3.下载安装微信小程序开发工具 4.运行示例代码

  7. python面向对象程序设计实训学生自我总结_实训学生自我总结

    实训学生自我总结 实训学生自我总结范文 个人总结, 就是把一个时间段的个人情况进行一次全面 系统的总检查.总评价.总分析.总研究,分析成绩.不足. 经验等.下面是小编准备的实训学生自我总结,欢迎阅读. ...

  8. Java项目实训——学生成绩查询系统

    avg:分别统计学生或课程 get song Java:输出song的Java成绩 sort C:输出所有学生在C课程下的排名 new:重新输入学生的成绩 首先应定义静态变量: static Stri ...

  9. 软件工程实训——点歌管理系统开发记录

    软件工程期末作业--点歌管理系统开发记录 博客 http://blog.csdn.net/kernel_/article/details/49459947 github项目的地址 https://gi ...

最新文章

  1. leetcode算法题解(Java版)-9-N皇后问题
  2. Oracle 11.2.0.4 x64 RAC扩展存储空间
  3. oracle时间格式要注意的问题
  4. 【死磕 Spring】----- IOC 之解析 bean 标签:解析自定义标签
  5. Windows 7 安装 .NET 5 / .NET Core 3.1 环境的方法和依赖文件
  6. opencv 图像 抠图 算法_图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting
  7. 在读博士一作发Nature,学校重奖50万!
  8. 临死之前我要写一本《中国哲学史——以自然主义和人道主义的矛盾为视角》...
  9. 面试题:双重检验锁⽅式实现 单例模式
  10. Secure CRT修改文件夹的颜色
  11. 嵌入式操作系统内核原理和开发(固定内存分配算法)
  12. 苹果Mac如何在全屏幕模式下使用 App?
  13. 相机内存卡照片删除怎么恢复
  14. 电脑重装系统后播放视频卡顿怎么办
  15. RouterOS 重置密码
  16. 该战斗的时候战斗,该转身的时候转身,但请保持优雅
  17. c++ nvcc编译CUDA程序入门示例
  18. inductive bias:归纳偏置
  19. 谷粒学院day09——课程发布与阿里云视频点播服务
  20. java编写打字游戏_程序设计:简单字母打字游戏(JAVA编写)

热门文章

  1. 打标签的U盘,win10不认服务未开启
  2. java对象转json格式化_Java对象转json JsonFormat注解
  3. CSDN上传资源提示:资源上传中断
  4. docker安装postgis
  5. java计算机毕业设计飞机航班信息查询系统(附源码、数据库)
  6. 3.模板模式_1:什么是模板模式???什么情况适合用模板模式???模板模式如何实现???
  7. matlab常用函数与常用指令大全
  8. tomcat--catalina
  9. jOOQ是如何设计事务API(详细指南)
  10. android里的 ARGB 和 RGB