当前位置:   article > 正文

学生网上选课管理系统_JAVA课程设计_开发日志_学生选课管理系统

学生选课管理系统

一、设计方案:

设计目标:

通过此次开发建立一个学生网上选课管理系统,实现对课程的基本信息维护以及学生选课、教师查看上课信息,管理员管理系统数据等。

从而通过本选课系统达到实际应用目的,为高校管理学生网上选课等方面提供极大便利。
 

流程目的图:

 功能拆分:

 

ER图:

变量名称标准:

管理员相关变量:Mno:管理员号码, Mname:管理员姓名, Maccount:管理员账号, Mtelephone:管理员电话, Mage:管理员年龄, Msex:性别, code:密码, Mpower:权限

        二、源代码开发部分:

1、数据库数据的导入:

使用sql语言创建:

 

 此处不过多赘述。详情数据库数据可私信。

2、sql server java建立 代码部分:

check course:

  1. import java.sql.Connection;
  2. public class CheckCourse {
  3. public static void CheckCourse(User user) throws Exception{
  4. System.out.println("已选择的课程有:");
  5. user.sql = "select * from myCourseInfo";
  6. user.count = user.statement.executeQuery(user.sql);
  7. while(user.count.next()) {
  8. System.out.println(user.count.getString("Cno")
  9. + user.count.getString("Grade") + user.count.getString("Sno"));
  10. }
  11. }
  12. }

TC:

  1. import java.util.Scanner;
  2. public class TC {
  3. static private Scanner scanner = new Scanner(System.in);
  4. static private String Course_no;
  5. static private int count;
  6. //分配课程
  7. public static boolean Assignment(User user) throws Exception{
  8. System.out.println("请输入教师号:");
  9. String Tno ="'"+ scanner.next()+"\'";
  10. user.sql = "select Tno from Teacher where Tno = "+Tno;
  11. user.count=user.statement.executeQuery(user.sql);
  12. if(!user.count.next()){
  13. System.out.println("教师查询失败");
  14. return false;
  15. }
  16. System.out.println("请输入课程号:");
  17. Course_no ="\'"+ scanner.next()+"\'";
  18. user.sql="select Couser_no from Couser where Couser_no="+Course_no;
  19. user.count = user.statement.executeQuery(user.sql);
  20. if(!user.count.next()){
  21. System.out.println("课程查询失败");
  22. return false;
  23. }
  24. user.sql="insert into TC values("+Tno+","+Course_no+")";
  25. count=user.statement.executeUpdate(user.sql);
  26. if(count==1) System.out.println("分配成功。");
  27. else{
  28. System.out.println("分配失败。");
  29. return false;
  30. }
  31. return true;
  32. }
  33. //删除课程
  34. public static boolean Delete(User user) throws Exception{
  35. System.out.println("请输入教师号:");
  36. String Tno ="'"+ scanner.next()+"\'";
  37. user.sql = "select Tno from TC where Tno = "+Tno;
  38. user.count=user.statement.executeQuery(user.sql);
  39. if(!user.count.next()){
  40. System.out.println("教师查询失败");
  41. return false;
  42. }
  43. System.out.println("请输入课程号:");
  44. Course_no ="\'"+ scanner.next()+"\'";
  45. user.sql="select Cno from TC where Cno="+Course_no;
  46. user.count = user.statement.executeQuery(user.sql);
  47. if(!user.count.next()){
  48. System.out.println("课程查询失败");
  49. return false;
  50. }
  51. user.sql="delete TC where Tno="+Tno+" and Cno="+Course_no;
  52. System.out.println(user.sql);
  53. count=user.statement.executeUpdate(user.sql);
  54. if(count==1) System.out.println("删除成功");
  55. else{
  56. System.out.println("删除失败");
  57. return false;
  58. }
  59. return true;
  60. }
  61. }

studentsystem:

  1. import java.util.Scanner;
  2. public class StudentSystem {
  3. public static void QueryStudentInfor(String id,User user) throws Exception{
  4. user.sql = "select * from scoreinfo where id=" + id;
  5. user.count = user.statement.executeQuery(user.sql);
  6. while(user.count.next()) {
  7. System.out.println(user.count.getString("name")
  8. + " " + user.count.getString("score")
  9. + " " + user.count.getString("Sno")
  10. + " " + user.count.getString("age")
  11. + " " + user.count.getString("grade"));
  12. }
  13. }
  14. }

courseFunction:

  1. import java.util.Scanner;
  2. public class CourseFunction {
  3. static private String Course_no;
  4. static private String Course_name;
  5. static private int Course_gpa;
  6. static private String Course_stratyear;
  7. static private int count;
  8. static private Scanner scanner = new Scanner(System.in);
  9. public static boolean Insert(User user) throws Exception {
  10. System.out.println("请输入课程号:");
  11. Course_no = "\'" + scanner.next() + "\',";
  12. System.out.println("请输入课程名:");
  13. Course_name = "\'" + scanner.next() + "\',";
  14. System.out.println("请输入课程的学分:");
  15. Course_gpa = scanner.nextInt();
  16. System.out.println("请输入课程的开设学年:");
  17. Course_stratyear = ",\'" + scanner.next() + "\'";
  18. user.sql = "insert into Couser values( " + Course_no + Course_name + Course_gpa + Course_stratyear + ")";
  19. count = user.statement.executeUpdate(user.sql);
  20. if (count == 1) {
  21. System.out.println("插入成功。");
  22. } else {
  23. System.out.println("插入失败。");
  24. return false;
  25. }
  26. return true;
  27. }
  28. public static boolean Select(User user) throws Exception {
  29. System.out.println("请输入课程号:");
  30. Course_no = "\'" + scanner.next() + "\'";
  31. user.sql = "select * from Couser where Couser_no = " + Course_no;
  32. user.count = user.statement.executeQuery(user.sql);
  33. if (user.count.next()) System.out.println(user.count.getString("Couser_name") +
  34. " 学分:" + user.count.getString("Couser_gpa") +
  35. " 开设学年:" + user.count.getString("Couser_stratyear"));
  36. else {
  37. System.out.println("查询失败");
  38. return false;
  39. }
  40. return true;
  41. }
  42. //删除课程
  43. public static boolean Delete(User user) throws Exception {
  44. System.out.println("请输入课程号:");
  45. Course_no = "\'" + scanner.next() + "\'";
  46. user.sql = "delete Couser where Couser_no=" + Course_no;
  47. System.out.println(user.sql);
  48. count = user.statement.executeUpdate(user.sql);
  49. if (count == 1) System.out.println("删除成功");
  50. else {
  51. System.out.println("删除失败");
  52. return false;
  53. }
  54. return true;
  55. }
  56. //修改课程
  57. public static boolean Alter(User user) throws Exception {
  58. System.out.println("请输入课程号:");
  59. String Course_nofirst = "'" + scanner.next() + "'";
  60. user.sql = "select * from Couser where Couser_no =" + Course_nofirst;
  61. try {
  62. user.count = user.statement.executeQuery(user.sql);
  63. } catch (Exception e) {
  64. System.out.println("查询失败。");
  65. return false;
  66. }
  67. if (user.count.next()) {
  68. Course_no = "\'" + user.count.getString("Couser_no") + "\',";
  69. Course_name = "\'" + user.count.getString("Couser_name") + "\',";
  70. Course_gpa = user.count.getInt("Couser_gpa");
  71. Course_stratyear = "\'" + user.count.getString("Couser_stratyear") + "\'";
  72. }
  73. boolean n = true;
  74. while (n) {
  75. System.out.println("请输入你要修改的信息的选项\n" +
  76. "1.课程号\n2.课程名\n3.学分\n4.开设学年\n5.完成退出");
  77. int choice = scanner.nextInt();
  78. switch (choice) {
  79. case (1):
  80. Course_no = "\'" + scanner.next() + "\',";
  81. break;
  82. case (2):
  83. Course_name = "\'" + scanner.next() + "\',";
  84. break;
  85. case (3):
  86. Course_gpa = scanner.nextInt();
  87. break;
  88. case (4):
  89. Course_stratyear = "\'" + scanner.next() + "\'";
  90. break;
  91. case (5):
  92. n = false;
  93. break;
  94. default:
  95. System.out.println("请输入正确的选项号。");
  96. }
  97. }
  98. user.sql = "update Couser set Couser_no = " + Course_no + " Couser_name = " + Course_name +
  99. " Couser_gpa=" + Course_gpa + ", Couser_stratyear=" + Course_stratyear +
  100. " where Couser_no = " + Course_nofirst;
  101. System.out.println(user.sql);
  102. if (user.statement.executeUpdate(user.sql) == 1) {
  103. System.out.println("修改成功。");
  104. } else {
  105. System.out.println("修改失败。");
  106. return false;
  107. }
  108. return true;
  109. }
  110. }

