Java-学生管理系统

创建学校实体类College

在这里插入图片描述

package c02.s02.p02.bean;

import java.sql.ResultSet;
import java.sql.Time;
import java.util.Date;

/**
 * 功能:
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class College {
    private int id;
    private String username;
    private String password;
    private String telephone;
    private Time register_time;
    private String president;
    private String name;
    private ResultSet startTime;
    private String email;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getTelephone() {
        return telephone;
    }

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

    public Time getRegister_time() { return register_time;    }

    public void setRegister_time(Time register_time) { this.register_time = register_time; }

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


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

    public String getPresident() {
        return president;
    }

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

    public void setStartTime(Date start_time) {

    }

    public void setEmail(String email) {

    }

    public void setAddress(String address) {

    }

    public void setProfile(String profile) {

    }

    public String getName() {
        return null;
    }

    public ResultSet getStartTime() {
        return startTime;
    }

    public String getEmail() {
        return email;
    }

    public String getProfile() {
        return null;
    }
}

创建状态实体类Status

在这里插入图片描述

package c02.s02.p02.bean;

/**
 * 功能:
 * 作者:hf
 * 日期:2022年 06月 11日
 */
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 username) {
        this.college = college;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String password) {
        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 getAdress() {
        return address;
    }

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

    public String getEmail() {
        return email;
    }

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

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


    public String getAddress() {
        return null;
    }
}

创建学生实体类Student

在这里插入图片描述

package c02.s02.p02.bean;

/**
 * 功能:
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;
    private String department;
    private String t_class ;
    private String telephone;

    public Student() {

    }

    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 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 getT_class() { return t_class ; }

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

    public String getTelephone() { return telephone ; }

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

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

创建用户实体类User

在这里插入图片描述

package c02.s02.p02.bean;

import java.util.Date;

/**
 * 功能:用户实体类
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class User {
    private int id;
    private String username;
    private String password;
    private String telephone;
    private Date registerTime;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getTelephone() {
        return telephone;
    }

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

    public Date getRegisterTime() {
        return registerTime;
    }

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

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

创建数据库连接管理类

在这里插入图片描述

package c02.s02.p02.dbutils;

import com.mysql.jdbc.Connection;

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

/**
 * 功能:数据库连接管理类
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class ConnectionManager {
    // 数据库连接属性
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false";
    private static final String USER = "root";
    private static final String PASSWORD = "qian0717."; // 改成自己数据库的密码

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

    /**
     * 获取数据库连接静态方法
     *
     * @return 数据库连接
     */
    public static Connection getConnection() {
        // 定义数据库连接
        Connection conn = null;

        try {
            // 安装数据库驱动
            Class.forName(DRIVER);
            // 获取数据库连接
            conn = (Connection) DriverManager.getConnection(URL, USER, PASSWORD);
            // 提示用户数据库连接成功
            System.out.println("提示:数据库连接成功~");
        } catch (ClassNotFoundException e) {
            System.err.println("异常:数据库驱动程序未找到!");
        } catch (SQLException e) {
            System.err.println("异常:数据库连接失败!");
        }

        // 返回数据库连接
        return conn;
    }

    /**
     * 关闭数据库连接静态方法
     *
     * @param conn
     */
    public static void closeConnection(Connection conn) {
        // 判断连接是否为空
        if (conn != null) {
            try {
                // 判断连接是否关闭
                if (!conn.isClosed()) {
                    // 关闭数据库连接,释放资源
                    conn.close();
                    // 提示用户
                    System.out.println("提示:数据库连接关闭~");
                }
            } catch (SQLException e) {
                System.err.println(e.getMessage());
            }
        }
    }

    /**
     * 主方法测试数据库连接
     *
     * @param args
     */
    public static void main(String[] args) {
        // 获取数据库连接
        Connection conn = getConnection();
        // 关闭数据库连接
        closeConnection(conn);
    }
}

创建数据访问接口

在这里插入图片描述

package c02.s02.p02.dao;

import c02.s02.p02.bean.College;

/**
 * 功能:创建学校数据访问接口
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public interface CollegeDao {
    College findById(int id);
    int update(College college);
}

package c02.s02.p02.dao;

import c02.s02.p02.bean.Status;

/**
 * 功能:创建状态数据访问接口
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public interface StatusDao {
    Status findById(int id);
    int update(Status status);
}

package c02.s02.p02.dao;

import c02.s02.p02.bean.Student;

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

/**
 * 功能:创建学生数据访问接口
 * 作者:hf
 * 日期:2022年 06月 11日
 */
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 findRowsByClass();
    Vector findRowsByDepartment();
}

package c02.s02.p02.dao;

import c02.s02.p02.bean.User;

import java.util.List;

