本期带领大家一起来学习java课程设计(学生信息管理系统设计)+数据库的实现思路

文章目录

    • 题目要求+数据库
    • 一 、环境搭建
    • 二 、功能实现
      • 1.学生信息类的创建✅
      • 2.学生信息的添加功能✅✅
      • 3.学生信息的删除功能✅✅✅
      • 4.学生信息的修改功能 ✅✅✅✅
      • 5.学生信息的查看功能✅✅✅✅✅
    • 三、主类的调用
      • 1、 登录界面
      • 2、 界面搭建
    • 四、 Extents的相关善后操作
    • 五、 数据库的连接
    • 六 、感谢与交流

题目要求+数据库

学生信息包括:学号,姓名,年龄,性别,出生年月,地址,电话,E-mail等。试设计学生信息管理系统,使之能提供以下功能:
1、系统以菜单方式工作
2、学生信息录入功能--输入
3、学生信息浏览功能--输出
4、学生信息查询功能--算法
按学号查询
按姓名查询
5、学生信息的删除与修改(可选项)


一 、环境搭建

在idea创建一个工程文件,在工程文件下创建一个model模块,在model模块下创建一个classSystem包,然后再存放对应的类,如下图所示

需要注意是因为连接了数据库,所以需要导入相应的jar包

二 、功能实现

1.学生信息类的创建✅

首先实现这个学生信息管理系统,我们需要先创建一个学生信息类
,包括学生的学号,姓名,年龄,地址,电话,邮箱,出生日期,具体代码如下✈️