ManagerFunction:

  1. import java.util.Scanner;
  2. public class ManagerFunction {
  3. static private String Mno;
  4. static private String Mname;
  5. static private String Maccount;
  6. static private String Mtelephone;
  7. static private String Mage;
  8. static private String Msex;
  9. static private String code;
  10. static private String Mpower;
  11. static private Scanner scanner = new Scanner(System.in);
  12. public static boolean Select(User user) throws Exception {
  13. System.out.println("请选择以下选项:\n1.选择全部管理员的信息。\n2.指定查询管理员的信息。");
  14. int n = scanner.nextInt();
  15. if (n == 1) user.sql = "select * from Student";
  16. else {
  17. System.out.println("请输入要查询管理员的姓名。");
  18. String m = scanner.next();
  19. user.sql = "select * from Administrators where Sname =\'" + m + "\'";
  20. }
  21. try {
  22. user.count = user.statement.executeQuery(user.sql);
  23. } catch (Exception e) {
  24. System.out.println("查询失败");
  25. return false;
  26. }
  27. while (user.count.next()) {
  28. System.out.println("管理员编号:" + user.count.getString("Mno") + " "
  29. + user.count.getString("Mname") +
  30. " " + user.count.getString("Mage") + " "
  31. + user.count.getString("Msex")
  32. + user.count.getString("Mpower") +
  33. " 管理员账户号:" + user.count.getString("Maccount")
  34. + " 密码:" + user.count.getString("code") +
  35. " 电话:" + user.count.getString("Mtelephone"));
  36. }
  37. return true;
  38. }
  39. //添加新的管理员
  40. public static boolean Insert(User user) throws Exception {
  41. System.out.println("请输入你要插入的管理员编号:");
  42. while (true) {
  43. Mno = "\'"+scanner.next()+"\',";
  44. if (Mno.compareTo("\'\',")==0) System.out.println("管理员号不能为空!");
  45. else break;
  46. }
  47. System.out.println("请输入管理员姓名:");
  48. while (true) {
  49. Mname ="\'"+ scanner.next() + "\',";
  50. if (Mname.compareTo("\'\',")==0) System.out.println("管理员名字不能为空!");
  51. else break;
  52. }
  53. System.out.println("请输入管理员年龄:");
  54. while(true){
  55. Mage ="\'"+ scanner.next()+"\',";
  56. if(Mage.compareTo("\'\',") ==0) System.out.println("管理员年龄不能为空!");
  57. else break;
  58. }
  59. System.out.println("请输入管理员性别:");
  60. while(true){
  61. Msex ="\'"+ scanner.next()+"\',";
  62. if(Msex.compareTo("\'男\',")==0 || Msex.compareTo("\'女\',")==0) break;
  63. else System.out.println("性别必须为男或者女!");
  64. }
  65. System.out.println("请输入管理员的权限:");
  66. Mpower ="\'"+ scanner.next()+"\',";
  67. System.out.println("请输入管理员账户:");
  68. Maccount ="\'"+ scanner.next()+"\'";
  69. System.out.println("请输入管理员密码:");
  70. while(true){
  71. code ="\'"+ scanner.next()+"\',";
  72. if(code.compareTo("\'\',")==0) System.out.println("密码不能为空!");
  73. else break;
  74. }
  75. System.out.println("请输入管理员电话:");
  76. Mtelephone="\'"+ scanner.next()+"\'";
  77. user.sql = "insert into Administrators values ("+Mno+ Mname+Mage+Msex+Mpower+Maccount+code+Mtelephone+")";
  78. int count = user.statement.executeUpdate(user.sql);
  79. if(count == 1) System.out.println("插入成功。");
  80. else {
  81. System.out.println("插入失败。");
  82. return false;
  83. }
  84. return true;
  85. }
  86. }