/**
 * 功能:用户数据访问接口
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public interface UserDao {
    int insert(User user);
    int delete(int id);
    int update(User user);
    User findById(int id);
    List<User> findAll();
    User login(String username, String password);
}

创建数据访问接口实现类

package c02.s02.p02.dao.impl;

import c02.s02.p02.bean.College;
import c02.s02.p02.dao.CollegeDao;
import c02.s02.p02.dbutils.ConnectionManager;
import com.mysql.jdbc.Connection;

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

import static c02.s02.p02.dbutils.ConnectionManager.getConnection;

/**
 * 功能:
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class CollegeDaoImpl implements CollegeDao {
    @Override
    public College findById(int id) {

        College college = null;

        Connection conn = getConnection();

        String strSQL = "select * from t_college where id = ?";
        try {

            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {

                college = new College();

                college.setId(rs.getInt("id"));
                college.setName(rs.getString("name"));
                college.setPresident(rs.getString("president"));
                college.setStartTime(rs.getTimestamp("start_time"));
                college.setTelephone(rs.getString("telephone"));
                college.setEmail(rs.getString("email"));
                college.setAddress(rs.getString(" address"));
                college.setProfile(rs.getString("profile"));
            }
            pstmt.close();
            rs.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return college;
    }

    @Override
    public int update(College college) {
        int count = 0;

        Connection conn = ConnectionManager.getConnection();

        //String strSQL F "update t. .college set name = ?, president F ?, start_time F ?,"
        // + "telephone = ?, email F ?, address = ?, profile=?, whereid- ?"

        String strSQL = "update t_college set name = ?, president = ?, start_time =?,"

                + " telephone E ?, email E ?, address = ?, profile E ? where id = ?";

        try {

            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            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.getAddress());
            pstmt.setString(7, college.getProfile());
            pstmt.setInt(8, college.getId());

            count = pstmt.executeUpdate();
            pstmt.close();

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

        } finally {
            ConnectionManager.closeConnection(conn);
        }
        return count;
    }
}
package c02.s02.p02.dao.impl;

import c02.s02.p02.bean.Status;
import c02.s02.p02.dbutils.ConnectionManager;
import com.mysql.jdbc.Connection;

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

/**
 * 功能:
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class StatusDaoImpl {
    @Override
    public Status findById(int id) {
        // 声明状态对象
        Status status = null;

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

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

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

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

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

package c02.s02.p02.dao.impl;

import c02.s02.p02.bean.Student;
import c02.s02.p02.dbutils.ConnectionManager;
import com.mysql.jdbc.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * 功能:
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class StudentDaoImpl {
    /**
     * 插入学生记录
     *
     * @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.setInt(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.getT_class());
            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.getT_class());
            pstmt.setString(6,student.getTelephone());
            pstmt.setInt(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.getInt("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setT_class(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<Student> findByName(String name) {
        //声明学生列表
        List<Student> students = new ArrayList<Student>();

        //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.getInt("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setT_class(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<Student> findByClass(String clazz) {
        // 声明学生列表
        List<Student> students = new ArrayList<Student>();

        // 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.getInt("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setT_class(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<Student> findByDepartment(String department) {
        // 声明学生列表
        List<Student> students = new ArrayList<Student>();

        // 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.getInt("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setT_class(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<Student> findAll() {
        // 声明学生列表
        List<Student> students = new ArrayList<Student>();

        // 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.getInt("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setT_class(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 6. 关闭结果集
            rs.close();
            // 7. 关闭语句对象
            stmt.close();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回学生列表
        return students;
    }

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

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

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

package c02.s02.p02.dao.impl;

import c02.s02.p02.bean.User;
import c02.s02.p02.dao.UserDao;
import c02.s02.p02.dbutils.ConnectionManager;
import com.mysql.jdbc.Connection;

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

/**
 * 功能:用户数据访问接口实现类
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class UserDaoImpl implements UserDao {
    /**
     * 插入用户记录
     *
     * @param user
     * @return 插入记录数
     */
    @Override
    public int insert(User user) {
        // 定义插入记录数
        int count = 0;

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

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

    @Override
    public int delete(int id) {
        return 0;
    }

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

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

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

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

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

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

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

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

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

    /**
     * 查询所有用户
     *
     * @return 用户列表
     */
    @Override
    public List<User> findAll() {
        // 声明用户列表
        List<User> users = new ArrayList<User>();

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

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

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

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

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

SQLHelp

package c02.s02.p02.gui;

import c02.s02.p02.bean.Student;
import c02.s02.p02.bean.User;
import com.mysql.jdbc.Connection;

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

/**
 * 功能:
 * 作者:hf
 * 日期:2022年 06月 11日
 */
public class SQLHelp {
    public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";//版本低的可以删除中间得cj.
    public static final String DB_URL = "jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//在问好之后的东西都是一些去重连接中可能会报错的地方

    public static final String username = "root"; //数据库账号
    public static final String password = "qian0717.";//数据库密码

    private Connection connection = null;//初始化conn~
    private PreparedStatement pStatement = null;//初始化stat~
    private ResultSet rSet = null;//初始化结果

    // 加载驱动
    // 静态初始化块(只执行一次)
    static {
        try {
            Class.forName(JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();//抛出异常
        }
    }

    // 链接数据库
    public void  connectDB() {
        try {
            connection = (Connection) DriverManager.getConnection(DB_URL,username,password);
            System.out.println("数据库链接成功");
        } catch (SQLException e) {
            System.out.println("数据库链接失败");
            e.printStackTrace();//链接失败抛出异常
        }
    }

    // 关闭资源
    public void close() {
        if(rSet != null) {
            try {
                rSet.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(pStatement != null) {
            try {
                pStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //申明变量并且说明是SQL模块
    public void addStudent(int id, String nameString, String genderString, String dobString, int batch) throws SQLException {

        // try finally 无论是否抛出异常都将执行 finally 中的语句
        try {
            // 先链接到数据库
            connectDB();

            // sql 语句
            // 静态 sql 语句需要进行字符串拼接
            // 动态 sql 语句
            String addsql = "insert into student values(?, ?, ?, ?, ?)";//添加字段值

            pStatement = connection.prepareStatement(addsql);

            pStatement.setInt(1, id);
            pStatement.setString(2, nameString);
            pStatement.setString(3, genderString);
            pStatement.setString(4, dobString);
            pStatement.setInt(5, batch);

            pStatement.executeUpdate();

        } finally {

            close();

        }

    }

    public void addManeger(String username, String password) throws SQLException {

        // try finally 无论是否抛出异常都将执行 finally 中的语句
        try {
            // 先链接到数据库
            connectDB();

            // sql 语句
            // 静态 sql 语句需要进行字符串拼接
            // 动态 sql 语句
            String addsql = "insert into m_user values(?, ?)";//添加字段值

            pStatement = connection.prepareStatement(addsql);

            pStatement.setString(1, username);
            pStatement.setString(2, password);

            pStatement.executeUpdate();

        } finally {

            close();

        }

    }

    public void changeStudent(int id, String nameString, String genderString, String dobString, String batch) throws SQLException {

        try {
            connectDB();

            String changesql = "update student set name = ?, gender = ?, dob = ?, batch = ? where id = ?";

            pStatement = connection.prepareStatement(changesql);

            pStatement.setString(1, nameString);
            pStatement.setString(2, genderString);
            pStatement.setString(3, dobString);
            pStatement.setString(4, batch);
            pStatement.setInt(5, id);

            pStatement.executeUpdate();

        } finally {
            close();
        }

    }

    public void deletestudent(int id) throws SQLException {

        try {

            connectDB();

            String deleteString = "select * from student where id = ?";

            pStatement = connection.prepareStatement(deleteString);

            pStatement.setInt(1, id);

            pStatement.execute();

        } finally {
            close();
        }

    }
    public User userpassweord(String username) throws SQLException {
        try {
            connectDB();//调用数据库相关数据

            String slelectusername = "select * from m_user where username = ?";
            pStatement = connection.prepareStatement(slelectusername);
            pStatement.setString(1, username);
            rSet = pStatement.executeQuery();
            if (rSet.next()) {
                String usernameString = rSet.getString(1);
                String passwordString = rSet.getString(2);
                return new User(usernameString, passwordString);
            }else {
                return null;
            }
        } finally {
            close();
        }
    }

    public Student queryStudent(int id) throws SQLException {
        try {
            connectDB();

            String querysql = "select * from student where id = ?";
            pStatement = connection.prepareStatement(querysql);
            pStatement.setInt(1, id);
            // 三种方法执行
            // execute()	都可以使用,返回true或false
            // executeQuery() 专门用于查询,返回结果集
            // executeUpdate() 专门用于删除、更新
            rSet = pStatement.executeQuery();

            if(rSet.next()) {

                String nameString = rSet.getString(2);
                String genderString = rSet.getString(3);
                String dobString = rSet.getString(4);
                int batchString = rSet.getInt(5);

                // 查询到学生信息返回结果集
                return new Student(nameString, genderString, dobString, batchString);
            } else {
                // 没有查询到学生信息,返回null
                return null;
            }

        } finally {
            close();
        }

    }
}

SelectFrame

package StudentManager;

/**
 * 功能:
 * 作者:
 * 日期:
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;

public class SelectFrame extends JFrame {
    public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";//版本低的可以删除中间得cj.
    public static final String DB_URL = "jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//在问好之后的东西都是一些去重连接中可能会报错的地方

    public static final String username = "root"; //数据库账号
    public static final String password = "111111";//数据库密码

    //创建窗口
    public SelectFrame() throws SQLException {

//        JTable resultarea = new JTable();

        Connection connection=null;//初始化conn
        connection = DriverManager.getConnection(DB_URL,username,password);

        PreparedStatement preparedStatement=connection.prepareStatement("select *from student");//sql语句编译预处理
        ResultSet res=preparedStatement.executeQuery();//获取ResultSet结果集

        res.last();//游标移动到查询到的数据库数据记录的最后一条
        int row=res.getRow();//获取记录的总条数
        res.beforeFirst();//游标回到第一个记录前的位置


        String arr[]= {"姓名","性别","出生日期","班级"};//定义表格的列名称
        String comm[][] = new String[row][4];//row行,4列

        res.next();//游标回到第一个记录的位置
        for(int i=0;i<row;i++) {
            comm[i][0]=res.getString("name");
            comm[i][1]=res.getString("sex");
            comm[i][2]=res.getString("birthday");
            comm[i][3]=res.getString("gender");
            res.next();
        }

        JTable jTable=new JTable(comm,arr);
        jTable.setPreferredScrollableViewportSize(new Dimension(800,200));//设置可滚动视图的大小
        JScrollPane jScrollPane=new JScrollPane(jTable);
        add(jScrollPane,BorderLayout.CENTER);//将滚动条放到窗体
//        setDefaultCloseOperation(EXIT_ON_CLOSE);//这个是关闭全部程序
        setVisible(true);//设置窗体可见
        validate();//设置组件可见
//        setLocationRelativeTo(null);
        this.setBounds(550,400,800,200);
        pack();//自动调整组建大小使每个组键都能显示
        connection.close();

        //设置程序关闭方式,
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int choice = JOptionPane.showConfirmDialog(null, "关闭该窗口?","学生管理", JOptionPane.YES_NO_OPTION);
                if (choice == JOptionPane.YES_OPTION) {
                    dispose();
                } else {
                    try {
                        new SelectFrame();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }


        });
    }
}


Start

package StudentManager;

/**
 * 功能:
 * 作者:
 * 日期:
 */
public class Start {
    public static void main(String[] args) {
        LoginStar l = new LoginStar();
    }

}

package StudentManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;


/**
 * 功能: 
 * 作者:
 * 日期:
 */
public class LoginStar extends JFrame {
    //需要5个标签   一个文本框   一个密码框    一个登录按钮
    JLabel bgimg; //定义背景标签
    JLabel username; //定义账号标签
    JLabel password; //定义密码标签
//    JLabel title; //定义标题标签
    JTextField usernametext;//输入账号的文本框
    JPasswordField passwordtext;//输入密码的文本框
    JButton login; //登录按钮
    JLabel register;//注册标签
    //窗口变量
    final int WIDTH = 300;//设置顶层框架的宽度
    final int HEIGHT = 300;//设置顶层框架的高度
    //定义一个箱子
    JPanel p;//放图片的容器
    Regevent regeveent;//定义注册标签的书签监听事件

    public LoginStar() {
        init();
        setResizable(false); //窗口的大小不可变
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置默认关闭方式
        validate();//让组件生效
        setVisible(true);   //设置当前窗口是否可以显示
    }

    private void init() {
        //给容器创建对象
        p = new JPanel();
        p.setBounds(0, 0, WIDTH, HEIGHT);
        p.setSize(300,300);
        p.setLayout(null); // 设置布局为空
        //设置当前窗口大小
//        this.setBounds();
        Toolkit kit = Toolkit.getDefaultToolkit();//获取对象大小  设置窗口位置
        Dimension screenSize = kit.getScreenSize();
        int width = screenSize.width;
        int height = screenSize.height;
        int x = (width - WIDTH) / 2;
        int y = (height - HEIGHT) / 2;
//        this.setSize(WIDTH, HEIGHT);
//        this.setLocation(x, y);//设置位置和窗口大小,以上设置背景设置图标
        this.setBounds(x, y, WIDTH, HEIGHT);
        this.setTitle("登录");
        this.add(p); //主窗口添加  容器 p

//        //背景图片会导致下面不能直接显示因为层次不对
//        ImageIcon img = new ImageIcon("src/img/bg.png");//设置背景赌片
//        bgimg = new JLabel(img);
//        //设置图片位置
//        bgimg.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
//        p.add(bgimg);  //添加图片到容器  p

        username = new JLabel("账号");
        password = new JLabel("密码");
//        title = new JLabel("登录");
        login = new JButton("登录");
        register = new JLabel("注册");

//        title.setSize(30,30);
        usernametext = new JTextField(20);
        passwordtext = new JPasswordField(20);
        passwordtext.setEchoChar('*');

//        title.setBounds(10,10,120,25);
        passwordtext.setBounds(125, 120, 120, 25);
        usernametext.setBounds(125, 90, 120, 25);
        password.setBounds(60, 120, 60, 25);
        username.setBounds(60, 90, 60, 25);
//        register.setBounds(0,200,200,30);
        login.setBounds(90,180,60,25);
        register.setBounds(10,200,100,100);

//        p.setVisible(true);
//        p.add(register);
        p.add(username);
        p.add(password);
//        p.add(title);
        p.add(usernametext);
        p.add(passwordtext);
        p.add(login);
        p.add(register);

        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String strusername = usernametext.getText();
                String strpassword = new String(passwordtext.getPassword());
                SQLHelp sqlHelp =  new SQLHelp();

                try {
                    User users = sqlHelp.userpassweord(strusername);
                    String username = users.getUsername();
                    String password = users.getPassword();
                    System.out.println(username);
                    System.out.println(password);

                    if (strusername.equals(username) && strpassword.equals(password)){
                        JOptionPane.showMessageDialog(null,"登录成功","登陆成功提示窗口",JOptionPane.INFORMATION_MESSAGE);
    //                    Register r = new Register();
    //                    r.setVisible(true);
                        MainFrame main = new MainFrame();
                        main.setVisible(true);
    //                    CommonFrame c = new CommonFrame();

                    }else {
                        JOptionPane.showMessageDialog(null,"登录失败,若没有账号点击左下角注册","登录失败提示窗口",JOptionPane.INFORMATION_MESSAGE);
    //                    Register r = new Register();//确定之后自动跳入注册页面
    //                    r.setVisible(true);
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
//        register.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                Register r = new Register();
//                r.setVisible(true);
//            }
//        });
        allEvent();
    }
    //所有处理事件都在这个地方去写
    void allEvent(){
        regeveent = new Regevent();
        
        register.addMouseListener(regeveent);
    }

}

package StudentManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * 功能: 
 * 作者:
 * 日期:
 */
public class Register extends JFrame {
    JLabel username;
    JLabel password;
    JLabel passwordAgain;

    JTextField usernameFile;
    JPasswordField passwordFile;
    JPasswordField PasswordFileAgain;
//    FlowLayout flowLayout;

    JButton sign;

    JPanel p;
    JPanel p1;

//    private Connection connection = null;//初始化conn~
//    private PreparedStatement pStatement = null;//初始化stat~
//    private ResultSet rSet = null;//初始化结果

    final int WIDTH = 410;
    final int HEIGHT = 610;

//    flowLayout = new FlowLayout(FlowLayout.CENTER);

    public Register() {
        init();//调用init方法
        setResizable(false);//这是不可变窗口
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置窗口关闭默认关闭方式
        validate();
        setVisible(true);//可显示
        Toolkit kit = Toolkit.getDefaultToolkit();//获取屏幕宽度
        Dimension screenSize = kit.getScreenSize();//初始化变量
        int width = screenSize.width;//这边都是设置大小的,当然后面我老师i给我讲的时候发现根本不需要
        int height = screenSize.height;
        int x = (width - WIDTH) / 2;
        int y = (height - HEIGHT) / 2;
        setBounds(x, y, WIDTH, HEIGHT);//设置主窗口位置和大小
        setTitle("账号注册");//设置主窗口的标题
    }

    void init() {
//        p.setBorder(BorderFactory.createTitledBorder("基本信息处理"));
//        p.setPreferredSize(new Dimension(WIDTH, HEIGHT));
//        p.setLayout(new FlowLayout(FlowLayout.CENTER));
//        p.setBounds(0,0,WIDTH,HEIGHT);
//        p.setLayout(null);
//        p.setOpaque(false);
        p = new JPanel();//初始化窗口p
        p.setLayout(null);//设置布局为空
        p.setVisible(true);//设置可见

        p1 = new JPanel();//这里因为不知道什么原因如果不重新声明一个Panle那么我的内容显示不出来
//        p1.setLayout(null);
        p1.setVisible(true);//设置可见
        p1.setLayout(null);//设置恐怖剧
        p1.setBounds(0, 0, 400, 600);//设置位置和大小

        username = new JLabel("账号:");//这里全是定义所需要的元素
        password = new JLabel("密码:");
        passwordAgain = new JLabel("确认密码:");

        usernameFile = new JTextField(20);//这里是设置长度
        passwordFile = new JPasswordField(20);//注意TextFile和PasswordFile在后续获取的时候不一样
        PasswordFileAgain = new JPasswordField(20);

        passwordFile.setEchoChar('*');//这个是输入密码的填充
        PasswordFileAgain.setEchoChar('*');

        sign = new JButton("注册");//设置注册按钮

        //这里开始设置元素位置和大小
        sign.setBounds(160, 300, 100, 50);

        username.setBounds(130, 150, 40, 20);
        password.setBounds(130, 200, 40, 20);
        passwordAgain.setBounds(130, 250, 120, 20);

        usernameFile.setBounds(200, 150, 100, 20);
        passwordFile.setBounds(200, 200, 100, 20);
        PasswordFileAgain.setBounds(200, 250, 100, 20);

        //将元素逐层添加
        p1.add(username);
        p1.add(password);
        p1.add(passwordAgain);
        p1.add(usernameFile);
        p1.add(passwordFile);
        p1.add(PasswordFileAgain);
        p1.add(sign);
        p.add(p1);
        this.add(p);

        //这里我们为注册按钮添加了监听事件
        sign.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String strusername = usernameFile.getText();//获取usernameFile的内容
                String strpassword = new String(passwordFile.getPassword());//在这里我们获取usernameFile和passwordFile方式就不一样了,看好
                String strpasswordAgain = new String(PasswordFileAgain.getPassword());
                SQLHelp sqlHelp = new SQLHelp();//这里我们调用SQLHelp文件  我在那里写了注册所需的模块

                try {
                    User users = sqlHelp.userpassweord(strusername);  //这里我们先接收看看能不能接收到数据

                    if (users == null) {//没数据说明没有账户可以注册
                        if (strpassword.equals(strpasswordAgain)) {
                            System.out.println("注册成功");//判断是否进入
                            String username = strusername;//当然这里也可以不用在初始化一次,因为上面可以直接使用
                            String password = strpassword;
                            sqlHelp.addManeger(username,password);//这里就是向SQLHelp文件里卖弄的addManager模块传入数据
                            JOptionPane.showMessageDialog(null, "注册成功", "点击确定后返回登录界面", JOptionPane.INFORMATION_MESSAGE);

                        //这里是两次密码不同
                        } else {
                            System.out.println("注册失败");
                            JOptionPane.showMessageDialog(null, "注册失败,请检查两次密码是否相同", "登录失败提示窗口", JOptionPane.INFORMATION_MESSAGE);
                        }
                    //这里是账号重复
                    }else{
                        System.out.println("已有账号无法注册");//控制台提示信息
                        JOptionPane.showMessageDialog(null,"注册失败了,已拥有该账号,请检查密码是否输入错误,目前不支持找回密码","账号已有窗口",JOptionPane.INFORMATION_MESSAGE);
//                        dispose();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }


    //这里是单独测试Register文件的时候用的
    public static void main(String[] args) {
        new Register();
    }

}

package StudentManager;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class MainFrame extends JFrame {

    private JPanel contentPane;

//    // 有了main函数才可以单独运行   取消注释可以单独测试该文件
//    public static void main(String[] args) {
//        EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                try {
//                    MainFrame frame = new MainFrame();
//                    frame.setVisible(true);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    public MainFrame() {
        // 设置大小不可改变
        setResizable(false);
        // 设置标题
        setTitle("学生管理系统");
        // 由于这是主页面,所有当主页面关闭的时候,程序就直接退出
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setBounds(int x,int y,int width,int height);
        // x 和 y 是窗口打开时的位置
        // width 和 height 是窗口打宽度和高度
        setBounds(100, 100, 450, 500);
        // setLocationRelativeTo(c)
        // 设置窗口相对于 c 的位置,当 c 为空或者 null 时,默认为是相对于屏幕中央
        setLocationRelativeTo(null);
        // 实例化一个 pane
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("学生管理系统");
        lblNewLabel.setForeground(Color.RED);
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 25));
        lblNewLabel.setBounds(140, 10, 163, 44);
        contentPane.add(lblNewLabel);

        JButton addButton = new JButton("添加学生");
        // addActionListener 注册监听器
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 设置新窗口可见
                new AddFrame().setVisible(true);
            }
        });

        addButton.setFont(new Font("宋体", Font.PLAIN, 18));
        addButton.setBounds(167, 64, 114, 37);
        contentPane.add(addButton);

        JButton changeButton = new JButton("修改信息");
        changeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 同上
                new ChangeFrame().setVisible(true);
            }
        });
        changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
        changeButton.setBounds(167, 121, 114, 37);
        contentPane.add(changeButton);

        JButton deleteButton = new JButton("删除学生");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 同上
                new DeleteFrame().setVisible(true);
            }
        });
        deleteButton.setFont(new Font("宋体", Font.PLAIN, 18));
        deleteButton.setBounds(167, 180, 114, 37);
        contentPane.add(deleteButton);

        JButton queryButton = new JButton("查询信息");
        queryButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 同上
                new QueryFrame().setVisible(true);
            }
        });
        queryButton.setFont(new Font("宋体", Font.PLAIN, 18));
        queryButton.setBounds(167, 240, 114, 37);
        contentPane.add(queryButton);

        JButton selectButton = new JButton("查询所有学生记录");
        selectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    SelectFrame s = new SelectFrame();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        selectButton.setBounds(134,300,180,37);
        selectButton.setFont(new Font("宋体", Font.PLAIN, 18));
        contentPane.add(selectButton);
    }
}

