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

本文介绍了一个基于JDBC实现的学生管理系统,包括显示所有学生、新增、删除、修改和查找学生功能。通过命令行交互,程序利用JDBC连接MySQL数据库,提供了对ResultSet到学生对象的转换。代码中包含JDBC连接的工具类、学生管理系统的接口和实现,以及输入验证和异常处理机制。

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

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

题目描述:

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

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

实现过程

1、JDBC安装下载

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

  • 下载文件:官网图片
    记住需要选择Platform Independent,下载zip包文件。

  • 将压缩包添加进入整个项目的lib包文件下
    文件目录

  • 将该jar包添加入库
    添加入库
    至此完成了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;
    }
    
}

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

参考资源链接:[C语言课程设计题目集锦](https://wenku.csdn.net/doc/8axe27c970?utm_source=wenku_answer2doc_content)实现一个具有用户界面的简单管理系统以管理通讯录信息,并且包含数据增删改查功能,可以使用C语言结合一些基础的数据结构和文件操作技术。首先,你需要定义通讯录数据结构,通常可以使用结构体来实现,比如: ```c typedef struct { char name[50]; char phone[15]; char email[50]; } Contact; ``` 接着,设计一个简单的文本界面,利用`printf`和`scanf`函数来与用户交互,实现如下功能: - 增加联系人:通过输入联系人的相关信息并保存到数组或链表中。 - 删除联系人:通过查找特定的联系人信息,然后从数组或链表中删除。 - 修改联系人信息:通过查找特定联系人,并允许用户更新信息。 - 查询联系人:通过遍历数组或链表,显示所有联系人信息或搜索特定联系人的信息。 可以使用文件操作函数,如`fopen`, `fprintf`, `fclose`, `fscanf`等,将通讯录信息存储到文件中,并从文件中读取数据。这样即使程序关闭,通讯录信息也不会丢失。 在实际编码过程中,你可能还需要考虑异常处理,比如处理用户输入错误或者文件操作失败的情况。此外,为了提高效率,可以考虑使用二分查找算法来优化查找功能。如果需要管理大量数据,可能还需要考虑将通讯录信息存储到数据库中,并使用C语言的数据库访问接口进行操作。 具体的实现代码会涉及到较多细节,但是上述步骤为你提供了一个基本的框架。要深入理解并实践这些功能,我建议你参考《C语言课程设计题目集锦》这份资料。它包含了多个课程设计题目,可以帮助你更好地理解如何实现类似管理系统,并且通过实际操作来提高你的C语言编程技能和问题解决能力。 参考资源链接:[C语言课程设计题目集锦](https://wenku.csdn.net/doc/8axe27c970?utm_source=wenku_answer2doc_content)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值