TeacherFunction:

  1. import java.util.Scanner;
  2. public class TeacherFunction {
  3. static private String Tno;
  4. static private String Tname;
  5. static private String Tage;
  6. static private String Tsex;
  7. static private String Tdatatime;
  8. static private String code;
  9. static private String Telephone;
  10. static private Scanner scanner = new Scanner(System.in);
  11. //以上代码实验的是选择功能。
  12. public static boolean Select(User user) throws Exception {
  13. System.out.println("请选择以下选项:\n1.选择全部教师的信息。\n2.指定查询教师信息。");
  14. int n = scanner.nextInt();
  15. if (n == 1) user.sql = "select * from Teacher";
  16. else {
  17. System.out.println("请输入要查询教师的姓名。");
  18. String m = scanner.next();
  19. user.sql = "select * from Teacher where Tname =\'" + m + "\'";
  20. }
  21. try {
  22. user.count = user.statement.executeQuery(user.sql);
  23. } catch (Exception e) {
  24. System.out.println("查询失败");
  25. return false;
  26. }
  27. while(user.count.next()) {
  28. System.out.println("教师号:" + user.count.getString("Tno") + " " + user.count.getString("Tname") +
  29. " " + user.count.getString("Tage") + " " + user.count.getString("Tsex") +
  30. " 出生日期:" + user.count.getString("Tdatatime") + " 密码:" + user.count.getString("code") +
  31. " 电话:" + user.count.getString("Telephone"));
  32. }
  33. return true;
  34. }
  35. //以下代码实现的是插入功能。
  36. public static boolean Insert(User user) throws Exception {
  37. System.out.println("请输入你要插入的教师号:");
  38. while (true) {
  39. Tno = "\'"+scanner.next()+"\',";
  40. if (Tno.compareTo("\'\',")==0) System.out.println("教师号不能为空!");
  41. else break;
  42. }
  43. System.out.println("请输入教师姓名:");
  44. while (true) {
  45. Tname ="\'"+ scanner.next() + "\',";
  46. if (Tname.compareTo("\'\',")==0) System.out.println("教师名字不能为空!");
  47. else break;
  48. }
  49. System.out.println("请输入教师年龄:");
  50. while(true){
  51. Tage ="\'"+ scanner.next()+"\',";
  52. if(Tage.compareTo("\'\',") ==0) System.out.println("教师年龄不能为空!");
  53. else break;
  54. }
  55. System.out.println("请输入教师性别:");
  56. while(true){
  57. Tsex ="\'"+ scanner.next()+"\',";
  58. if(Tsex.compareTo("\'男\',")==0 || Tsex.compareTo("\'女\',")==0) break;
  59. else System.out.println("性别必须为男或者女!");
  60. }
  61. System.out.println("请输入教师的出生日期(输入格式:20030419:");
  62. Tdatatime ="\'"+ scanner.next()+"\',";
  63. System.out.println("请输入教师密码:");
  64. while(true){
  65. code ="\'"+ scanner.next()+"\',";
  66. if(code.compareTo("\'\',")==0) System.out.println("密码不能为空!");
  67. else break;
  68. }
  69. System.out.println("请输入教师电话:");
  70. Telephone ="\'"+ scanner.next()+"\'";
  71. user.sql = "insert into Teacher values ("+Tno+ Tname+Tage+Tsex+Tdatatime+code+Telephone+")";
  72. int count = user.statement.executeUpdate(user.sql);
  73. if(count == 1) System.out.println("插入成功。");
  74. else {
  75. System.out.println("插入失败。");
  76. return false;
  77. }
  78. return true;
  79. }
  80. //删除教师功能
  81. public static boolean Delete(User user) throws Exception{
  82. System.out.println("请提供要删除的教师的教师号。");
  83. Tno ="\'" + scanner.next()+"\'";
  84. user.sql = "delete Teacher where Tno ="+Tno;
  85. int count = user.statement.executeUpdate(user.sql);
  86. if(count == 1) {
  87. System.out.println("删除成功。");
  88. }else{
  89. System.out.println("删除失败。");
  90. return false;
  91. }
  92. return true;
  93. }
  94. //修改教师信息功能
  95. public static boolean Alter(User user) throws Exception{
  96. System.out.println("请输入你要修改教师的教师号");
  97. String Tnofrist ="\'"+scanner.next()+"\'";
  98. user.sql = "select * from Teacher where Tno =" + Tnofrist;
  99. try{
  100. user.count=user.statement.executeQuery(user.sql);
  101. }catch(Exception e){
  102. System.out.println("查询失败。");
  103. return false;
  104. }
  105. if(user.count.next()){
  106. Tno = "\'"+user.count.getString("Tno")+"\',";
  107. Tname = "\'"+user.count.getString("Tname")+"\',";
  108. Tage = "\'"+user.count.getString("Tage")+"\',";
  109. Tsex = "\'"+user.count.getString("Tsex")+"\',";
  110. Tdatatime = "\'"+user.count.getString("Tdatatime")+"\',";
  111. code = "\'"+user.count.getString("code")+"\',";
  112. Telephone = "\'"+user.count.getString("Telephone")+"\'";
  113. }
  114. boolean n = true;//case(8)处用来跳出循环
  115. while (n) {
  116. System.out.println("请输入你要修改的信息的选项\n" +
  117. "1.教师号\n2.教师姓名\n3.教师年龄\n4.教师性别\n5.出生日期\n6.密码\n7.电话\n8.完成退出");
  118. int choice = scanner.nextInt();
  119. switch (choice){
  120. case(1): Tno ="\'"+ scanner.next()+"\',"; break;
  121. case(2): Tname ="\'"+ scanner.next()+"\',";break;
  122. case(3): Tage = "\'"+scanner.next()+"\',";break;
  123. case(4): Tsex = "\'"+scanner.next()+"\',";break;
  124. case(5): Tdatatime = "\'"+scanner.next()+"\',";break;
  125. case(6): code = "\'"+scanner.next()+"\',";break;
  126. case(7): Telephone= "\'"+scanner.next()+"\'";break;
  127. case(8): n=false;break;
  128. default: System.out.println("请输入正确的选项号。");
  129. }
  130. }
  131. user.sql="update Teacher set Tno = "+Tno+" Tname = "+Tname +
  132. " Tage="+Tage+" Tsex="+Tsex+
  133. " Tdatatime="+Tdatatime+" code="+code+
  134. " Telephone="+Telephone+" where Tno = "+Tnofrist;
  135. System.out.println(user.sql);
  136. if(user.statement.executeUpdate(user.sql)==1){
  137. System.out.println("修改成功。");
  138. }else{
  139. System.out.println("修改失败。");
  140. return false;
  141. }
  142. return true;
  143. }
  144. }

2、数据库建立连接代码部分:

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.sql.ResultSet;
  6. public class User {
  7. public String user = "user=zhang;";
  8. public String password = "password=123456;";
  9. public String databasename = "databaseName=administartor;";
  10. public String encrypt = "encrypt=false;";
  11. String connectionUrl = "jdbc:sqlserver://localhost:1433;" + databasename + user + password + encrypt;
  12. Statement statement;
  13. Connection connection;
  14. String sql ="";
  15. ResultSet count;
  16. public User() {
  17. Connection();
  18. try {
  19. statement = connection.createStatement();
  20. System.out.println("statement对象创建成功");
  21. }catch(Exception e) {
  22. System.out.println("statement对象创建失败");
  23. }
  24. }
  25. public boolean Connection() {
  26. try {
  27. connection = DriverManager.getConnection(connectionUrl);
  28. System.out.println("连接成功");
  29. }
  30. catch (SQLException e) {
  31. System.out.println("连接失败");
  32. return false;
  33. }
  34. return true;
  35. }
  36. public boolean Close() {
  37. try {
  38. statement.close();
  39. connection.close();
  40. System.out.println("关闭成功");
  41. }catch(Exception e){
  42. System.out.println("关闭失败");
  43. return false;
  44. }
  45. return true;
  46. }
  47. }

3、Java开发部分:

系统测试:(终端):

  1. import java.util.Scanner;
  2. public class Test4 {
  3. public static void Test(String Tno) throws Exception
  4. {
  5. Scanner scanner = new Scanner(System.in);
  6. User user = new User();
  7. System.out.println("欢迎来到学生选课系统");
  8. // System.out.println("请你先输入姓名和密码登陆系统:");
  9. // System.out.print("请输入你的id:");
  10. // String id = scanner.next();
  11. // System.out.print("请输入你的password:");
  12. // String code = scanner.next();
  13. System.out.println("###########################");
  14. System.out.println(" 1.");
  15. System.out.println(" 2.");
  16. System.out.println(" 3.");
  17. System.out.println(" 4.");
  18. System.out.println(" 5.");
  19. System.out.println("###########################");
  20. System.out.println("请选择你要的服务:");
  21. String select = scanner.next();
  22. while(select != "6")
  23. {
  24. if(select.equals("1")){
  25. System.out.println("--------------------------");
  26. caozuo m1 = new caozuo();
  27. m1.Query();
  28. System.out.println("--------------------------");
  29. }
  30. else if(select.equals("2")){
  31. System.out.println("--------------------------");
  32. caozuo m1 = new caozuo();
  33. m1.QuerybyTno(Tno);
  34. System.out.println("--------------------------");
  35. }
  36. if(select.equals("3")){
  37. System.out.println("--------------------------");
  38. caozuo m1 = new caozuo();
  39. m1.chaxunrenke();
  40. System.out.println("--------------------------");
  41. }
  42. else if(select.equals("4")){
  43. System.out.println("--------------------------");
  44. caozuo m1 = new caozuo();
  45. m1.findByTno(Tno);
  46. System.out.println("--------------------------");
  47. }
  48. else if(select.equals("5")){
  49. System.out.println("--------------------------");
  50. caozuo m1 = new caozuo();
  51. m1.pingfen();
  52. System.out.println("--------------------------");
  53. }
  54. else if(select.equals("6")){
  55. break;
  56. }
  57. else {
  58. System.out.println("输入错误,请重新输入");
  59. }
  60. System.out.println("请选择你要的服务:");
  61. select = scanner.next();
  62. }
  63. System.out.println("--------------------------");
  64. System.out.println("成功退出系统,欢迎再次光临!");
  65. System.out.println("--------------------------");
  66. }
  67. }

