代码开发平台:jdk1.8.0_181
所用到的关系型数据库管理系统MySQL管理工具:SQLyog 9.63
说明:本教程默认程序代码部分读者是没有问题的,也就是说关于功能代码部分读者已经可以独立完成,有问题的是不知道如何运用面向对象程序设计思想来让自己的代码看上去更简洁,而避免大量重复的垃圾代码;而且代码实现的功能也很简单,适用于刚刚接触java实践没有项目经验的初学者
该管理系统所涉及到的数据库中表
- 课程表(表名:course)
- 年级表(表名:grade)
- 成绩表(表名:score)
- 学生信息表(表名:student_infor)
学生管理系统系统功能
代码模块
由于每一个模块的代码重复性很高,所以我只展示其中一个模块的代码实现,以成绩表管理模块为例:
对于成绩表管理模块而言,其包括三个功能:
- 向成绩表中添加一条记录
- 更改成绩表中的记录
- 删除成绩表中的记录
无论是增加,更改,还是删除,其都需要进行数据库的连接,然后向数据库管理系统递交sql语句,让数据库管理系统来执行sql命令,然后待数据库管理系统执行完sql命令后我们还需要关闭数据库连接,所以对于数据库连接->递交sql命令->关闭数据库连接这些操作是被频繁使用到的代码功能,我们上诉数据库连接->递交sql命令->关闭数据库连接功能代码封装成类,让子类(具体实现增删改功能的类去继承它)这样就避免了写大量的重复代码,基于此思想,为实现成绩表管理模块,我设计了五个类:
- CommonClass:用于封装数据库连接->递交sql命令->关闭数据库连接功能代码部分,作为公共父类,以便于实现代码复用,避免写大量的重复代码
package StudentManagementSystem; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * @author mli-m * @Date 2023/8/26 * @Desc 用于创建数据库连接,执行相关的sql语句,其中的方法在每一个子管理系统中都是共用的 */ public class CommonClass { public CommonClass(String url, String userName, String password) { this.url = url; this.userName = userName; this.password = password; } public CommonClass(){ this.url="jdbc:mysql://localhost:3306/student?useSSL=false"; this.userName="root"; this.password="123456"; } private static String url; private static String userName; private static String password; //建立数据库连接,发送sql命令给mysql数据库管理系统,让其执行sql命令后返回执行结果 public static void excuteUpdate(String sql) throws ClassNotFoundException, SQLException { //导入jdbc驱动程序,jdbc驱动程序是用于建立与数据库连接的工具 Class.forName("com.mysql.jdbc.Driver"); //利用jdbc驱动程序创建数据库连接通道 Connection connection= DriverManager.getConnection(url,userName,password); //创建好通道之后还需要构造一个用于在通道上跑的小车,该小车用于传送sql语句给mysql数据库管理系统执行 Statement statement=connection.createStatement(); statement.executeUpdate(sql); //关闭数据库连接 //先释放小车,再释放通道 statement.close(); connection.close(); } }
-
ScoreDao:继承成CommonClass类,用于完成系统交互部分,将相关功能的sql命令设计出来然后交给MySQL数据库管理系统来执行
package StudentManagementSystem.ScoreManageModule; import StudentManagementSystem.CommonClass; import StudentManagementSystem.StudentTable.Score; import java.sql.SQLException; /** * @author mli-m * @Date 2023/8/27 * @Desc 成绩管理模块系统交互板块 */ public class ScoreDao extends CommonClass { private String sql; public void insert(Score score) throws SQLException, ClassNotFoundException { sql="insert into score values('" + score.recordId + "','" + score.studentId + "','" + score.subjectId + "','" + score.examDate + "','" + score.studentScore + "')"; excuteUpdate(sql); } public void update(Score score) throws SQLException, ClassNotFoundException { sql="update score set student_id='" + score.studentId + "',subject_id='" + score.subjectId + "',exam_date='" + score.examDate + "',student_score='" + score.studentScore + "' where record_id='" + score.recordId + "'"; excuteUpdate(sql); } public void delete(Score score) throws SQLException, ClassNotFoundException { sql="delete from score where record_id='" + score.recordId + "'"; excuteUpdate(sql); } }
-
ScoreManagement:实现业务交互板块,即管理用户输入
package StudentManagementSystem.ScoreManageModule; import StudentManagementSystem.StudentTable.Score; import java.sql.SQLException; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; /** * @author mli-m * @Date 2023/8/27 * @Desc 成绩表管理模块业务交互板块 */ public class ScoreManagement { public static Score score=new Score(); public static Scanner scanner=new Scanner(System.in); public static ScoreDao scoreDao=new ScoreDao(); public static void addScoreInfor() throws SQLException, ClassNotFoundException, ParseException { //业务处理模块,所谓高内聚就是只做一件事 System.out.println("请输入你所需要添加的成绩表信息"); System.out.println("--------------------------------------------"); System.out.println("记录编号:"); score.recordId=Integer.parseInt(scanner.nextLine()); System.out.println("学生编号:"); score.studentId=Integer.parseInt(scanner.nextLine()); System.out.println("课程编号:"); score.subjectId=Integer.parseInt(scanner.nextLine()); System.out.println("考试日期:"); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date temp=simpleDateFormat.parse(scanner.nextLine()); score.examDate= new Timestamp(temp.getTime()); //nextLine()函数可以得到带空格的字符串,而next()函数遇到tab键和空格则以为字符串已经输入完毕,所以next()函数得不到带空格键的函数 System.out.println("学生成绩:"); score.studentScore=Integer.parseInt(scanner.nextLine()); scoreDao.insert(score); System.out.println("插入成功"); } public static void updateScoreInfor() throws SQLException, ClassNotFoundException, ParseException { System.out.println("请输入你所需要更改的记录编号"); System.out.println("-------------------------------------------"); score.recordId=Integer.parseInt(scanner.nextLine()); System.out.println("请输入更改后的成绩表记录信息"); System.out.println("学生编号:"); score.studentId=Integer.parseInt(scanner.nextLine()); System.out.println("课程编号:"); score.subjectId=Integer.parseInt(scanner.nextLine()); System.out.println("考试日期:"); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date temp=simpleDateFormat.parse(scanner.nextLine()); score.examDate= new Timestamp(temp.getTime()); System.out.println("学生成绩:"); score.studentScore=Integer.parseInt(scanner.nextLine()); scoreDao.update(score); System.out.println("更新成功"); } public static void deleteScoreInfor() throws SQLException, ClassNotFoundException { System.out.println("请输入你所需要删除的记录编号"); System.out.println("--------------------------------------------"); score.recordId=Integer.parseInt(scanner.nextLine()); scoreDao.delete(score); System.out.println("删除成功"); } }
-
ScoreManageMenu:菜单栏,用户功能选择界面设计
package StudentManagementSystem.ScoreManageModule; import java.sql.SQLException; import java.text.ParseException; import java.util.Scanner; /** * @author mli-m * @Date 2023/8/27 * @Desc 成绩管理模块菜单栏 */ public class ScoreManageMenu { //用户界面 public static void menuSelect() throws SQLException, ClassNotFoundException, ParseException { //用户界面 System.out.println("学生信息管理>成绩管理"); System.out.println(); System.out.println("欢迎来到成绩管理界面!!!!!!"); System.out.println(); boolean isContinue=true; do { System.out.println("请输入你所需要进行的操作"); System.out.println("1.向成绩表中插入一条记录"); System.out.println("2.更改成绩表中的记录"); System.out.println("3.删除成绩表中的记录"); System.out.println("0.返回上一级"); Scanner scanner = new Scanner(System.in); int operation = scanner.nextInt(); switch (operation) { case 1: ScoreManagement.addScoreInfor(); break; case 2: ScoreManagement.updateScoreInfor(); break; case 3: ScoreManagement.deleteScoreInfor(); break; case 0: isContinue=false; break; default: System.out.println("你所输入的操作不存在,请重新输入"); break; } System.out.println("------------------分割线---------------------------"); }while(isContinue); } }
-
Score:这个类的作用是用于存放成绩表中的各列属性的,方便管理用户输入,如果不设计这个类的话就要定义很多变量来存放用户输入
package StudentManagementSystem.StudentTable; import java.sql.Timestamp; /** * @author mli-m * @Date 2023/8/26 * @Desc 学生成绩表 */ public class Score { public int recordId; public Integer studentId; public Integer subjectId; public Timestamp examDate; public Integer studentScore; }
以下是我的代码管理部分
该系统分为四个子模块,将每一个模块的代码放在一个包里面来管理,StudentSystemManageApp是主程序,用于测试系统功能的
系统的完整源代码以及创建数据库的脚本语言
我将完整代码和数据库脚本语言压缩在一个文件夹里面上传资源了,有需要应付实践作业的可以去我的主页点击资源部分自取,如果在资源部份没有看到我上传的代码那么就是csdn还在审核中,还得等几天审核通过之后才可以看到
祝学习进步,生活愉快!!!!