JDBC实现增删改查(简单的学生管理系统)

JDBC实现增删改查(简单的学生管理系统)

题目描述:

1.请编程实现基于数据库的学生信息管理程序,程序的功能有:显示所有学生、新增学生、删除学生、修改学生、查找学生(根据学号、姓名、班级、性别、专业、学院等),程序采用命令行方式。

2.请编程实现把从数据库中查询出的学生信息记录集(ResultSet)中的记录转换为学生对象。

实现过程

1、JDBC安装下载

  • 下载jar包,官网链接为:链接: 官网链接

  • 下载文件:JDBC实现增删改查(简单的学生管理系统)_第1张图片
    记住需要选择Platform Independent,下载zip包文件。

  • 将压缩包添加进入整个项目的lib包文件下
    JDBC实现增删改查(简单的学生管理系统)_第2张图片

  • 将该jar包添加入库
    JDBC实现增删改查(简单的学生管理系统)_第3张图片
    至此完成了JDBC的安装与下载

本处只写了一种方法,其实还有一种在项目环境中的配置方法,本处不多赘述。

2、JDBC连接的工具类

工具类的主要作用就是进行连接,在学生管理系统中,有多次连接和释放的过程,每次都写大段代码,阅读效果不好。将url、username、password全放入一个地方,并在该类中进行连接和释放的过程,易于代码的阅读和使用。
代码如下:

public class JDBC {
    private final String url = "jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8&useSSL=true";
    private final String username="root";
    private final String password="root";

    public JDBC() {
    }

    public Connection getConnection() throws SQLException, ClassNotFoundException {
        //        加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //     连接成功,数据库对象 Connection
        return DriverManager.getConnection(url,username,password);
    }
    //        释放连接
    public void closeStatement(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
        resultSet.close();
        statement.close();
        connection.close();
    }
    public void close(PreparedStatement statement,Connection connection) throws SQLException {
        statement.close();
        connection.close();
    }
}

3、学生管理系统的接口类

public interface StudentSystemDao {
    public void findList() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException;
    public void find(String attribute, String content) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException;
    public void save(Student student) throws SQLException, ClassNotFoundException;
    public void delete(String id) throws SQLException, ClassNotFoundException;
    public void update(String id,Student student) throws SQLException, ClassNotFoundException;
}

这个没啥好说的。

4、学生管理系统

  • 首先,在输入上,使用了while循环,当输入的格式有错误,就可以要求用户进行重新输入,这是我觉得非常有用的一个点,在以后的学习中很有用,希望大家都能学会。
  • 其次,两个题目的大致过程一样,不过在输出查询结果的时候有不同。第一题中的输出采用的普通字符串拼接(find和findList方法中被注释掉的代码),第二题中采用将其返回的结果集读出(populate方法),结果变成一个个的对象随后在方法中输出。
  • 最后,此处populate方法没有放入学生系统的接口中,因为我觉得不合适。

public class StudentSystem implements StudentSystemDao {
    public static JDBC jdbc = new JDBC();

    public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        System.out.println("欢迎使用本系统,请输入数字进行对应的操作");

        int flag;
        do {
            System.out.println("1:查询所有学生信息 2:查询指定学生信息 3:删除学生信息 4:保存学生信息 5:修改学生信息 6:退出");
            StudentSystem studentSystem = new StudentSystem();
            while (true) {
                try {
                    Scanner in = new Scanner(System.in);
                    flag = in.nextInt();
                    if (!(flag <= 6 && flag >= 1)) {
                        throw new Exception(new Exception());
                    }
                    break;
                } catch (Exception e) {
                    System.out.println("输入错误,请重新输入");
                }
            }

            Scanner in = new Scanner(System.in);
            switch (flag) {
                case 1: {
                    studentSystem.findList();
                    break;
                }
                case 2: {
                    System.out.println("想利用什么属性查询学生信息?");
                    System.out.println("1:姓名    2:年龄    3:专业    4:班级    5:学号");
                    int flagS;
                    //判断输入是否错误
                    while (true) {
                        try {
                            Scanner inner = new Scanner(System.in);
                            flagS = inner.nextInt();
                            if (!(flagS <= 5 && flagS >= 1)) {
                                throw new Exception(new Exception());
                            }
                            break;
                        } catch (Exception e) {
                            System.out.println("输入错误,请重新输入");
                        }
                    }
                    //通过输入的信息进行查询
                    String attribute = null;
                    switch (flagS) {
                        case 1: {
                            attribute = "name";
                            break;
                        }
                        case 2: {
                            attribute = "age";
                            break;
                        }
                        case 3: {
                            attribute = "specialized";
                            break;
                        }
                        case 4: {
                            attribute = "clazz";
                            break;
                        }
                        case 5: {
                            attribute = "id";
                            break;
                        }
                    }
                    System.out.println("请输入查询的信息:");
                    String content = in.next();
                    studentSystem.find(attribute, content);
                    break;
                }
                case 3: {
                    System.out.println("删除需要提供学生的id信息,请输入:");
                    String id = in.next();
                    studentSystem.delete(id);
                    break;
                }
                case 4: {
                    String name;
                    String age;
                    String specialized;
                    String clazz;
                    String id;
                    System.out.println("请输入你想保存的学生的信息");
                    System.out.println("姓名:");
                    name = in.next();
                    System.out.println("年龄:");
                    age = in.next();
                    System.out.println("专业");
                    specialized = in.next();
                    System.out.println("班级");
                    clazz = in.next();
                    System.out.println("学号");
                    id = in.next();
                    Student student = new Student(name, age, specialized, clazz, id);
                    studentSystem.save(student);
                    break;
                }
                case 5: {
                    System.out.println("修改学生信息需要提供学号,请输入:");
                    String id = in.next();
                    String name;
                    String age;
                    String specialized;
                    String clazz;
                    System.out.println("请输入你想保存的学生的信息");
                    System.out.println("姓名:");
                    name = in.next();
                    System.out.println("年龄:");
                    age = in.next();
                    System.out.println("专业");
                    specialized = in.next();
                    System.out.println("班级");
                    clazz = in.next();
                    System.out.println("学号");
                    id = in.next();
                    Student student = new Student(name, age, specialized, clazz, id);
                    studentSystem.update(id, student);
                    System.out.println("修改学生信息成功");
                    break;
                }
            }
        } while (flag != 6);


    }

    @Override
    public void findList() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Connection connection = jdbc.getConnection();
        String sql = "select * from student";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