4、gui设计部分:

总页面:(登陆页面):

  1. import java.awt.*;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. public class kk {
  5. public static void main(String args[]) throws Exception {
  6. User user=new User(); /*
  7. Scanner scanner=new Scanner(System.in);
  8. System.out.println("欢迎来到学生选课系统");
  9. System.out.println("即将进入登陆界面,请按提示操作");
  10. System.out.println("请选择您的身份:");
  11. System.out.println("1、管理员");
  12. System.out.println("2、学生");
  13. System.out.println("3、教师");
  14. System.out.println("0、退出服务");
  15. int n=scanner.nextInt();
  16. System.out.print("请输入你的id:");
  17. String id = scanner.next();
  18. System.out.println("请输入你的密码:");
  19. String code = scanner.next();
  20. switch (n){
  21. case(1):
  22. if(Log(user,n,id,code));
  23. Monitor monitor = new Monitor(user);
  24. monitor.Interface();
  25. break;
  26. case(2):
  27. if(Log(user,n,id,code));
  28. Ztest.StudentInterface();
  29. break;
  30. case(3):
  31. if(Log(user,n,id,code));
  32. Test1 teacher= new Test1();
  33. teacher.MonitorInterface();
  34. break;
  35. case(0):
  36. return;
  37. }
  38. user.Close();
  39. System.out.println("--------------------------");
  40. System.out.println("成功退出系统,欢迎再次光临!");
  41. System.out.println("--------------------------");*/
  42. LogIn login = new LogIn();
  43. login.log();
  44. }
  45. public static boolean Log(User user,int n,String id,String code) throws Exception{
  46. switch (n){
  47. case(1):
  48. user.sql="select * from administrators where Sno ='"+id+"' and code = '"+code+"'";
  49. user.count=user.statement.executeQuery(user.sql);
  50. if(user.count.next()) {
  51. System.out.println("登入成功。");
  52. return true;
  53. }
  54. else {
  55. System.out.println("登入失败。");
  56. return false;
  57. }
  58. case(2):
  59. user.sql="select * from Student where Sno ='"+id+"' and code = '"+code+"'";
  60. user.count=user.statement.executeQuery(user.sql);
  61. if(user.count.next()) {
  62. System.out.println("登入成功。");
  63. return true;
  64. }
  65. else {
  66. System.out.println("登入失败。");
  67. return false;
  68. }
  69. case(3):
  70. user.sql="select * from Teacher where Tno ='"+id+"' and code = '"+code+"'";
  71. user.count=user.statement.executeQuery(user.sql);
  72. if(user.count.next()) {
  73. System.out.println("登入成功。");
  74. return true;
  75. }
  76. else {
  77. System.out.println("登入失败。");
  78. return false;
  79. }
  80. }
  81. return false;
  82. }
  83. }

管理系统页面:

  1. import java.awt.*;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. public class kk {
  5. public static void main(String args[]) throws Exception {
  6. User user=new User(); /*
  7. Scanner scanner=new Scanner(System.in);
  8. System.out.println("欢迎来到学生选课系统");
  9. System.out.println("即将进入登陆界面,请按提示操作");
  10. System.out.println("请选择您的身份:");
  11. System.out.println("1、管理员");
  12. System.out.println("2、学生");
  13. System.out.println("3、教师");
  14. System.out.println("0、退出服务");
  15. int n=scanner.nextInt();
  16. System.out.print("请输入你的id:");
  17. String id = scanner.next();
  18. System.out.println("请输入你的密码:");
  19. String code = scanner.next();
  20. switch (n){
  21. case(1):
  22. if(Log(user,n,id,code));
  23. Monitor monitor = new Monitor(user);
  24. monitor.Interface();
  25. break;
  26. case(2):
  27. if(Log(user,n,id,code));
  28. Ztest.StudentInterface();
  29. break;
  30. case(3):
  31. if(Log(user,n,id,code));
  32. Test1 teacher= new Test1();
  33. teacher.MonitorInterface();
  34. break;
  35. case(0):
  36. return;
  37. }
  38. user.Close();
  39. System.out.println("--------------------------");
  40. System.out.println("成功退出系统,欢迎再次光临!");
  41. System.out.println("--------------------------");*/
  42. LogIn login = new LogIn();
  43. login.log();
  44. }
  45. public static boolean Log(User user,int n,String id,String code) throws Exception{
  46. switch (n){
  47. case(1):
  48. user.sql="select * from administrators where Sno ='"+id+"' and code = '"+code+"'";
  49. user.count=user.statement.executeQuery(user.sql);
  50. if(user.count.next()) {
  51. System.out.println("登入成功。");
  52. return true;
  53. }
  54. else {
  55. System.out.println("登入失败。");
  56. return false;
  57. }
  58. case(2):
  59. user.sql="select * from Student where Sno ='"+id+"' and code = '"+code+"'";
  60. user.count=user.statement.executeQuery(user.sql);
  61. if(user.count.next()) {
  62. System.out.println("登入成功。");
  63. return true;
  64. }
  65. else {
  66. System.out.println("登入失败。");
  67. return false;
  68. }
  69. case(3):
  70. user.sql="select * from Teacher where Tno ='"+id+"' and code = '"+code+"'";
  71. user.count=user.statement.executeQuery(user.sql);
  72. if(user.count.next()) {
  73. System.out.println("登入成功。");
  74. return true;
  75. }
  76. else {
  77. System.out.println("登入失败。");
  78. return false;
  79. }
  80. }
  81. return false;
  82. }
  83. }