package classSystem;public class Student {private int stuId;private String name;private int age;private String sex;private String birth;private String address;private String tel;private String Email;public Student() {}public Student(int stuId, String name, int age, String sex, String birth, String address, String tel, String email) {this.stuId = stuId;this.name = name;this.age = age;this.sex = sex;this.birth = birth;this.address = address;this.tel = tel;Email = email;}public int getStuId() {return stuId;}public void setStuId(int stuId) {this.stuId = stuId;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getBirth() {return birth;}public void setBirth(String birth) {this.birth = birth;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;}public String getEmail() {return Email;}public void setEmail(String email) {Email = email;}}

2.学生信息的添加功能✅✅

因为这个学生信息管理系统的java程序是连接了数据库,所以我们在录入我们的信息的时候,需要讲我们所录入的学生信息存放到数据库当中,所以我们需要运用到sql语句
去添加我们所录入的信息,做到了随时随地可以查询,更好地管理我们的学生信息

public static void addStu(ArrayList<Student> stu) throws AWTException {while (true) {Extents.clearConsole();System.out.println(">首界面>功能界面>添加学生信息\n\n");Scanner sc = new Scanner(System.in);System.out.print("请输入学生学号(例:2022): ");int stuId=sc.nextInt();System.out.print("请输入学生姓名(例:张三): ");String name = sc.next();System.out.print("请输入学生年龄(例:18): ");int age = sc.nextInt();System.out.print("请输入学生性别(例:男): ");String sex =sc.next();System.out.print("请输入学生出生日期(例:2004-6-1): ");String birth =sc.next();System.out.print("请输入学生电话(例:123456): ");String tel=sc.next();System.out.print("请输入学生email(例:123456@qq.com): ");String emial =sc.next();System.out.print("请输入学生地址(例:深圳): ");String address = sc.next();System.out.println("\n\n-----------------------------------------------------");System.out.println( "\t\t学号: "+stuId+"\t\t姓名: " + name + "\t\t年龄: " + age + "\t\t地址: " + address+"\t\t性别: " +sex+"\t\t出生日期: " +birth+"\t\t地址: " +address+"\t\t电话: " +tel+"\t\t邮箱: " +emial);System.out.print("\n\n是否添加该学生信息? [Yes(1) / No(0)] :");Extents.isAdd(stu, sc, stuId,sex,birth,tel,emial, name, age, address);System.out.println("\n\n\n>首界面>功能界面>添加学生信息\n");System.out.println("\t继续添加 请输入1 ");System.out.println();System.out.println("\t返回上级 请输入0 ");System.out.println("\t ---------------------------------------------------------------");System.out.print("\n请输入您的选择:");while (true) {int choose = sc.nextInt();if (choose == 1) {break;} else if (choose == 0) {Extents.clearConsole();return;} else {System.out.print("看清选项! 再给你一次机会: ");}}}}
public static void isAdd(ArrayList<Student> stu, Scanner sc,int stuId ,String sex,String birth,String tel,String email, String name, int age, String address) throws AWTException {while (true) {int is = sc.nextInt();if (is == 0) {Extents.clearConsole();System.out.println("取消成功!");break;} else if (is == 1) {/*Student s = new Student(stuId,name, age,sex,birth, address,tel,email);stu.add(s);*/Connection connection =null;String sql ="insert into Student values(" />;PreparedStatement preparedStatement=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,stuId); preparedStatement.setString(2,name); preparedStatement.setInt(3,age); preparedStatement.setString(4,sex); preparedStatement.setString(5,address); preparedStatement.setString(6,birth); preparedStatement.setString(7,tel); preparedStatement.setString(8,email);//执行preparedStatement.executeUpdate();} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(null,preparedStatement,connection);}stuId += 1;Extents.clearConsole();System.out.println("添加信息成功!\n\n");FunctionalBlock.showStu(stu);break;}System.out.print("\n输入错误!请重新输入:");}}

相信大家看到这里会有点迷惑,那么现在我来梳理一下增加信息代码块的实现逻辑
首先我们先在FunctionalBlock当中的addStudan当中录入我们的信息
然后再调用我们Extens当中的isAdd判断是否录入我们的信息
如果录入信息的话,使用sql语句进行与数据库的连接,并且将信息录入到数据库当中
并且会查询数据库当中的信息
如果不录入信息的话,那么就取消录入

增加学生信息效果图如下

3.学生信息的删除功能✅✅✅

学生信息的删除功能的实现,先调用FunctionalBlock
当中的showStu的方法展示全部学生的信息
再选择需要删除的学生信息
同样先要判断所输入的学号是=是否存在,用到了Extents当中的getFlag的方法
如果不存在,则返回-1,存在的话则进行删除操作
同样用到了sql语句与数据库当中的学生信息进行了交互

public static void deleteStu(ArrayList<Student> stu) throws AWTException {Scanner sc = new Scanner(System.in);showStu(stu);while (true) {System.out.print("\n请输入要删除的学生学号:");int sid = sc.nextInt();sc.nextLine();int flag = Extents.getFlag(stu, sid);if (flag == -1) {System.out.print("\n该学号不存在,请重新输入\n");} else {System.out.print("\n是否删除学号为:" + sid + " 的学生信息? [Yes(1) / No(0)] :");Extents.isDlete(stu, sc, flag);System.out.println("\n\n\n>首界面>功能界面>删除学生信息\n");System.out.println("\t继续删除 请输入1");System.out.println();System.out.println("\t返回上级 请输入0");System.out.println("\t ----------------------------------------------------------------");System.out.print("\n请输入您的选择: ");while (true) {int choose = sc.nextInt();if (choose == 1) {break;} else if (choose == 0) {Extents.clearConsole();return;} else {System.out.print("看清选项! 再给你一次机会: ");}}}}}

删除学生信息效果图如下

4.学生信息的修改功能 ✅✅✅✅

修改学生信息的功能呢,和上面删除学生信息 功能的实现类似,我们同样来看看如何实现的
学生信息的修改功能的实现,先调用FunctionalBlock当中的showStu的方法展示全部学生的信息
再选择需要修改的学生学号⌛️ ⏳⌛️ ⏳
同样先要判断所输入的学号是=是否存在,用到了Extents当中的getFlag的方法
如果不存在,则返回-1,存在的话则进行修改操作
再然后输入修改之后的学生信息
同样用到了sql语句与数据库当中的学生信息进行了交互

 public static void updateStu(ArrayList<Student> stu) throws AWTException {Scanner sc = new Scanner(System.in);while (true) {showStu(stu);System.out.print("\n\n请输入要修改信息的学生学号:");int sidUpdate = sc.nextInt();int flag = Extents.getFlag(stu, sidUpdate);Extents.clearConsole();if (flag == -1) {System.out.print("该学号不存在,请重新输入\n\n\n ");} else {System.out.println(">首界面>功能界面>修改学生信息\n\n");System.out.print("请输入学生学号(例:2022: ");int id=sc.nextInt();System.out.print("请输入学生姓名(例:张三): ");String name = sc.next();System.out.print("请输入学生年龄(例:18): ");int age = sc.nextInt();System.out.print("请输入学生性别(例:男): ");String sex =sc.next();System.out.print("请输入学生出生日期(例:2004-6-1): ");String birth =sc.next();System.out.print("请输入学生电话(例:123456): ");String tel=sc.next();System.out.print("请输入学生email(例:123456@qq.com): ");String emial =sc.next();System.out.print("请输入学生地址(例:深圳): ");String address = sc.next();Extents.clearConsole();Extents.getFlag(stu, sidUpdate);Connection connection =null;String sql ="update Student set stuId=" />;PreparedStatement preparedStatement=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,id);preparedStatement.setString(2,name);preparedStatement.setInt(3,age);preparedStatement.setString(4,sex);preparedStatement.setString(5,address);preparedStatement.setString(6,birth);preparedStatement.setString(7,tel);preparedStatement.setString(8,emial);preparedStatement.setInt(9,flag);//执行preparedStatement.executeUpdate();} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(null,preparedStatement,connection);}System.out.println(">首界面>功能界面>修改学生信息");System.out.println("\n\n------------------------------------------------------------------");System.out.println("修改后——>\n");System.out.println( "\t\t姓名: " + name + "\t\t年龄: " + age + "\t\t地址: " + address+"\t\t性别: " +sex+"\t\t出生日期: " +birth+"\t\t地址: " +address+"\t\t电话: " +tel+"\t\t邮箱: " +emial);System.out.print("\n\n是否修改该学生信息? [Yes(1) / No(0)] :");Extents.isUpdata(stu, sc, sidUpdate, flag,sex,birth,tel,emial, name, age, address);System.out.println("\n\n\n>首界面>功能界面>修改学生信息\n");System.out.println("\t继续修改 请输入1");System.out.println();System.out.println("\t返回上级 请输入0");System.out.println("\t --------------------------------------------------------------");System.out.print("\n请输入您的选择:");while (true) {int choose = sc.nextInt();if (choose == 1) {Extents.clearConsole();break;} else if (choose == 0) {Extents.clearConsole();return;} else {System.out.print("看清选项! 再给你一次机会: ");}}}}}

修改学生信息效果图如下

5.学生信息的查看功能✅✅✅✅✅

由于查看学生信息的功能在前面三个功能当中都有调用⌛️ ⏳⌛️ ⏳
所以我们为了使得代码更加简洁,将查询学生信息的功能封装起来了
避免了代码的冗余
同样我们来看看到底是如何实现的
因为我们的程序是连接了数据库,所以我们需要查询学生的信息的时候⌛️ ⏳⌛️ ⏳
需要用到查询的sql语句进行查询,同我们的数据库进行交互

 public static void showStu(ArrayList<Student> stu) {System.out.println("1.按学号查询");System.out.println("2.按姓名查询");System.out.println("3.查询全部 ");Scanner sc =new Scanner(System.in);int input=sc.nextInt();if(input==1){System.out.print("输入需要查询的学生学号:");int stuid=sc.nextInt();Connection connection =null;String sql="select stuId,name,age,sex,adress,birth,tel,Email from Student where stuId=" />;PreparedStatement preparedStatement=null;ResultSet set=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,stuid);//执行set = preparedStatement.executeQuery();while(set.next()){int id=set.getInt("stuId");String name=set.getString("name");int age =set.getInt("age");String sex =set.getString("sex");String address=set.getString("adress");String birth=set.getString("birth");String tel=set.getString("tel");String email=set.getString("Email");System.out.println("\t\t" + id + " \t\t\t" + name+ " \t\t\t" + age + " \t\t\t" + sex +" \t\t\t" + address +" \t\t\t" + birth+ " \t\t\t" + tel +" \t\t\t" + email +"\t\t\t"+ "\n");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(set,preparedStatement,connection);}} else if (input==2) {System.out.print("输入需要查询的学生名字:");String stuname=sc.next();Connection connection =null;String sql="select stuId,name,age,sex,adress,birth,tel,Email from Student where name=?";PreparedStatement preparedStatement=null;ResultSet set=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,stuname);//执行set = preparedStatement.executeQuery();while(set.next()){int id=set.getInt("stuId");String name=set.getString("name");int age =set.getInt("age");String sex =set.getString("sex");String address=set.getString("adress");String birth=set.getString("birth");String tel=set.getString("tel");String email=set.getString("Email");System.out.println("\t\t" + id + " \t\t\t" + name+ " \t\t\t" + age + " \t\t\t" + sex +" \t\t\t" + address +" \t\t\t" + birth+ " \t\t\t" + tel +" \t\t\t" + email +"\t\t\t"+ "\n");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(set,preparedStatement,connection);}}else{System.out.println(">学生信息显示\n");System.out.println("\t --------------------------------------------------------------------------------------------------------");System.out.println("\t 学号\t\t" + " 姓名\t\t" + " \t年龄\t" + "\t\t性别" + " \t\t\t地址"+" \t\t\t出生日期"+" \t\t\t电话"+" \t\t\t邮箱" );System.out.println("\t------------------------------------------------------------------------------------------------------");Connection connection =null;String sql="select * from Student";PreparedStatement preparedStatement=null;ResultSet set=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql);//执行set = preparedStatement.executeQuery();while(set.next()){int id=set.getInt("stuId");String name=set.getString("name");int age =set.getInt("age");String sex =set.getString("sex");String address=set.getString("adress");String birth=set.getString("birth");String tel=set.getString("tel");String email=set.getString("Email");System.out.println("\t\t" + id + " \t\t\t" + name+ " \t\t\t" + age + " \t\t\t" + sex +" \t\t\t" + address +" \t\t\t" + birth+ " \t\t\t" + tel +" \t\t\t" + email +"\t\t\t"+ "\n");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(set,preparedStatement,connection);}System.out.println("\t------------------------------------" +"------------------------------------------------------------------");}}

查询学生信息效果图如下

三、主类的调用

1、 登录界面

首先出现登陆的界面,我们需要输入我们的用户名和密码,然后判断是否正确
正确的话则执行下一步操作

 public static void interFace() throws AWTException {System.out.println(">首界面\n");System.out.println("\t*****************************************************************");System.out.println("\t   首界面");System.out.println("\t ---------------------------------------------------------------");System.out.println("\t开始登录 请输入1 ");System.out.println("\t ---------------------------------------------------------------");System.out.println("\t退出  请输入0 ");System.out.println("\t*****************************************************************");Scanner sc = new Scanner(System.in);System.out.print("\n请输入您的选择: ");while (true) {int choose = sc.nextInt();if (choose == 1) {register();break;} else if (choose == 0) {System.out.println("退出成功!");System.exit(0);} else {System.out.print("看清选项! 再给你一次机会:");}}}
public static void register() throws AWTException {for (int i = COUNT; i >= 0; i--) {Scanner sc = new Scanner(System.in);System.out.print("请输入您的用户名: ");String loginSid = sc.nextLine();System.out.print("请输入您的密码: ");String loginPassWd = sc.nextLine();if (loginSid.equals(MYSID) && loginPassWd.equals(MYPASSWD)) {Extents.clearConsole();System.out.println("欢迎登录! 用户:" + MYSID + "\n\n");menu();break;} else {if (i == 0) {System.out.println("你是不是傻!");System.exit(0);}System.out.println("错了错了, 你还有 " + i + " 次机会");}}}

2、 界面搭建

 public static void menu() throws AWTException {ArrayList<Student> stu = new ArrayList();while (true) {System.out.println(">首界面>功能界面\n");System.out.println("\t*****************************************************************");System.out.println("\t欢迎来到学生管理系统!");System.out.println("\t ---------------------------------------------------------------");System.out.println("\t 1.添加学生信息");System.out.println("\t ---------------------------------------------------------------");System.out.println("\t 2.删除学生信息");System.out.println("\t ---------------------------------------------------------------");System.out.println("\t 3.修改学生信息");System.out.println("\t ---------------------------------------------------------------");System.out.println("\t 4.查看学生信息");System.out.println("\t ---------------------------------------------------------------");System.out.println("\t   q:返回上级菜单p:退出管理系统");System.out.println("\t******************************************************************");Scanner sc = new Scanner(System.in);System.out.print("\n请输入您的选择:");String choose = sc.nextLine();switch (choose) {case "1":FunctionalBlock.addStu(stu);break;case "2":Extents.clearConsole();FunctionalBlock.deleteStu(stu);break;case "3":Extents.clearConsole();FunctionalBlock.updateStu(stu);break;case "4":Extents.clearConsole();FunctionalBlock.showStu(stu);Extents.isShow(sc);break;case "q":Extents.clearConsole();interFace();return;case "p":System.out.println("退出成功!");System.exit(0);default:Extents.clearConsole();System.out.println("这都错!看清选项再选\n\n\n");break;}}}

四、 Extents的相关善后操作

Extents是完成相关的善后操作,是对FunctionalBlock里面功能实现的相关善后的操作
目的是为了使得更加程序逻辑清晰

package classSystem;import java.awt.*;import java.awt.event.KeyEvent;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Scanner;public class Extents {public static void isAdd(ArrayList<Student> stu, Scanner sc,int stuId ,String sex,String birth,String tel,String email, String name, int age, String address) throws AWTException {while (true) {int is = sc.nextInt();if (is == 0) {Extents.clearConsole();System.out.println("取消成功!");break;} else if (is == 1) {/*Student s = new Student(stuId,name, age,sex,birth, address,tel,email);stu.add(s);*/Connection connection =null;String sql ="insert into Student values(" />;PreparedStatement preparedStatement=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1,stuId); preparedStatement.setString(2,name); preparedStatement.setInt(3,age); preparedStatement.setString(4,sex); preparedStatement.setString(5,address); preparedStatement.setString(6,birth); preparedStatement.setString(7,tel); preparedStatement.setString(8,email);//执行preparedStatement.executeUpdate();} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(null,preparedStatement,connection);}stuId += 1;Extents.clearConsole();System.out.println("添加信息成功!\n\n");FunctionalBlock.showStu(stu);break;}System.out.print("\n输入错误!请重新输入:");}}public static void isDlete(ArrayList<Student> stu, Scanner sc, int flag) throws AWTException {while (true) {int is = sc.nextInt();if (is == 0) {Extents.clearConsole();System.out.println("取消成功!");break;} else if (is == 1) {Connection connection =null;String sql ="delete from Studentwhere stuId=?";PreparedStatement preparedStatement=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,flag);//执行preparedStatement.executeUpdate();} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(null,preparedStatement,connection);}Extents.clearConsole();System.out.println("删除学生信息成功!\n\n");FunctionalBlock.showStu(stu);break;}System.out.print("\n输入错误!请重新输入:");}}public static void isUpdata(ArrayList<Student> stu, Scanner sc, int sidUpdate, int flag, String sex,String birth,String tel,String email, String name, int age, String address) throws AWTException {while (true) {int is = sc.nextInt();if (is == 0) {Extents.clearConsole();System.out.println("取消成功!");break;} else if (is == 1) {Student newStu = new Student(sidUpdate, name,age,sex,birth,address, tel,email);Extents.clearConsole();System.out.println("修改学生信息成功!\n\n");FunctionalBlock.showStu(stu);break;}System.out.print("\n输入错误!请重新输入:");}}public static void isShow(Scanner sc) throws AWTException {System.out.println("\n\n\n>首界面>功能界面>查看学生信息\n\n");System.out.println("\t返回上级 请输入0 ");System.out.println("\t ---------------------------------------------------------------");System.out.print("\n请输入您的选择: ");while (true) {int choose1 = sc.nextInt();if (choose1 == 0) {Extents.clearConsole();break;} else {System.out.print("看清选项! 再给你一次机会: ");}}}public static int getFlag(ArrayList<Student> stu, int sid) {Connection connection =null;String sql="select * from Student";PreparedStatement preparedStatement=null;ResultSet set=null;try {connection= jdbcUtiles.getConnection();preparedStatement = connection.prepareStatement(sql);//执行set = preparedStatement.executeQuery();while(set.next()){int id=set.getInt("stuId");if(sid==id){return id;}}return -1;} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtiles.close(set,preparedStatement,connection);}}public static void clearConsole() throws AWTException {Robot r = new Robot();// 按下Ctrl键r.keyPress(KeyEvent.VK_CONTROL);// 按下R键r.keyPress(KeyEvent.VK_R);// 释放R键r.keyRelease(KeyEvent.VK_R);// 释放Ctrl键r.keyRelease(KeyEvent.VK_CONTROL);r.delay(50);}}

五、 数据库的连接

毕竟我们的这个项目是要连接到数据库当中的
‍♂️ ‍♀️所以我们需要手动操作完成数据库连接的相关代码 ‍♂️ ‍♀️

package classSystem;import java.io.FileInputStream;import java.io.IOException;import java.sql.*;import java.util.Properties;public class jdbcUtiles {//工具类,连接mysql的连接和关闭private static String user;private static String password;private static String url;private static String driver;static {Properties properties =new Properties();try {properties.load(new FileInputStream("src\\mysql.properties"));//读取相关属性user=properties.getProperty("uesr");password =properties.getProperty("password");url=properties.getProperty("url");driver = properties.getProperty("driver");} catch (IOException e) {throw new RuntimeException(e);}}//连接数据库public static Connection getConnection(){try {return DriverManager.getConnection(url,user,password);} catch (SQLException e) {throw new RuntimeException(e);}}//关闭相关资源public static void close(ResultSet set, PreparedStatement preparedStatement,Connection connection){try {if(set!=null){set.close();}if(preparedStatement!=null){preparedStatement.close();}if(connection!=null){connection.close();}} catch (SQLException e) {throw new RuntimeException(e);}}}

六 、感谢与交流

如果大家通过本篇博客收获了,对通过ava实现学生课程管理系统(数据库)
有了更好的了解的话
那么希望支持一下哦如果还有不明白的,疑惑的话,或者什么比较好的建议的话,可以发到评论区,
我们一起解决,共同进步 ❗️❗️❗️
最后谢谢大家❗️❗️❗️