package StudentManager;

/**
 * 功能:
 * 作者:
 * 日期:
 */

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class QueryFrame extends JFrame {

    private JPanel contentPane;
    private JTextField idField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField dobField;
    private JTextField batchField;
    private JTextField searchField;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    QueryFrame frame = new QueryFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public QueryFrame() {
        setResizable(false);
        setTitle("查询信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 470);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel.setBounds(71, 0, 208, 29);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("学号:");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1.setBounds(71, 105, 50, 30);
        panel.add(lblNewLabel_1);

        idField = new JTextField();
        idField.setEditable(false);
        idField.setBounds(143, 99, 240, 45);
        panel.add(idField);
        idField.setColumns(10);

        JLabel lblNewLabel_1_1 = new JLabel("姓名:");
        lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1.setBounds(71, 160, 50, 30);
        panel.add(lblNewLabel_1_1);

        nameField = new JTextField();
        nameField.setColumns(10);
        nameField.setBounds(143, 154, 240, 45);
        panel.add(nameField);

        JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
        lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
        panel.add(lblNewLabel_1_1_1);

        genderField = new JTextField();
        genderField.setColumns(10);
        genderField.setBounds(143, 209, 240, 45);
        panel.add(genderField);

        JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
        lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
        panel.add(lblNewLabel_1_1_1_1);

        dobField = new JTextField();
        dobField.setColumns(10);
        dobField.setBounds(143, 264, 240, 45);
        panel.add(dobField);

        JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
        lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
        panel.add(lblNewLabel_1_1_1_2);

        batchField = new JTextField();
        batchField.setColumns(10);
        batchField.setBounds(143, 319, 240, 45);
        panel.add(batchField);

        searchField = new JTextField();
        searchField.setBounds(71, 39, 208, 45);
        panel.add(searchField);
        searchField.setColumns(10);

        JButton searchButton = new JButton("查找");
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                // 1.读出要查询的学生输入的id
                // 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据
                // 转化一下数据类型
                int id = Integer.parseInt(searchField.getText());

                // 2.执行JDBC语句
                try {
                    SQLHelp sqlHelp = new SQLHelp();

                    Student student = sqlHelp.queryStudent(id);

                    // 3.将查询结果填到文本框中
                    // 前提是学生存在
                    if(student != null) {
                        idField.setText(String.valueOf(id));
                        nameField.setText(student.getName());
                        genderField.setText(student.getGender());//性别
                        dobField.setText(student.getDob());//出生日期
                        batchField.setText(String.valueOf(student.getBatch()));//班级
                    } else {
                        // 此时学生不存在,提示用户,不存在该学生
                        JOptionPane.showMessageDialog(QueryFrame.this, "无此学生");
                    }

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

            }
        });
        searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
        searchButton.setBounds(289, 42, 97, 39);
        panel.add(searchButton);
    }
}