管理员页面:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. public class AdminGUI {
  6. private JFrame frame;
  7. private JTextField adminIdField;
  8. private JTextField nameField;
  9. private JTextField accountField;
  10. private JTextField phoneField;
  11. private JTextField ageField;
  12. private JTextField genderField;
  13. private JPasswordField passwordField;
  14. private JTextField permissionField;
  15. private JTextArea outputTextArea;
  16. public AdminGUI() {
  17. frame = new JFrame("管理员界面");
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. frame.setSize(400, 300);
  20. frame.setLayout(new BorderLayout());
  21. JPanel inputPanel = new JPanel(new GridLayout(9, 2));
  22. inputPanel.add(new JLabel("管理员ID:"));
  23. adminIdField = new JTextField();
  24. inputPanel.add(adminIdField);
  25. inputPanel.add(new JLabel("姓名:"));
  26. nameField = new JTextField();
  27. inputPanel.add(nameField);
  28. inputPanel.add(new JLabel("账号:"));
  29. accountField = new JTextField();
  30. inputPanel.add(accountField);
  31. inputPanel.add(new JLabel("电话:"));
  32. phoneField = new JTextField();
  33. inputPanel.add(phoneField);
  34. inputPanel.add(new JLabel("年龄:"));
  35. ageField = new JTextField();
  36. inputPanel.add(ageField);
  37. inputPanel.add(new JLabel("性别:"));
  38. genderField = new JTextField();
  39. inputPanel.add(genderField);
  40. inputPanel.add(new JLabel("密码:"));
  41. passwordField = new JPasswordField();
  42. inputPanel.add(passwordField);
  43. inputPanel.add(new JLabel("权限:"));
  44. permissionField = new JTextField();
  45. inputPanel.add(permissionField);
  46. JButton viewInfoButton = new JButton("查看学生信息");
  47. viewInfoButton.addActionListener(new ActionListener() {
  48. public void actionPerformed(ActionEvent e) {
  49. // 在此处编写查看学生信息的逻辑
  50. outputTextArea.setText("学生信息:");
  51. }
  52. });
  53. inputPanel.add(viewInfoButton);
  54. JButton addAdminButton = new JButton("添加新管理员");
  55. addAdminButton.addActionListener(new ActionListener() {
  56. public void actionPerformed(ActionEvent e) {
  57. // 在此处编写添加新管理员的逻辑
  58. outputTextArea.setText("已添加新管理员:" + nameField.getText());
  59. }
  60. });
  61. inputPanel.add(addAdminButton);
  62. frame.add(inputPanel, BorderLayout.NORTH);
  63. outputTextArea = new JTextArea();
  64. frame.add(outputTextArea, BorderLayout.CENTER);
  65. frame.setVisible(true);
  66. }
  67. public static void main(String[] args) {
  68. SwingUtilities.invokeLater(new Runnable() {
  69. public void run() {
  70. new AdminGUI();
  71. }
  72. });
  73. }
  74. }

课程管理页面:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class CourseGUI {
  8. public JFrame frame;
  9. private JTextField idTextField;
  10. private JTextField nameTextField;
  11. private JTextField creditTextField;
  12. private JTextField yearTextField;
  13. private JTextArea outputTextArea;
  14. private List<Course> courseList;
  15. public static void main(String[] args) {
  16. EventQueue.invokeLater(() -> {
  17. try {
  18. CourseGUI window = new CourseGUI();
  19. window.frame.setVisible(true);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. });
  24. }
  25. public CourseGUI() {
  26. initialize();
  27. courseList = new ArrayList<>();
  28. }
  29. private void initialize() {
  30. frame = new JFrame();
  31. frame.setBounds(100, 100, 450, 300);
  32. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. frame.getContentPane().setLayout(null);
  34. JLabel lblNewLabel = new JLabel("课程ID:");
  35. lblNewLabel.setBounds(10, 11, 64, 14);
  36. frame.getContentPane().add(lblNewLabel);
  37. idTextField = new JTextField();
  38. idTextField.setBounds(84, 8, 86, 20);
  39. frame.getContentPane().add(idTextField);
  40. idTextField.setColumns(10);
  41. JLabel lblNewLabel_1 = new JLabel("名称:");
  42. lblNewLabel_1.setBounds(10, 36, 64, 14);
  43. frame.getContentPane().add(lblNewLabel_1);
  44. nameTextField = new JTextField();
  45. nameTextField.setBounds(84, 33, 86, 20);
  46. frame.getContentPane().add(nameTextField);
  47. nameTextField.setColumns(10);
  48. JLabel lblNewLabel_2 = new JLabel("学分:");
  49. lblNewLabel_2.setBounds(10, 61, 64, 14);
  50. frame.getContentPane().add(lblNewLabel_2);
  51. creditTextField = new JTextField();
  52. creditTextField.setBounds(84, 58, 86, 20);
  53. frame.getContentPane().add(creditTextField);
  54. creditTextField.setColumns(10);
  55. JLabel lblNewLabel_3 = new JLabel("开设学年:");
  56. lblNewLabel_3.setBounds(10, 86, 64, 14);
  57. frame.getContentPane().add(lblNewLabel_3);
  58. yearTextField = new JTextField();
  59. yearTextField.setBounds(84, 83, 86, 20);
  60. frame.getContentPane().add(yearTextField);
  61. yearTextField.setColumns(10);
  62. JButton addButton = new JButton("添加课程");
  63. addButton.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent e) {
  65. addCourse();
  66. }
  67. });
  68. addButton.setBounds(10, 116, 120, 23);
  69. frame.getContentPane().add(addButton);
  70. JButton queryButton = new JButton("查询课程");
  71. queryButton.addActionListener(new ActionListener() {
  72. public void actionPerformed(ActionEvent e) {
  73. queryCourse();
  74. }
  75. });
  76. queryButton.setBounds(140, 116, 120, 23);
  77. frame.getContentPane().add(queryButton);
  78. JButton assignButton = new JButton("分配课程给教师");
  79. assignButton.addActionListener(new ActionListener() {
  80. public void actionPerformed(ActionEvent e) {
  81. assignCourse();
  82. }
  83. });
  84. assignButton.setBounds(10, 150, 150, 23);
  85. frame.getContentPane().add(assignButton);
  86. JButton cancelButton = new JButton("取消教师的课程");
  87. cancelButton.addActionListener(new ActionListener() {
  88. public void actionPerformed(ActionEvent e) {
  89. cancelCourse();
  90. }
  91. });
  92. cancelButton.setBounds(170, 150, 150, 23);
  93. frame.getContentPane().add(cancelButton);
  94. JScrollPane scrollPane = new JScrollPane();
  95. scrollPane.setBounds(240, 8, 184, 242);
  96. frame.getContentPane().add(scrollPane);
  97. outputTextArea = new JTextArea();
  98. scrollPane.setViewportView(outputTextArea);
  99. }
  100. private void addCourse() {
  101. String id = idTextField.getText();
  102. String name = nameTextField.getText();
  103. int credit = Integer.parseInt(creditTextField.getText());
  104. int year = Integer.parseInt(yearTextField.getText());
  105. Course course = new Course(id, name, credit, year);
  106. courseList.add(course);
  107. outputTextArea.append("课程已添加:" + course.toString() + "\n");
  108. clearFields();
  109. }
  110. private void queryCourse() {
  111. String id = idTextField.getText();
  112. String name = nameTextField.getText();
  113. boolean courseFound = false;
  114. for (Course course : courseList) {
  115. if (course.getId().equals(id) || course.getName().equals(name)) {
  116. outputTextArea.append("查询到课程:" + course.toString() + "\n");
  117. courseFound = true;
  118. }
  119. }
  120. if (!courseFound) {
  121. outputTextArea.append("找不到指定课程。\n");
  122. }
  123. clearFields();
  124. }
  125. private void assignCourse() {
  126. String id = idTextField.getText();
  127. String name = nameTextField.getText();
  128. boolean courseAssigned = false;
  129. for (Course course : courseList) {
  130. if (course.getId().equals(id) || course.getName().equals(name)) {
  131. // Perform teacher assignment logic here
  132. outputTextArea.append("已将课程分配给教师:" + course.toString() + "\n");
  133. courseAssigned = true;
  134. }
  135. }
  136. if (!courseAssigned) {
  137. outputTextArea.append("找不到指定课程。\n");
  138. }
  139. clearFields();
  140. }
  141. private void cancelCourse() {
  142. String id = idTextField.getText();
  143. String name = nameTextField.getText();
  144. boolean courseCanceled = false;
  145. for (Course course : courseList) {
  146. if (course.getId().equals(id) || course.getName().equals(name)) {
  147. // Perform cancellation logic here
  148. outputTextArea.append("已取消教师的课程:" + course.toString() + "\n");
  149. courseCanceled = true;
  150. }
  151. }
  152. if (!courseCanceled) {
  153. outputTextArea.append("找不到指定课程。\n");
  154. }
  155. clearFields();
  156. }
  157. private void clearFields() {
  158. idTextField.setText("");
  159. nameTextField.setText("");
  160. creditTextField.setText("");
  161. yearTextField.setText("");
  162. }
  163. }
  164. class Course {
  165. private String id;
  166. private String name;
  167. private int credit;
  168. private int year;
  169. public Course(String id, String name, int credit, int year) {
  170. this.id = id;
  171. this.name = name;
  172. this.credit = credit;
  173. this.year = year;
  174. }
  175. public String getId() {
  176. return id;
  177. }
  178. public String getName() {
  179. return name;
  180. }
  181. public int getCredit() {
  182. return credit;
  183. }
  184. public int getYear() {
  185. return year;
  186. }
  187. @Override
  188. public String toString() {
  189. return "课程ID: " + id + ", 名称: " + name + ", 学分: " + credit + ", 开设学年: " + year;
  190. }
  191. }