//        while (resultSet.next()){
//            System.out.println("姓名:" + resultSet.getString("name")
//                    + " 年龄:" + resultSet.getString("age")
//                    +" 专业:"+ resultSet.getString("specialized")
//                    +" 班级:" + resultSet.getString("clazz")
//                    +" 学号:" + resultSet.getString("id")
//            );
//        }
        List<Student> list = populate(resultSet);
        for (Student s : list) {
            System.out.println("姓名:" + s.name
                    + " 年龄:" + s.age
                    + " 专业:" + s.specialized
                    + " 班级:" + s.clazz
                    + " 学号:" + s.id
            );
        }
        jdbc.closeStatement(resultSet, statement, connection);
    }

    @Override
    public void find(String attribute, String content) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Connection connection = jdbc.getConnection();
        String sql = "select * from student where " + attribute + "= " + "\"" + content + "\"";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if (resultSet == null) {
            System.out.println("输入的" + attribute + "在数据库中无对应数据,请重新输入");
        } else {
//            while (resultSet.next()){
//                System.out.println("姓名:" + resultSet.getString("name")
//                        + " 年龄:" + resultSet.getString("age")
//                        +" 专业:"+ resultSet.getString("specialized")
//                        +" 班级:" + resultSet.getString("clazz")
//                        +" 学号:" + resultSet.getString("id")
//                );
//            }
            List<Student> list = populate(resultSet);
            for (Student s : list) {
                System.out.println("姓名:" + s.name
                        + " 年龄:" + s.age
                        + " 专业:" + s.specialized
                        + " 班级:" + s.clazz
                        + " 学号:" + s.id
                );
            }
        }

        jdbc.closeStatement(resultSet, statement, connection);
    }

    @Override
    public void save(Student student) throws SQLException, ClassNotFoundException {
        Connection connection = jdbc.getConnection();
        String sql = "insert  into student(name,age,specialized,clazz,id) values (?,?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, student.name);
        ps.setString(2, String.valueOf(student.age));
        ps.setString(3, student.specialized);
        ps.setString(4, student.clazz);
        ps.setString(5, student.id);
        int i = ps.executeUpdate();
        if (i != 0) {
            System.out.println("保存数据成功");
        } else {
            System.out.println("保存数据失败");
        }
        jdbc.close(ps, connection);
    }

    @Override
    public void delete(String id) throws SQLException, ClassNotFoundException {
        Connection connection = jdbc.getConnection();
        String sql = "DELETE FROM student WHERE id = " + id;
        PreparedStatement ps = connection.prepareStatement(sql);
        int i = ps.executeUpdate();
        if (i != 0) {
            System.out.println("删除数据成功");
        } else {
            System.out.println("删除数据失败");
        }
        jdbc.close(ps, connection);
    }

    @Override
    public void update(String id, Student student) throws SQLException, ClassNotFoundException {
        Connection connection = jdbc.getConnection();
        String sql = "update student set " + "name = \'" + student.name + "\' ,age = \'" + student.age
                + "\' ,specialized= \'" + student.specialized + "\' ,clazz= \'" + student.clazz
                + "\' WHERE id = \'" + id + "\'";
        System.out.println(sql);
        PreparedStatement ps = connection.prepareStatement(sql);
        int i = ps.executeUpdate();
        if (i != 0) {
            System.out.println("更新数据成功");
        } else {
            System.out.println("更新数据失败");
        }
        jdbc.close(ps, connection);
    }

    public List populate(ResultSet rs) throws SQLException, InstantiationException, IllegalAccessException {
        List<Student> studentList = new ArrayList<>();
        while (rs.next()) {
            String name = rs.getString("name");
            String age = rs.getString("age");
            String specialized = rs.getString("specialized");
            String clazz = rs.getString("clazz");
            String id = rs.getString("id");
            Student student = new Student(name , age ,specialized ,clazz ,id);
            studentList.add(student);
        }
        return studentList;
    }
    
}

文章到这里就结束了,感谢大家的观看,如果有哪里写的不对,欢迎大家指正。

你可能感兴趣的:(数据库,服务器,java)