package StudentManager;

/**
 * 功能: 
 * 作者:
 * 日期:
 */

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class QueryFrame extends JFrame {

    private JPanel contentPane;
    private JTextField idField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField dobField;
    private JTextField batchField;
    private JTextField searchField;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    QueryFrame frame = new QueryFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public QueryFrame() {
        setResizable(false);
        setTitle("查询信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 470);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel.setBounds(71, 0, 208, 29);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("学号:");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1.setBounds(71, 105, 50, 30);
        panel.add(lblNewLabel_1);

        idField = new JTextField();
        idField.setEditable(false);
        idField.setBounds(143, 99, 240, 45);
        panel.add(idField);
        idField.setColumns(10);

        JLabel lblNewLabel_1_1 = new JLabel("姓名:");
        lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1.setBounds(71, 160, 50, 30);
        panel.add(lblNewLabel_1_1);

        nameField = new JTextField();
        nameField.setColumns(10);
        nameField.setBounds(143, 154, 240, 45);
        panel.add(nameField);

        JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
        lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
        panel.add(lblNewLabel_1_1_1);

        genderField = new JTextField();
        genderField.setColumns(10);
        genderField.setBounds(143, 209, 240, 45);
        panel.add(genderField);

        JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
        lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
        panel.add(lblNewLabel_1_1_1_1);

        dobField = new JTextField();
        dobField.setColumns(10);
        dobField.setBounds(143, 264, 240, 45);
        panel.add(dobField);

        JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
        lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
        panel.add(lblNewLabel_1_1_1_2);

        batchField = new JTextField();
        batchField.setColumns(10);
        batchField.setBounds(143, 319, 240, 45);
        panel.add(batchField);

        searchField = new JTextField();
        searchField.setBounds(71, 39, 208, 45);
        panel.add(searchField);
        searchField.setColumns(10);

        JButton searchButton = new JButton("查找");
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                // 1.读出要查询的学生输入的id
                // 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据
                // 转化一下数据类型
                int id = Integer.parseInt(searchField.getText());

                // 2.执行JDBC语句
                try {
                    SQLHelp sqlHelp = new SQLHelp();

                    Student student = sqlHelp.queryStudent(id);

                    // 3.将查询结果填到文本框中
                    // 前提是学生存在
                    if(student != null) {
                        idField.setText(String.valueOf(id));
                        nameField.setText(student.getName());
                        genderField.setText(student.getGender());//性别
                        dobField.setText(student.getDob());//出生日期
                        batchField.setText(String.valueOf(student.getBatch()));//班级
                    } else {
                        // 此时学生不存在,提示用户,不存在该学生
                        JOptionPane.showMessageDialog(QueryFrame.this, "无此学生");
                    }

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

            }
        });
        searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
        searchButton.setBounds(289, 42, 97, 39);
        panel.add(searchButton);
    }
}