学生管理页面:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class StudentManagementGUI extends JFrame {
  8. private JButton addStudentButton;
  9. private JButton searchStudentButton;
  10. private JButton deleteStudentButton;
  11. private JButton updateStudentButton;
  12. private List<Student> studentList;
  13. public StudentManagementGUI() {
  14. // 设置窗口标题
  15. setTitle("Student Management System");
  16. // 创建界面组件
  17. addStudentButton = new JButton("Add Student");
  18. searchStudentButton = new JButton("Search Student");
  19. deleteStudentButton = new JButton("Delete Student");
  20. updateStudentButton = new JButton("Update Student");
  21. // 设置布局管理器
  22. setLayout(new GridLayout(4, 1));
  23. // 添加组件到界面
  24. add(addStudentButton);
  25. add(searchStudentButton);
  26. add(deleteStudentButton);
  27. add(updateStudentButton);
  28. // 初始化学生列表
  29. studentList = new ArrayList<>();
  30. // 添加添加学生按钮的事件监听器
  31. addStudentButton.addActionListener(new ActionListener() {
  32. @Override
  33. public void actionPerformed(ActionEvent e) {
  34. AddStudentDialog dialog = new AddStudentDialog(StudentManagementGUI.this);
  35. dialog.setVisible(true);
  36. }
  37. });
  38. // 添加查询学生按钮的事件监听器
  39. searchStudentButton.addActionListener(new ActionListener() {
  40. @Override
  41. public void actionPerformed(ActionEvent e) {
  42. String input = JOptionPane.showInputDialog(StudentManagementGUI.this,
  43. "Enter student ID or name:",
  44. "Search Student",
  45. JOptionPane.PLAIN_MESSAGE);
  46. if (input != null && !input.isEmpty()) {
  47. List<Student> searchResults = new ArrayList<>();
  48. for (Student student : studentList) {
  49. if (student.getId().equals(input) || student.getName().equals(input)) {
  50. searchResults.add(student);
  51. }
  52. }
  53. if (!searchResults.isEmpty()) {
  54. StringBuilder result = new StringBuilder();
  55. for (Student student : searchResults) {
  56. result.append(student).append("\n");
  57. }
  58. JOptionPane.showMessageDialog(StudentManagementGUI.this,
  59. result.toString(),
  60. "Search Results",
  61. JOptionPane.INFORMATION_MESSAGE);
  62. } else {
  63. JOptionPane.showMessageDialog(StudentManagementGUI.this,
  64. "No matching students found.",
  65. "Search Results",
  66. JOptionPane.INFORMATION_MESSAGE);
  67. }
  68. }
  69. }
  70. });
  71. // 添加删除学生按钮的事件监听器
  72. deleteStudentButton.addActionListener(new ActionListener() {
  73. @Override
  74. public void actionPerformed(ActionEvent e) {
  75. String id = JOptionPane.showInputDialog(StudentManagementGUI.this,
  76. "Enter student ID:",
  77. "Delete Student",
  78. JOptionPane.PLAIN_MESSAGE);
  79. String name = JOptionPane.showInputDialog(StudentManagementGUI.this,
  80. "Enter student name:",
  81. "Delete Student",
  82. JOptionPane.PLAIN_MESSAGE);
  83. if (id != null && !id.isEmpty() && name != null && !name.isEmpty()) {
  84. boolean deleted = false;
  85. for (int i = 0; i < studentList.size(); i++) {
  86. Student student = studentList.get(i);
  87. if (student.getId().equals(id) && student.getName().equals(name)) {
  88. studentList.remove(i);
  89. deleted = true;
  90. break;
  91. }
  92. }
  93. if (deleted) {
  94. JOptionPane.showMessageDialog(StudentManagementGUI.this,
  95. "Student deleted successfully.",
  96. "Delete Student",
  97. JOptionPane.INFORMATION_MESSAGE);
  98. } else {
  99. JOptionPane.showMessageDialog(StudentManagementGUI.this,
  100. "Student not found.",
  101. "Delete Student",
  102. JOptionPane.ERROR_MESSAGE);
  103. }
  104. }
  105. }
  106. });
  107. // 添加修改学生按钮的事件监听器
  108. updateStudentButton.addActionListener(new ActionListener() {
  109. @Override
  110. public void actionPerformed(ActionEvent e) {
  111. String id = JOptionPane.showInputDialog(StudentManagementGUI.this,
  112. "Enter student ID or name:",
  113. "Update Student",
  114. JOptionPane.PLAIN_MESSAGE);
  115. if (id != null && !id.isEmpty()) {
  116. Student studentToUpdate = null;
  117. for (Student student : studentList) {
  118. if (student.getId().equals(id) || student.getName().equals(id)) {
  119. studentToUpdate = student;
  120. break;
  121. }
  122. }
  123. if (studentToUpdate != null) {
  124. UpdateStudentDialog dialog = new UpdateStudentDialog(StudentManagementGUI.this, studentToUpdate);
  125. dialog.setVisible(true);
  126. } else {
  127. JOptionPane.showMessageDialog(StudentManagementGUI.this,
  128. "Student not found.",
  129. "Update Student",
  130. JOptionPane.ERROR_MESSAGE);
  131. }
  132. }
  133. }
  134. });
  135. // 设置窗口大小、可见性和关闭操作
  136. pack();
  137. setVisible(true);
  138. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  139. }
  140. // 添加学生对话框
  141. private class AddStudentDialog extends JDialog {
  142. private JTextField idField;
  143. private JPasswordField passwordField;
  144. private JTextField nameField;
  145. private JTextField phoneField;
  146. private JTextField genderField;
  147. private JTextField dobField;
  148. private JTextField classField;
  149. private JButton addButton;
  150. public AddStudentDialog(JFrame parent) {
  151. super(parent, "Add Student", true);
  152. // 创建对话框组件
  153. idField = new JTextField(20);
  154. passwordField = new JPasswordField(20);
  155. nameField = new JTextField(20);
  156. phoneField = new JTextField(20);
  157. genderField = new JTextField(20);
  158. dobField = new JTextField(20);
  159. classField = new JTextField(20);
  160. addButton = new JButton("Add");
  161. // 设置布局管理器
  162. setLayout(new GridLayout(8, 2));
  163. // 添加组件到对话框
  164. add(new JLabel("ID:"));
  165. add(idField);
  166. add(new JLabel("Password:"));
  167. add(passwordField);
  168. add(new JLabel("Name:"));
  169. add(nameField);
  170. add(new JLabel("Phone:"));
  171. add(phoneField);
  172. add(new JLabel("Gender:"));
  173. add(genderField);
  174. add(new JLabel("DOB:"));
  175. add(dobField);
  176. add(new JLabel("Class:"));
  177. add(classField);
  178. add(new JLabel());
  179. add(addButton);
  180. // 添加添加按钮的事件监听器
  181. addButton.addActionListener(new ActionListener() {
  182. @Override
  183. public void actionPerformed(ActionEvent e) {
  184. // 从输入框获取学生信息
  185. String id = idField.getText();
  186. String password = new String(passwordField.getPassword());
  187. String name = nameField.getText();
  188. String phone = phoneField.getText();
  189. String gender = genderField.getText();
  190. String dob = dobField.getText();
  191. String className = classField.getText();
  192. // 创建学生对象并添加到学生列表
  193. Student newStudent = new Student(id, password, name, phone, gender, dob, className);
  194. studentList.add(newStudent);
  195. // 清空输入框
  196. idField.setText("");
  197. passwordField.setText("");
  198. nameField.setText("");
  199. phoneField.setText("");
  200. genderField.setText("");
  201. dobField.setText("");
  202. classField.setText("");
  203. // 关闭对话框
  204. dispose();
  205. JOptionPane.showMessageDialog(StudentManagementGUI.this,
  206. "Student added successfully.",
  207. "Add Student",
  208. JOptionPane.INFORMATION_MESSAGE);
  209. }
  210. });
  211. // 设置对话框大小、可见性和关闭操作
  212. pack();
  213. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  214. setLocationRelativeTo(parent);
  215. }
  216. }
  217. // 修改学生对话框
  218. private class UpdateStudentDialog extends JDialog {
  219. private JTextField idField;
  220. private JTextField passwordField;
  221. private JTextField nameField;
  222. private JTextField phoneField;
  223. private JTextField genderField;
  224. private JTextField dobField;
  225. private JTextField classField;
  226. private JButton updateButton;
  227. public UpdateStudentDialog(JFrame parent, Student student) {
  228. super(parent, "Update Student", true);
  229. // 创建对话框组件
  230. idField = new JTextField(student.getId(), 20);
  231. passwordField = new JTextField(student.getPassword(), 20);
  232. nameField = new JTextField(student.getName(), 20);
  233. phoneField = new JTextField(student.getPhone(), 20);
  234. genderField = new JTextField(student.getGender(), 20);
  235. dobField = new JTextField(student.getDob(), 20);
  236. classField = new JTextField(student.getClassName(), 20);
  237. updateButton = new JButton("Update");
  238. // 设置布局管理器
  239. setLayout(new GridLayout(8, 2));
  240. // 添加组件到对话框
  241. add(new JLabel("ID:"));
  242. add(idField);
  243. add(new JLabel("Password:"));
  244. add(passwordField);
  245. add(new JLabel("Name:"));
  246. add(nameField);
  247. add(new JLabel("Phone:"));
  248. add(phoneField);
  249. add(new JLabel("Gender:"));
  250. add(genderField);
  251. add(new JLabel("DOB:"));
  252. add(dobField);
  253. add(new JLabel("Class:"));
  254. add(classField);
  255. add(new JLabel());
  256. add(updateButton);
  257. // 添加更新按钮的事件监听器
  258. updateButton.addActionListener(new ActionListener() {
  259. @Override
  260. public void actionPerformed(ActionEvent e) {
  261. // 获取修改后的学生信息
  262. String id = idField.getText();
  263. String password = passwordField.getText();
  264. String name = nameField.getText();
  265. String phone = phoneField.getText();
  266. String gender = genderField.getText();
  267. String dob = dobField.getText();
  268. String className = classField.getText();
  269. // 更新学生信息
  270. student.setId(id);
  271. student.setPassword(password);
  272. student.setName(name);
  273. student.setPhone(phone);
  274. student.setGender(gender);
  275. student.setDob(dob);
  276. student.setClassName(className);
  277. // 关闭对话框
  278. dispose();
  279. JOptionPane.showMessageDialog(StudentManagementGUI.this,
  280. "Student updated successfully.",
  281. "Update Student",
  282. JOptionPane.INFORMATION_MESSAGE);
  283. }
  284. });
  285. // 设置对话框大小、可见性和关闭操作
  286. pack();
  287. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  288. setLocationRelativeTo(parent);
  289. }
  290. }
  291. public static void main(String[] args) {
  292. SwingUtilities.invokeLater(new Runnable() {
  293. @Override
  294. public void run() {
  295. new StudentManagementGUI();
  296. }
  297. });
  298. }
  299. }
  300. // 学生类
  301. class Student {
  302. private String id;
  303. private String password;
  304. private String name;
  305. private String phone;
  306. private String gender;
  307. private String dob;
  308. private String className;
  309. public Student(String id, String password, String name, String phone, String gender, String dob, String className) {
  310. this.id = id;
  311. this.password = password;
  312. this.name = name;
  313. this.phone = phone;
  314. this.gender = gender;
  315. this.dob = dob;
  316. this.className = className;
  317. }
  318. // Getter和Setter方法
  319. public String getId() {
  320. return id;
  321. }
  322. public void setId(String id) {
  323. this.id = id;
  324. }
  325. public String getPassword() {
  326. return password;
  327. }
  328. public void setPassword(String password) {
  329. this.password = password;
  330. }
  331. public String getName() {
  332. return name;
  333. }
  334. public void setName(String name) {
  335. this.name = name;
  336. }
  337. public String getPhone() {
  338. return phone;
  339. }
  340. public void setPhone(String phone) {
  341. this.phone = phone;
  342. }
  343. public String getGender() {
  344. return gender;
  345. }
  346. public void setGender(String gender) {
  347. this.gender = gender;
  348. }
  349. public String getDob() {
  350. return dob;
  351. }
  352. public void setDob(String dob) {
  353. this.dob = dob;
  354. }
  355. public String getClassName() {
  356. return className;
  357. }
  358. public void setClassName(String className) {
  359. this.className = className;
  360. }
  361. // 重写toString()方法,用于显示学生信息
  362. @Override
  363. public String toString() {
  364. return "Student{" +
  365. "id='" + id + '\'' +
  366. ", password='" + password + '\'' +
  367. ", name='" + name + '\'' +
  368. ", phone='" + phone + '\'' +
  369. ", gender='" + gender + '\'' +
  370. ", dob='" + dob + '\'' +
  371. ", className='" + className + '\'' +
  372. '}';
  373. }
  374. }