package StudentManager;

/**
 * 功能:
 * 作者:
 * 日期:
 */

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class ChangeFrame extends JFrame {

    private JPanel contentPane;
    private JTextField idField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField dobField;
    private JTextField batchField;
    private JTextField searchField;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChangeFrame frame = new ChangeFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ChangeFrame() {
        setResizable(false);
        setTitle("修改信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 470);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("请输入要修改的学生的学号:");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel.setBounds(71, 0, 208, 29);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("学号:");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1.setBounds(71, 105, 50, 30);
        panel.add(lblNewLabel_1);

        idField = new JTextField();
        idField.setEditable(false);
        idField.setBounds(143, 99, 240, 45);
        panel.add(idField);
        idField.setColumns(10);

        JLabel lblNewLabel_1_1 = new JLabel("姓名:");
        lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1.setBounds(71, 160, 50, 30);
        panel.add(lblNewLabel_1_1);

        nameField = new JTextField();
        nameField.setColumns(10);
        nameField.setBounds(143, 154, 240, 45);
        panel.add(nameField);

        JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
        lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
        panel.add(lblNewLabel_1_1_1);

        genderField = new JTextField();
        genderField.setColumns(10);
        genderField.setBounds(143, 209, 240, 45);
        panel.add(genderField);

        JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
        lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
        panel.add(lblNewLabel_1_1_1_1);

        dobField = new JTextField();
        dobField.setColumns(10);
        dobField.setBounds(143, 264, 240, 45);
        panel.add(dobField);

        JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
        lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
        lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
        panel.add(lblNewLabel_1_1_1_2);

        batchField = new JTextField();
        batchField.setColumns(10);
        batchField.setBounds(143, 319, 240, 45);
        panel.add(batchField);

        JButton changeButton = new JButton("修改");
        changeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                int id = Integer.parseInt(searchField.getText());

                String nameString = nameField.getText();

                String gendeString = genderField.getText();

                String dobString = dobField.getText();

                String batch = batchField.getText();

                SQLHelp sqlHelp = new SQLHelp();

                try {
                    sqlHelp.changeStudent(id, nameString, gendeString, dobString, batch);

                    JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!");
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!");
                    e.printStackTrace();
                }

            }
        });
        changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
        changeButton.setBounds(182, 389, 97, 33);
        panel.add(changeButton);

        searchField = new JTextField();
        searchField.setBounds(71, 39, 208, 45);
        panel.add(searchField);
        searchField.setColumns(10);

        JButton searchButton = new JButton("查找");
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){

                int id = Integer.parseInt(searchField.getText());

                try {

                    SQLHelp sqlHelp = new SQLHelp();

                    Student student = sqlHelp.queryStudent(id);

                    if(student != null) {

                        // id 是直接读入的,所以就不需要从数据库中读出了
                        idField.setText(String.valueOf(id));
                        nameField.setText(student.getName());
                        genderField.setText(student.getGender());
                        dobField.setText(student.getDob());
                        batchField.setText(String.valueOf(student.getBatch()));

                    } else {
                        JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户");
                    }

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

            }
        });
        searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
        searchButton.setBounds(289, 42, 97, 39);
        panel.add(searchButton);
    }
}