教师管理页面:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class TeacherGUI {
  8. public JFrame frame;
  9. private JTextField idTextField;
  10. private JTextField nameTextField;
  11. private JTextField passwordTextField;
  12. private JTextField phoneTextField;
  13. private JTextArea outputTextArea;
  14. private List<Teacher> teacherList;
  15. public static void main(String[] args) {
  16. EventQueue.invokeLater(() -> {
  17. try {
  18. TeacherGUI window = new TeacherGUI();
  19. window.frame.setVisible(true);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. });
  24. }
  25. public TeacherGUI() {
  26. initialize();
  27. teacherList = new ArrayList<>();
  28. }
  29. private void initialize() {
  30. frame = new JFrame();
  31. frame.setBounds(100, 100, 450, 300);
  32. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. frame.getContentPane().setLayout(null);
  34. JLabel lblNewLabel = new JLabel("教师ID:");
  35. lblNewLabel.setBounds(10, 11, 64, 14);
  36. frame.getContentPane().add(lblNewLabel);
  37. idTextField = new JTextField();
  38. idTextField.setBounds(84, 8, 86, 20);
  39. frame.getContentPane().add(idTextField);
  40. idTextField.setColumns(10);
  41. JLabel lblNewLabel_1 = new JLabel("姓名:");
  42. lblNewLabel_1.setBounds(10, 36, 64, 14);
  43. frame.getContentPane().add(lblNewLabel_1);
  44. nameTextField = new JTextField();
  45. nameTextField.setBounds(84, 33, 86, 20);
  46. frame.getContentPane().add(nameTextField);
  47. nameTextField.setColumns(10);
  48. JLabel lblNewLabel_2 = new JLabel("密码:");
  49. lblNewLabel_2.setBounds(10, 61, 64, 14);
  50. frame.getContentPane().add(lblNewLabel_2);
  51. passwordTextField = new JTextField();
  52. passwordTextField.setBounds(84, 58, 86, 20);
  53. frame.getContentPane().add(passwordTextField);
  54. passwordTextField.setColumns(10);
  55. JLabel lblNewLabel_3 = new JLabel("电话:");
  56. lblNewLabel_3.setBounds(10, 86, 64, 14);
  57. frame.getContentPane().add(lblNewLabel_3);
  58. phoneTextField = new JTextField();
  59. phoneTextField.setBounds(84, 83, 86, 20);
  60. frame.getContentPane().add(phoneTextField);
  61. phoneTextField.setColumns(10);
  62. JButton addButton = new JButton("添加教师");
  63. addButton.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent e) {
  65. addTeacher();
  66. }
  67. });
  68. addButton.setBounds(10, 116, 120, 23);
  69. frame.getContentPane().add(addButton);
  70. JButton getAllButton = new JButton("获取所有教师信息");
  71. getAllButton.addActionListener(new ActionListener() {
  72. public void actionPerformed(ActionEvent e) {
  73. getAllTeachers();
  74. }
  75. });
  76. getAllButton.setBounds(140, 116, 150, 23);
  77. frame.getContentPane().add(getAllButton);
  78. JButton deleteButton = new JButton("删除指定教师信息");
  79. deleteButton.addActionListener(new ActionListener() {
  80. public void actionPerformed(ActionEvent e) {
  81. deleteTeacher();
  82. }
  83. });
  84. deleteButton.setBounds(10, 150, 150, 23);
  85. frame.getContentPane().add(deleteButton);
  86. JButton updateButton = new JButton("修改指定教师信息");
  87. updateButton.addActionListener(new ActionListener() {
  88. public void actionPerformed(ActionEvent e) {
  89. updateTeacher();
  90. }
  91. });
  92. updateButton.setBounds(170, 150, 150, 23);
  93. frame.getContentPane().add(updateButton);
  94. JScrollPane scrollPane = new JScrollPane();
  95. scrollPane.setBounds(240, 8, 184, 242);
  96. frame.getContentPane().add(scrollPane);
  97. outputTextArea = new JTextArea();
  98. scrollPane.setViewportView(outputTextArea);
  99. }
  100. private void addTeacher() {
  101. String id = idTextField.getText();
  102. String name = nameTextField.getText();
  103. String password = passwordTextField.getText();
  104. String phone = phoneTextField.getText();
  105. Teacher teacher = new Teacher(id, name, password, phone);
  106. teacherList.add(teacher);
  107. outputTextArea.append("教师已添加:" + teacher.toString() + "\n");
  108. clearFields();
  109. }
  110. private void getAllTeachers() {
  111. if (teacherList.isEmpty()) {
  112. outputTextArea.setText("没有教师信息。\n");
  113. } else {
  114. outputTextArea.setText("所有教师信息:\n");
  115. for (Teacher teacher : teacherList) {
  116. outputTextArea.append(teacher.toString() + "\n");
  117. }
  118. }
  119. }
  120. private void deleteTeacher() {
  121. String id = idTextField.getText();
  122. boolean teacherRemoved = false;
  123. for (Teacher teacher : teacherList) {
  124. if (teacher.getId().equals(id)) {
  125. teacherList.remove(teacher);
  126. outputTextArea.append("教师已删除:" + teacher.toString() + "\n");
  127. teacherRemoved = true;
  128. break;
  129. }
  130. }
  131. if (!teacherRemoved) {
  132. outputTextArea.append("找不到指定教师。\n");
  133. }
  134. clearFields();
  135. }
  136. private void updateTeacher() {
  137. String id = idTextField.getText();
  138. String name = nameTextField.getText();
  139. String password = passwordTextField.getText();
  140. String phone = phoneTextField.getText();
  141. boolean teacherUpdated = false;
  142. for (Teacher teacher : teacherList) {
  143. if (teacher.getId().equals(id)) {
  144. teacher.setName(name);
  145. teacher.setPassword(password);
  146. teacher.setPhone(phone);
  147. outputTextArea.append("教师已更新:" + teacher.toString() + "\n");
  148. teacherUpdated = true;
  149. break;
  150. }
  151. }
  152. if (!teacherUpdated) {
  153. outputTextArea.append("找不到指定教师。\n");
  154. }
  155. clearFields();
  156. }
  157. private void clearFields() {
  158. idTextField.setText("");
  159. nameTextField.setText("");
  160. passwordTextField.setText("");
  161. phoneTextField.setText("");
  162. }
  163. }
  164. class Teacher {
  165. private String id;
  166. private String name;
  167. private String password;
  168. private String phone;
  169. public Teacher(String id, String name, String password, String phone) {
  170. this.id = id;
  171. this.name = name;
  172. this.password = password;
  173. this.phone = phone;
  174. }
  175. public String getId() {
  176. return id;
  177. }
  178. public String getName() {
  179. return name;
  180. }
  181. public void setName(String name) {
  182. this.name = name;
  183. }
  184. public String getPassword() {
  185. return password;
  186. }
  187. public void setPassword(String password) {
  188. this.password = password;
  189. }
  190. public String getPhone() {
  191. return phone;
  192. }
  193. public void setPhone(String phone) {
  194. this.phone = phone;
  195. }
  196. @Override
  197. public String toString() {
  198. return "教师ID: " + id + ", 姓名: " + name + ", 密码: " + password + ", 电话: " + phone;
  199. }
  200. }

等等页面我们都已开发。后续代码可以私聊我们领取。

5、数据库数据导入部分:

三、结果演示

答:

图一:管理系统界面的展示

 

 

 

 

 

 

 

  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/41545
推荐阅读
相关标签
  

闽ICP备14008679号