```java
package StudentManager;

/**
 * 功能:
 * 作者:
 * 日期:
 */

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class AddFrame extends JFrame {

    private JPanel contentPane;
    private JTextField idField;
    private JTextField nameField;
    private JTextField genderField;
    private JTextField dobField;
    private JTextField batchField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddFrame frame = new AddFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AddFrame() {
        // 和前面类似
        setResizable(false);
        setTitle("添加学生");
        // 这个地方,添加学生只是这个程序的一部分,所以当关闭这部分的时候,程序不直接退出,只是关闭该部分程序
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 470);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel TitleLabel = new JLabel("请输入新学生的信息:");
        TitleLabel.setFont(new Font("宋体", Font.BOLD, 20));
        TitleLabel.setBounds(71, 34, 220, 45);
        panel.add(TitleLabel);

        JLabel idLabel = new JLabel("学号:");
        idLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        idLabel.setBounds(71, 105, 50, 30);
        panel.add(idLabel);

        idField = new JTextField();
        idField.setBounds(143, 99, 240, 45);
        panel.add(idField);
        idField.setColumns(10);

        JLabel nameLabel = new JLabel("姓名:");
        nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        nameLabel.setBounds(71, 160, 50, 30);
        panel.add(nameLabel);

        nameField = new JTextField();
        nameField.setColumns(10);
        nameField.setBounds(143, 154, 240, 45);
        panel.add(nameField);

        JLabel genderLabel = new JLabel("性别:");
        genderLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        genderLabel.setBounds(71, 215, 50, 30);
        panel.add(genderLabel);

        genderField = new JTextField();
        genderField.setColumns(10);
        genderField.setBounds(143, 209, 240, 45);
        panel.add(genderField);

        JLabel dobLabel = new JLabel("出生日期:");
        dobLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        dobLabel.setBounds(41, 270, 80, 30);
        panel.add(dobLabel);

        dobField = new JTextField();
        dobField.setColumns(10);
        dobField.setBounds(143, 264, 240, 45);
        panel.add(dobField);

        JLabel batchLabel = new JLabel("班级:");
        batchLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        batchLabel.setBounds(71, 325, 50, 30);
        panel.add(batchLabel);

        batchField = new JTextField();
        batchField.setColumns(10);
        batchField.setBounds(143, 319, 240, 45);
        panel.add(batchField);

        JButton addButton = new JButton("添加");
        // 添加鼠标监听事件
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 先取出数据
                // getText() 返回字符串类型的
                // Integer.parseInt 数据类型转换
                int id = Integer.parseInt(idField.getText());

                String nameString = nameField.getText();

                String genderString = genderField.getText();

                String dobfieldString = dobField.getText();

                int batch = Integer.parseInt(batchField.getText());
                // 输出一下学生的信息,方便修改
                System.out.println(id + "\t" + nameString + "\t" + genderString + "\t" + dobfieldString + "\t" + batch);
                // SQLHelp 是自己写的一个工具类
                SQLHelp sqlHelp = new SQLHelp();
                try {
                    // 调用添加学生信息的方法
                    sqlHelp.addStudent(id, nameString, genderString, dobfieldString, batch);
                    // 弹出对话框,提示用户添加成功
                    JOptionPane.showMessageDialog(AddFrame.this, "添加成功!");
                } catch (SQLException e1) {
                    // 这里捕获一下异常,因为学生的学号是唯一的,所以当学号已经存在的时候,提示用户,该学生信息已存在
                    // 异常代码为 23000
                    if(e1.getSQLState().equals("23000")) {
                        JOptionPane.showMessageDialog(AddFrame.this, "添加失败!该学生已存在");
                    }
                    e1.printStackTrace();
                }

            }
        });
        addButton.setFont(new Font("宋体", Font.PLAIN, 18));
        addButton.setBounds(182, 389, 97, 33);
        panel.add(addButton);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、把6个java文件保存到同一文件夹,例如C:\Student.然后将6个java文件分别编译生成相应的字节码(.class)文件,然后,用java 解释器运行主类:C:\Student\java StudentManager 2、使用jar.exe把一些文件压缩成JAR文件,来发布我们的应用程序。我们可以把本java应用程序中涉及到的类文件(*.class)压缩成一个JAR文件,例如StudentManager.jar。 首先,用记事本编写一个清单文件(Manifestfiles): Manifest-Version: 1.0 Main-class: StudentManager Created-By: 1.2.2(Sun Microsystems Inc.) 例如,清单文件命名为moon.mf,保存到C:\Student。 注意:在编写清单文件时,在“Manifest-Version:”和“1.0”之间,“Main-class:”和“StudentManager”之间,以及在“Created-By:”和“1.2.2(Sun Microsystems Inc.)”之间必须有且只有一个空格。 然后,如下生成JAR文件: C:\Student\jar cvfm StudentManager.jar moon.mf *.class 其中,参数C表示要生成一个新的JAR文件;f表示要生成的JAR文件的名字,m表示文件清单文件的名字,v表示生成详细输出到标准输出上。具体运行jar命令请查看 现在就可以将StudentManager.jar拷贝到任何一个安装了java运行环境(jdk版本号须高于1.2.2)的计算机上,只要鼠标双击StudentManager.jar文件就可以运行该java应用程序了。 注意在装了WinRAR解压缩软件的机子上可能不能运行,因此,在发布本系统时,还应该建立一个有如下内容的.bat文件(StudentManager.bat),用记事本编写: javaw -jar StudentManager.jar 保存即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值