期末大作业【学生选课与成绩管理系统1】Java+MySQL数据库+可视化图形界面
bingo冰冰 2024-06-23 12:05:02 阅读 75
一、项目效果展示
本项目包含丰富子项目,涵盖增删改查和swing等基础java知识,Java+MySQL数据库+可视化图形界面,适用于初学者实战学习。
功能:①学籍信息管理 ②选课系统 ③教师课程信息管理 ④学生成绩管理。
代码经过精心调试,运行完全无误后发布。一共四个包,27个类,四个数据库表。如不想复制粘贴,也可以在我的Java专栏中下载本资源。
(一)总体功能结构图
本学生成绩管理系统的具体功能为三个模块和十五个子功能模块。下图为系统的总体功能结构图,如图 1-1 所示。
(二)界面演示
(三)项目结构目录
src包下建立四个package,每个package下创建相应的类名:
(四)开发环境
(1)操作系统:Windows 11
(2)数据库:MySQL
(3)开发环境:IDEA
(4)开发语言:Java
(五)项目结构
学生管理模块
1 添加学生界面2 修改学生信息3 删除学生信息4 学生选课界面5 查询学生信息
课程管理模块
1 添加课程信息2 修改课程信息3 删除课程信息4 课程查询
成绩管理模块
1 录入成绩2 修改成3 查询成绩
(六)图形界面
添加界面:
修改界面
删除界面
选课界面
查询界面
添加课程
修改课程
删除课程
课程查询
录入成绩
修改成绩
查询成绩
二、数据库
建库
建立一个名为yybtest的数据库,下面新建4个数据表
建表
course表
grade表
student表
studenttab表
三、源代码
manager包
CourseManager类
package cvit.com.manager;import cvit.com.manager.*;import cvit.com.pub.DBConn;import java.sql.Statement;import java.sql.ResultSet;import java.sql.Connection;import cvit.com.model.Course;import java.sql.SQLException;import java.util.Vector;//课程管理类public class CourseManager { //添加课程信息 public int addCourse(Course c) { Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "insert into course values('" + c.getCno()+ "', '" + c.getCname() + "', '" + c.getCteacher() +"', '" + c.getCtype() + "', '" + c.getCplace() +"', '" + c.getCtime() +"')"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e1) { e1.printStackTrace(); } return i; } //删除课程信息 public int deleteCourse(String cno) { Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "delete from course where cno='" + cno +"'"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return i; } //修改课程信息 public int update_Course(String cno, String cname, String cteacher, String ctype, String cplace, String ctime) { Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "update course set cname='" + cname +"', cteacher='"+ cteacher + "', ctype='" + ctype +"', cplace='"+ cplace + "', ctime='" + ctime + "' wherecno='" + cno +"'"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return i; } //根据课程编号查询课程信息 public Course findCourse(String cno) { Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select *from course where cno='" + cno +"'"; Course c = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { c = new Course(); c.setCno(rs.getString("cno")); c.setCname(rs.getString("cname")); c.setCteacher(rs.getString("cteacher")); c.setCtype(rs.getString("ctype")); c.setCplace(rs.getString("cplace")); c.setCtime(rs.getString("ctime")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return c; } //根据课程名称查询课程信息 public Course findCourse_name(String cname) { Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select *from course where cname='" + cname+"'"; Course c = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { c = new Course(); c.setCno(rs.getString("cno")); c.setCname(rs.getString("cname")); c.setCteacher(rs.getString("cteacher")); c.setCtype(rs.getString("ctype")); c.setCplace(rs.getString("cplace")); c.setCtime(rs.getString("ctime")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return c; } //根据教师名称查询课程信息 public Course findCourse_teacher(String cteacher) { Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select *from course where cteacher='" +cteacher + "'"; Course c = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { c = new Course(); c.setCno(rs.getString("cno")); c.setCname(rs.getString("cname")); c.setCteacher(rs.getString("cteacher")); c.setCtype(rs.getString("ctype")); c.setCplace(rs.getString("cplace")); c.setCtime(rs.getString("ctime")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return c; } //查询全部课程名称 public Vector selectCourse() { Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); String sql = "select cname from course"; Vector v = new Vector(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { v.add(rs.getString("cname")); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return v; } //获得全部教师名称 public Vector selectCourse2() { Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); String sql = "select cteacher from course"; Vector v = new Vector(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { v.add(rs.getString("cteacher")); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return v; } //根据课程名称查询 public Course findOneCourse(String cname) { Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); String sql ="select cno, cteacher, cplace, ctype, ctime from course where cname='" + cname + "'"; Course c = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { c = new Course(); c.setCno(rs.getString("cno")); c.setCteacher(rs.getString("cteacher")); c.setCtype(rs.getString("ctype")); c.setCplace(rs.getString("cplace")); c.setCtime(rs.getString("ctime")); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return c; } //根据教师名称查询 public Course findOneCourse2(String cteacher) { Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); String sql ="select cno, cname, cplace, ctype, ctime from course where cteacher='" +cteacher + "'"; Course c = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { c = new Course(); c.setCno(rs.getString("cno")); c.setCname(rs.getString("cname")); c.setCtype(rs.getString("ctype")); c.setCplace(rs.getString("cplace")); c.setCtime(rs.getString("ctime")); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return c; }}
GradeManager类
package cvit.com.manager;import cvit.com.model.Grade;import cvit.com.pub.DBConn;import java.sql.Statement;import java.sql.ResultSet;import java.sql.Connection;import cvit.com.model.Grade;import java.sql.SQLException;import java.util.Vector;import cvit.com.model.ZH;//成绩管理类public class GradeManager { //添加成绩 public int addGrade(Grade g) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "update grade set grade='" + g.getGrade() + "'where sno='" + g.getSno() + "'and cno='" + g.getCno() + "'"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e1) { e1.printStackTrace(); } return i; } //选课 public int chooseCourse(String sno, String cno) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "insert into grade values('" + sno + "', '" + cno +"', ' ')"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e1) { e1.printStackTrace(); } return i; } //根据学号、课程名查询一个学生成绩 public Grade findGrade(String sno, String cno) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 String sql = "select *from grade where sno='" + sno + "'and cno='" + cno + "'"; Grade g = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { g = new Grade(); g.setSno(rs.getString("sno")); g.setCno(rs.getString("cno")); g.setGrade(rs.getString("grade")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return g; } //修改成绩 public int updateGrade(String sno, String cno, String grade) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "update grade set cno='" + cno + "', grade='" + grade + "' where sno='" + sno + "'"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e1) { e1.printStackTrace(); } return i; } //查询一个学生全部学科的成绩 public Vector selectGradestudent(String sno) { Vector list = new Vector(); Connection conn = null; Statement stmt = null; ResultSet rs = null; DBConn db = new DBConn(); conn = db.getConn(); String sql = "select student.sno, sname, course.cno, cname, grade from student, course, grade where grade.sno='" + sno + "'"; ZH zh = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { zh = new ZH(); zh.setCno(rs.getString("cno")); zh.setCname(rs.getString("cname")); zh.setGrade(rs.getString("grade")); list.add(zh); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return list; }}
StudentManager类
package cvit.com.manager;import java.sql.ResultSet;import cvit.com.pub.DBConn;import java.sql.*;import cvit.com.model.*;import java.util.Vector;//学生信息管理类public class StudentManager { //添加学生信息 public int addStudent(Student s) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "insert into student values('" + s.getSno() + "', '" + s.getSname() + "', '" + s.getSsex() + "', '" + s.getSethnix() + "', '" + s.getShome() + "', '" + s.getSyear() + "', '" + s.getSmajor() + "', '" + s.getScollege() + "', '" + s.getSbirth() + "')"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e1) { e1.printStackTrace(); } return i; } //删除学生信息 public int deleteUser(String sno) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "delete from student where sno='" + sno + "'"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return i; } //修改学生信息 public int update_Student(String sno, String sname, String ssex, String sethnix, String shome, String syear, String smajor, String scollege, String sbirth) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); int i = 0; String sql = "update student set sname='" + sname +"', ssex='" + ssex + "', sethnix='" + sethnix +"', shome='" + shome + "', syear='" + syear +"', smajor='" + smajor + "', scollege='" + scollege +"', sbirth='" + sbirth + "' where sno='" + sno + "'"; try { stmt = conn.createStatement(); i = stmt.executeUpdate(sql); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return i; } //根据学号查询学生信息 public Student findStudent(String sno) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 String sql = "select *from student where sno='" + sno + "'"; Student s = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return s; } //根据姓名查询学生信息 public Student findStudentname(String sname) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 String sql = "select *from student where sname='" + sname + "'"; Student s = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return s; } //根据性别查询学生信息 public Student findStudentsex(String ssex) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 String sql = "select *from student where ssex='" + ssex + "'"; Student s = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return s; } //根据专业查询学生信息 public Student findStudenmajor(String smajor) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 String sql = "select *from student where smajor='" + smajor + "'"; Student s = null; DBConn db = new DBConn(); conn = db.getConn(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException ex3) { ex3.printStackTrace(); } return s; } //按分院查询所有学生信息 public Vector findStudentcollege(String scollege) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 Vector list = new Vector(); DBConn db = new DBConn(); conn = db.getConn(); String sql = "select *from student where scollege='" + scollege + "'"; Student s = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); list.add(i, s.getSno() + " " + s.getSname() + " " + s.getSsex() + " " + s.getSethnix() + " " + s.getShome() + " " + s.getSyear() + " " + s.getSmajor() + " " + s.getScollege() + " " + s.getSbirth()); i++; } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return list; } //获得全部分院名称 public Vector selectStudentcollege() { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); String sql = "select scollege from student"; Vector v = new Vector(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { v.add(rs.getString("scollege")); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return v; } //获得全部专业 public Vector selectStudentmajor() { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); String sql = "select smajor from student"; Vector v = new Vector(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { v.add(rs.getString("smajor")); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return v; } //按专业查询 public Vector findStudentmajor(String smajor) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 Vector list = new Vector(); DBConn db = new DBConn(); conn = db.getConn(); String sql = "select *from student where smajor='" + smajor + "'"; Student s = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); list.add(i, s.getSno() + " " + s.getSname() + " " + s.getSsex() + " " + s.getSethnix() + " " + s.getShome() + " " + s.getSyear() + " " + s.getSmajor() + " " + s.getScollege() + " " + s.getSbirth()); i++; } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return list; } //按性别查询 public Vector findStudentsex1(String ssex) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 Vector list = new Vector(); DBConn db = new DBConn(); conn = db.getConn(); String sql = "select *from student where ssex='" + ssex + "'"; Student s = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); list.add(i, s.getSno() + " " + s.getSname() + " " + s.getSsex() + " " + s.getSethnix() + " " + s.getShome() + " " + s.getSyear() + " " + s.getSmajor() + " " + s.getScollege() + " " + s.getSbirth()); i++; } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return list; } //按姓名查询 public Vector find; Vector Student_name(String sname) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 Vector list = new Vector(); DBConn db = new DBConn(); conn = db.getConn(); String sql = "select *from student where sname='" + sname + "'"; Student s = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { s = new Student(); s.setSno(rs.getString("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSethnix(rs.getString("sethnix")); s.setShome(rs.getString("shome")); s.setSyear(rs.getString("syear")); s.setSmajor(rs.getString("smajor")); s.setScollege(rs.getString("scollege")); s.setSbirth(rs.getString("sbirth")); list.add(i, s.getSno() + " " + s.getSname() + " " + s.getSsex() + " " + s.getSethnix() + " " + s.getShome() + " " + s.getSyear() + " " + s.getSmajor() + " " + s.getScollege() + " " + s.getSbirth()); i++; } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return list; } //查询全部 public Vector selectStudent() { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); String sql = "select sno from student"; Vector v = new Vector(); try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { v.add(rs.getString("sno")); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return v; } //通过学号查询全部 public String findOneStudent(String sno) { Connection conn=null; //连接数据库对象 Statement stmt=null; //执行sql语句的对象 ResultSet rs=null; //执行sql语句的返回结果 DBConn db = new DBConn(); conn = db.getConn(); String sql = "select sname from student where sno='" + sno + "'"; String s = ""; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { s = rs.getString("sname"); } rs.close(); stmt.close(); conn.close(); } catch (Exception em) { em.printStackTrace(); System.out.println("sql有错"); } return s; }}
model包
Course类
package cvit.com.model; //包//课程类public class Course { public String cno; //课程编号 public String cname; //课程名称 public String cteacher;//任课教师 public String ctype; //科目类型 public String cplace; //上课地点 public String ctime; //上课时间 //以下是set和get方法 public void setCname(String cname) { this.cname = cname; } public void setCno(String cno) { this.cno = cno; } public void setCplace(String cplace) { this.cplace = cplace; } public void setCteacher(String cteacher) { this.cteacher = cteacher; } public void setCtime(String ctime) { this.ctime = ctime; } public void setCtype(String ctype) { this.ctype = ctype; } public String getCtype() { return ctype; } public String getCtime() { return ctime; } public String getCteacher() { return cteacher; } public String getCplace() { return cplace; } public String getCno() { return cno; } public String getCname() { return cname; }}
Grade类
package cvit.com.model;//成绩类public class Grade { public String sno; //学生学号 public String cno; //课程编号 public String grade; //成绩 //以下是set和get方法 public String getCno() { return cno; } public String getGrade() { return grade; } public String getSno() { return sno; } public void setCno(String cno) { this.cno = cno; } public void setGrade(String grade) { this.grade = grade; } public void setSno(String sno) { this.sno = sno; }}
Student类
package cvit.com.model;//包//学生类public class Student { public String sno; //学号 public String sname; //学生姓名 public String ssex; //学生性别 public String sethnix; //民族 public String shome; //籍贯 public String syear; //入学时间 public String smajor; //专业 public String scollege; //所在分院 public String sbirth; //出生日期 //以下是set和get方法 public String getSbirth() { return sbirth; } public String getScollege() { return scollege; } public String getSethnix() { return sethnix; } public String getShome() { return shome; } public String getSmajor() { return smajor; } public String getSname() { return sname; } public String getSno() { return sno; } public String getSsex() { return ssex; } public String getSyear() { return syear; } public void setSbirth(String sbirth) { this.sbirth = sbirth; } public void setScollege(String scollege) { this.scollege = scollege; } public void setSethnix(String sethnix) { this.sethnix = sethnix; } public void setShome(String shome) { this.shome = shome; } public void setSmajor(String smajor) { this.smajor = smajor; } public void setSname(String sname) { this.sname = sname; } public void setSno(String sno) { this.sno = sno; } public void setSsex(String ssex) { this.ssex = ssex; } public void setSyear(String syear) { this.syear = syear; }}
ZH类
package cvit.com.model;//综合查询类public class ZH { public String sno; //学生学号 public String sname; //学生姓名 public String cno; //课程编号 public String cname; //课程名称 public String grade; //成绩 //以下是set和get方法 public String getCname() { return cname; } public String getCno() { return cno; } public String getGrade() { return grade; } public String getSname() { return sname; } public String getSno() { return sno; } public void setCname(String cname) { this.cname = cname; } public void setCno(String cno) { this.cno = cno; } public void setGrade(String grade) { this.grade = grade; } public void setSname(String sname) { this.sname = sname; } public void setSno(String sno) { this.sno = sno; }}
pub包
DBConn类
package cvit.com.pub;import java.sql.*;public class DBConn{ //连接数据库 private String driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/yybtest"; private Connection conn=null; //连接数据库对象 private String user="root"; //用户名 private String pwd="x5"; //口令 public Connection getConn() { try { Class.forName(driver); } catch (ClassNotFoundException ex) { System.out.println("加载驱动程序有错误,驱动程序类不存在"); } try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/yybtest", "root", "x5"); } catch (SQLException ex1) { System.out.print("取得连接的时候有错误,请核对用户名和密码"); } return conn; } //测试是否连接数据库 public static void main(String args[]) { DBConn db = new DBConn(); Connection conn = db.getConn(); System.out.println("Ok"); }}
view包
类1:AllgradePanel
package cvit.com.view;public class AllgradePanel { }
类2:StuMainFrame
package cvit.com.view;import java.awt.*;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.*;import java.awt.event.*;import java.awt.Dimension;import java.awt.Font;import cvit.com.view.*;//系统主界面public class StuMainFrame extends JFrame implements ActionListener { //定义菜单条 JMenuBar mb = new JMenuBar(); //定义菜单 JMenu systemment = new JMenu("系统管理"); JMenu studentment = new JMenu("学生管理"); JMenu coursement = new JMenu("课程管理"); JMenu gradement = new JMenu("成绩管理"); JMenu informationment = new JMenu("信息查询"); JMenu student_select = new JMenu("学生查询"); JMenu course_select = new JMenu("课程查询"); JMenu grade_select = new JMenu("成绩查询"); JMenuItem exit = new JMenuItem("退出"); JMenuItem student_add = new JMenuItem("增加"); JMenuItem student_update = new JMenuItem("修改"); JMenuItem student_delete = new JMenuItem("删除"); JMenuItem student_choose = new JMenuItem("学生选课"); JMenuItem course_add = new JMenuItem("课程增加"); JMenuItem course_update = new JMenuItem("课程修改"); JMenuItem course_delete = new JMenuItem("课程删除"); JMenuItem grade_add = new JMenuItem("成绩增加"); JMenuItem grade_update = new JMenuItem("成绩修改"); JMenuItem select_no = new JMenuItem("按学号查询"); JMenuItem select_name = new JMenuItem("按学生姓名查询"); JMenuItem select_sex = new JMenuItem("按学生性别查询"); JMenuItem select_major = new JMenuItem("按专业查询"); JMenuItem select_college = new JMenuItem("按学院查询"); JMenuItem select_cname = new JMenuItem("按课程名称查询"); JMenuItem select_cteacher = new JMenuItem("按授课教师查询"); JMenuItem select_allgrade = new JMenuItem("查询所有科目成绩"); JPanel p = null; JTextField jtf1 = new JTextField(); BorderLayout borderLayout1 = new BorderLayout(); public StuMainFrame() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { getContentPane().setLayout(borderLayout1); this.getContentPane().setBackground(new Color(168, 233, 216)); this.setJMenuBar(mb); jtf1.setBackground(SystemColor.control); jtf1.setEnabled(false); jtf1.setFont(new java.awt.Font("宋体", Font.BOLD, 12)); jtf1.setForeground(Color.cyan); jtf1.setSelectionColor(Color.black); //以下是向菜单条添加菜单项 mb.add(systemment); mb.add(studentment); mb.add(coursement); mb.add(gradement); mb.add(informationment); //以下是向菜单中添加菜单项 systemment.add(exit); //系统管理退出 studentment.add(student_add); //学生管理 studentment.add(student_update); studentment.add(student_delete); studentment.add(student_choose); coursement.add(course_add); //课程管理 coursement.add(course_update); coursement.add(course_delete); gradement.add(grade_add); //成绩管理 gradement.add(grade_update); informationment.add(student_select); //信息查询 informationment.add(course_select); informationment.add(grade_select); //以下是向二级菜单中添加菜单项 student_select.add(select_no); student_select.add(select_name); student_select.add(select_sex); student_select.add(select_major); student_select.add(select_college); course_select.add(select_cname); course_select.add(select_cteacher); grade_select.add(select_allgrade); this.getContentPane().add(jtf1, java.awt.BorderLayout.SOUTH); exit.addActionListener(this); //注册菜单监听 student_add.addActionListener(this); //添加学生信息 student_update.addActionListener(this); //修改学生信息 student_delete.addActionListener(this); //删除学生信息 student_choose.addActionListener(this); //学生选课 course_add.addActionListener(this); //添加课程信息 course_update.addActionListener(this); //修改课程信息 course_delete.addActionListener(this); //删除学生信息 grade_add.addActionListener(this); //添加成绩 grade_update.addActionListener(this); //修改成绩 select_no.addActionListener(this); //按学号查询 select_major.addActionListener(this); //按专业查询 select_cname.addActionListener(this); //按课程名称查询 select_cteacher.addActionListener(this); //按任课教师查询 select_college.addActionListener(this); //按学院查询 select_sex.addActionListener(this); //按性别查询 select_name.addActionListener(this); //按姓名查询 select_allgrade.addActionListener(this); //所有成绩 //版权信息 jtf1.setText("学生信息管理系统版由CVIT开发, 盗版必究! "); jtf1.setHorizontalAlignment(SwingConstants.CENTER); this.setTitle("学生信息管理系统"); //窗体名称 this.setSize(600, 600); //大小 this.setVisible(true); //可见性 //以下为设置窗体居中 //获得屏幕的宽和高 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获得当前窗体的宽和高 Dimension frameSize = this.getSize(); //设置窗体居中 if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); this.setVisible(true); } public static void main(String[] args) { StuMainFrame sm = new StuMainFrame(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == exit) { //退出 this.setVisible(false); } //添加学生信息 if (e.getSource() == student_add) { if (p != null) { this.remove(p); } //定义一个添加学生信息页面的对象 p = new Add_studentPanel(); //将panel放在页面的中间 this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //修改学生信息 if (e.getSource() == student_update) { if (p != null) { this.remove(p); } p = new Update_studentPanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //删除学生信息 if (e.getSource() == student_delete) { if (p != null) { this.remove(p); } p = new Delete_studentPanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //学生选课 if (e.getSource() == student_choose) { if (p != null) { this.remove(p); } p = new Student_choosePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //添加课程信息 if (e.getSource() == course_add) { if (p != null) { this.remove(p); } p = new Add_coursePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //修改课程信息 if (e.getSource() == course_update) { if (p != null) { this.remove(p); } p = new Update_coursePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //删除课程信息 if (e.getSource() == course_delete) { if (p != null) { this.remove(p); } p = new Delete_coursePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //成绩添加 if (e.getSource() == grade_add) { if (p != null) { this.remove(p); } p = new Add_gradePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //成绩修改 if (e.getSource() == grade_update) { if (p != null) { this.remove(p); } p = new Update_gradePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //按学号查询 if (e.getSource() == select_no) { if (p != null) { this.remove(p); } p = new Select_studentnoPanel1(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //按分院查询 if (e.getSource() == select_college) { if (p != null) { this.remove(p); } p = new Select_studentcollegePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //按专业查询 if (e.getSource() == select_major) { if (p != null) { this.remove(p); } p = new Select_studentmajorPanel1(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //按课程名称查询 if (e.getSource() == select_cname) { if (p != null) { this.remove(p); } p = new Select_coursecnamePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //按任课教师查询 if (e.getSource() == select_cteacher) { if (p != null) { this.remove(p); } p = new Select_coursecteacherPanel1(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //按学生姓名查询 if (e.getSource() == select_name) { if (p != null) { this.remove(p); } p = new Select_studentnamePanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //按性别查询 if (e.getSource() == select_sex) { if (p != null) { this.remove(p); } p = new Select_studentsexPanel(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } //查询所有科目成绩 if (e.getSource() == select_allgrade) { if (p != null) { this.remove(p); } p = new Select_gradestudentPanel1(); this.getContentPane().add(p, java.awt.BorderLayout.CENTER); this.setVisible(true); } }}
类3:Add_coursePanel
package cvit.com.view;import cvit.com.manager.CourseManager;import cvit.com.model.Course;import javax.swing.JPanel;import java.awt.Color;import javax.swing.JLabel;import java.awt.Rectangle;import javax.swing.JButton;import javax.swing.JTextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JOptionPane;import cvit.com.manager.CourseManager;//添加课程public class Add_coursePanel extends JPanel { public Add_coursePanel() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); this.setBackground(new Color(168, 233, 216)); btn_add.setBounds(new Rectangle(37, 182, 81, 23)); btn_add.setText("添加"); btn_add.addActionListener(new Add_coursePanel_btn_add_actionAdapter(this)); btn_null.setBounds(new Rectangle(153, 182, 81, 23)); btn_null.setText("清空"); btn_null.addActionListener(new Add_coursePanel_btn_null_actionAdapter(this)); btn_exit.setBounds(new Rectangle(267, 182, 81, 23)); btn_exit.setText("退出"); btn_exit.addActionListener(new Add_coursePanel_btn_exit_actionAdapter(this)); jLabel1.setText("课程类别"); jLabel1.setBounds(new Rectangle(198, 87, 62, 15)); jLabel2.setToolTipText(""); jLabel2.setText("授课教师"); jLabel2.setBounds(new Rectangle(19, 89, 53, 15)); tctime.setBounds(new Rectangle(265, 123, 77, 20)); tcno.setBounds(new Rectangle(93, 45, 77, 20)); tteacher.setBounds(new Rectangle(91, 85, 77, 20)); jLabel3.setText("上课时间"); jLabel3.setBounds(new Rectangle(199, 124, 57, 15)); jLabel4.setText("课程编码"); jLabel4.setBounds(new Rectangle(20, 47, 65, 15)); jLabel5.setText("课程名称"); jLabel5.setBounds(new Rectangle(201, 47, 60, 15)); tcplace.setBounds(new Rectangle(93, 123, 77, 20)); tctype.setBounds(new Rectangle(265, 85, 77, 20)); tcname.setBounds(new Rectangle(264, 44, 77, 20)); jLabel6.setText("上课地点"); jLabel6.setBounds(new Rectangle(19, 124, 67, 15)); this.add(tteacher); this.add(jLabel1); this.add(tctime); this.add(tcno); this.add(jLabel3); this.add(jLabel5); this.add(tcplace); this.add(tctype); this.add(tcname); this.add(jLabel6); this.add(jLabel4); this.add(btn_null); this.add(btn_add); this.add(btn_exit); this.add(jLabel2); this.setVisible(true); //窗体可见性 this.setSize(600, 600); //设置窗体大小 } JButton btn_add = new JButton(); JButton btn_null = new JButton(); JButton btn_exit = new JButton(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JTextField tctime = new JTextField(); JTextField tcno = new JTextField(); JTextField tteacher = new JTextField(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JTextField tcplace = new JTextField(); JTextField tctype = new JTextField(); JTextField tcname = new JTextField(); JLabel jLabel6 = new JLabel(); public void btn_add_actionPerformed(ActionEvent e) { String cno=""; //定义空的学生信息属性 String cname = ""; String cteacher = ""; String ctype = ""; String cplace = ""; String ctime = ""; if (!tcno.getText().equals("")) { cno=tcno.getText(); //获得课程名称文本框中的内容 } else { JOptionPane.showMessageDialog(this, "课程编码不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tcname.getText().equals("")) { cname = tcname.getText(); } else { JOptionPane.showMessageDialog(this, "课程名称不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tteacher.getText().equals("")) { cteacher = tteacher.getText(); } else { JOptionPane.showMessageDialog(this, "授课教师不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tctype.getText().equals("")) { ctype = tctype.getText(); } else { JOptionPane.showMessageDialog(this, "课程类别不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tcplace.getText().equals("")) { cplace = tcplace.getText(); } else { JOptionPane.showMessageDialog(this, "上课地点不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tctime.getText().equals("")) { ctime = tctime.getText(); } else { JOptionPane.showMessageDialog(this, "上课时间不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } Course c=new Course(); //定义课程信息并对信息封装 c.setCno(cno); c.setCname(cname); c.setCteacher(cteacher); c.setCtype(ctype); c.setCplace(cplace); c.setCtime(ctime); CourseManager cm = new CourseManager(); int i=cm.addCourse(c); //调用添加课程信息方法 if (i > 0) { JOptionPane.showMessageDialog(this, "添加成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE); tcno.setText(""); //清空文本框内容 tcname.setText(""); tctype.setText(""); tcplace.setText(""); tteacher.setText(""); tctime.setText(""); } else { JOptionPane.showMessageDialog(this, "添加失败!", "提示信息", JOptionPane.ERROR_MESSAGE); } } public void btn_null_actionPerformed(ActionEvent e) { tcno.setText(""); //清空文本框内容 tcname.setText(""); tteacher.setText(""); tctype.setText(""); tcplace.setText(""); tctime.setText(""); } //退出按钮关闭当前页面 public void btn_exit_actionPerformed(ActionEvent e) { this.setVisible(false); }}class Add_coursePanel_btn_exit_actionAdapter implements ActionListener { private Add_coursePanel adaptee; Add_coursePanel_btn_exit_actionAdapter(Add_coursePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_exit_actionPerformed(e); }}class Add_coursePanel_btn_null_actionAdapter implements ActionListener { private Add_coursePanel adaptee; Add_coursePanel_btn_null_actionAdapter(Add_coursePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_null_actionPerformed(e); }}class Add_coursePanel_btn_add_actionAdapter implements ActionListener { private Add_coursePanel adaptee; Add_coursePanel_btn_add_actionAdapter(Add_coursePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_add_actionPerformed(e); }}
类4:Add_gradePanel
package cvit.com.view;import javax.swing.JPanel;import java.awt.Color;import javax.swing.JLabel;import java.awt.Rectangle;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JTextField;import cvit.com.manager.CourseManager;import cvit.com.manager.StudentManager;import java.util.Vector;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import cvit.com.model.Course;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import cvit.com.model.Grade;import javax.swing.JOptionPane;import cvit.com.manager.GradeManager;//添加分数public class Add_gradePanel extends JPanel { public Add_gradePanel() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); this.setBackground(new Color(168, 233, 216)); jLabel1.setText("学号"); jLabel1.setBounds(new Rectangle(30, 22, 42, 15)); jLabel2.setText("姓名"); jLabel2.setBounds(new Rectangle(200, 20, 42, 15)); jLabel3.setText("课程名称"); jLabel3.setBounds(new Rectangle(23, 76, 55, 15)); jLabel4.setText("课程编码"); jLabel4.setBounds(new Rectangle(201, 74, 57, 15)); jLabel5.setText("授课教师"); jLabel5.setBounds(new Rectangle(15, 129, 61, 15)); jLabel6.setText("成绩"); jLabel6.setBounds(new Rectangle(202, 129, 42, 15)); btn_add.setBounds(new Rectangle(76, 176, 81, 23)); btn_add.setText("增加"); btn_add.addActionListener(new addcjPanel1_btn_add_actionAdapter(this)); btn_exit.setBounds(new Rectangle(233, 175, 81, 23)); btn_exit.setText("退出"); btn_exit.addActionListener(new addcjPanel1_btn_exit_actionAdapter(this)); jcb_sno.setBounds(new Rectangle(91, 17, 89, 23)); jcb_sno.addItemListener(new addcjPanel1_jcb_sno_itemAdapter(this)); jcb_cname.setBounds(new Rectangle(92, 72, 89, 23)); jcb_cname.addItemListener(new addcjPanel1_jcb_cname_itemAdapter(this)); tsname.setEnabled(false); tsname.setBounds(new Rectangle(261, 19, 77, 20)); tcno.setEnabled(false); tcno.setBounds(new Rectangle(260, 74, 77, 20)); tcteacher.setEnabled(false); tcteacher.setBounds(new Rectangle(91, 127, 77, 20)); tgrade.setBounds(new Rectangle(262, 127, 77, 20)); this.add(jLabel2); this.add(jcb_sno); this.add(tsname); this.add(jLabel1); this.add(tcno); this.add(jLabel4); this.add(jcb_cname); this.add(jLabel3); this.add(tcteacher); this.add(jLabel5); this.add(jLabel6); this.add(tgrade); this.add(btn_exit); this.add(btn_add); this.setVisible(true); this.setSize(600, 600); Vector v=sm.selectStudent(); //调用查询所有学号方法 for (int i = 0; i < v.size(); i++) { jcb_sno.addItem(v.get(i).toString()); //将数据库所有学号添加到下拉列表中 } Vector v1=cm.selectCourse(); //调用查询所有科目名称方法 for (int i = 0; i < v1.size(); i++) { jcb_cname.addItem(v1.get(i).toString()); //将数据库中所有的课程名称添加到列表中 } } JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); JButton btn_add = new JButton(); JButton btn_exit = new JButton(); JComboBox jcb_sno = new JComboBox(); JComboBox jcb_cname = new JComboBox(); JTextField tsname = new JTextField(); JTextField tcno = new JTextField(); JTextField tcteacher = new JTextField(); JTextField tgrade = new JTextField(); StudentManager sm=new StudentManager(); //初始化中定义学生管理对象 CourseManager cm=new CourseManager(); //初始化中定义课程管理对象 public static void main(String[] args) { Add_gradePanel sm = new Add_gradePanel(); } public void jcb_sno_itemStateChanged(ItemEvent e) { //调用按学号查询学生信息方法 tsname.setText(sm.findOneStudent(jcb_sno.getSelectedItem().toString())); } public void jcb_cname_itemStateChanged(ItemEvent e) { //调用按课程名称查询课程信息方法 Course c = cm.findOneCourse(jcb_cname.getSelectedItem().toString()); tcno.setText(c.getCno()); tcteacher.setText(c.getCteacher()); } public void btn_exit_actionPerformed(ActionEvent e) { this.setVisible(false); } public void btn_add_actionPerformed(ActionEvent e) { String sno = (String) jcb_sno.getSelectedItem(); String cno = tcno.getText(); String grade = tgrade.getText(); Grade g = new Grade(); g.setSno(sno); g.setCno(cno); g.setGrade(grade); GradeManager gm = new GradeManager(); int i=gm.addGrade(g); //调用添加成绩方法 if (i > 0) { JOptionPane.showMessageDialog(this, "添加成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this, "学生与实际选课科目不符,请核对!", "提示信息", JOptionPane.ERROR_MESSAGE); } }}class addcjPanel1_btn_add_actionAdapter implements ActionListener { private Add_gradePanel adaptee; addcjPanel1_btn_add_actionAdapter(Add_gradePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_add_actionPerformed(e); }}class addcjPanel1_btn_exit_actionAdapter implements ActionListener { private Add_gradePanel adaptee; addcjPanel1_btn_exit_actionAdapter(Add_gradePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_exit_actionPerformed(e); }}class addcjPanel1_jcb_cname_itemAdapter implements ItemListener { private Add_gradePanel adaptee; addcjPanel1_jcb_cname_itemAdapter(Add_gradePanel adaptee) { this.adaptee = adaptee; } public void itemStateChanged(ItemEvent e) { adaptee.jcb_cname_itemStateChanged(e); }}class addcjPanel1_jcb_sno_itemAdapter implements ItemListener { private Add_gradePanel adaptee; addcjPanel1_jcb_sno_itemAdapter(Add_gradePanel adaptee) { this.adaptee = adaptee; } public void itemStateChanged(ItemEvent e) { adaptee.jcb_sno_itemStateChanged(e); }}
类5:Add_studentPanel
package cvit.com.view;import cvit.com.manager.StudentManager;import cvit.com.model.Student;import javax.swing.JPanel;import javax.swing.JButton;import java.awt.Rectangle;import javax.swing.JLabel;import javax.swing.JTextField;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JOptionPane;import javax.swing.JComboBox;//添加学生信息public class Add_studentPanel extends JPanel { public Add_studentPanel() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); this.setSize(600, 600); this.setBackground(new Color(168, 233, 216)); this.setVisible(true); jLabel1.setText("学号"); jLabel1.setBounds(new Rectangle(15, 36, 42, 15)); jLabel2.setText("性别"); jLabel2.setBounds(new Rectangle(14, 74, 42, 15)); tsno.setBounds(new Rectangle(79, 33, 77, 20)); jLabel3.setText("姓名"); jLabel3.setBounds(new Rectangle(187, 35, 42, 15)); jLabel4.setText("民族"); jLabel4.setBounds(new Rectangle(184, 75, 42, 15)); tsname.setBounds(new Rectangle(250, 32, 77, 20)); jLabel5.setText("出生日期"); jLabel5.setBounds(new Rectangle(5, 112, 67, 15)); jLabel6.setText("入学时间"); jLabel6.setBounds(new Rectangle(185, 112, 57, 15)); tsbirth.setBounds(new Rectangle(79, 111, 77, 20)); tstime.setBounds(new Rectangle(251, 111, 77, 20)); jLabel7.setText("学院"); jLabel7.setBounds(new Rectangle(15, 154, 42, 15)); tscollege.setBounds(new Rectangle(79, 153, 77, 20)); jLabel8.setText("专业"); jLabel8.setBounds(new Rectangle(188, 154, 42, 15)); tsmajor.setBounds(new Rectangle(251, 156, 77, 20)); jLabel9.setText("籍贯"); jLabel9.setBounds(new Rectangle(16, 197, 42, 15)); tsplace.setBounds(new Rectangle(77, 198, 251, 20)); btn1.setBounds(new Rectangle(35, 247, 81, 23)); btn1.setText("添加"); btn1.addActionListener(new Add_studentPanel_btn1_actionAdapter(this)); btn2.setBounds(new Rectangle(151, 247, 81, 23)); btn2.setText("清空"); btn2.addActionListener(new Add_studentPanel_btn2_actionAdapter(this)); btn3.setBounds(new Rectangle(265, 247, 81, 23)); btn3.setText("退出"); btn3.addActionListener(new Add_studentPanel_btn3_actionAdapter(this)); jcb_sex.setBounds(new Rectangle(77, 66, 43, 23)); jcb_ethnix.setBounds(new Rectangle(253, 66, 58, 23)); this.add(tsno); this.add(jLabel3); this.add(tsname); this.add(btn1); this.add(btn2); this.add(jLabel5); this.add(jLabel1); this.add(tscollege); this.add(tsbirth); this.add(tstime); this.add(jLabel6); this.add(jLabel4); this.add(jLabel2); this.add(jLabel7); this.add(jLabel8); this.add(tsmajor); this.add(jLabel9); this.add(tsplace); this.add(btn3); this.add(jcb_ethnix); this.add(jcb_sex); jcb_sex.addItem("男"); //向性别的列表中添加内容 jcb_sex.addItem("女"); jcb_ethnix.addItem("汉族"); //向民族的列表中添加内容 jcb_ethnix.addItem("蒙族"); jcb_ethnix.addItem("回族"); jcb_ethnix.addItem("满族"); } JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JTextField tsno = new JTextField(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JTextField tsname = new JTextField(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); JTextField tsbirth = new JTextField(); JTextField tstime = new JTextField(); JLabel jLabel7 = new JLabel(); JTextField tscollege = new JTextField(); JLabel jLabel8 = new JLabel(); JTextField tsmajor = new JTextField(); JLabel jLabel9 = new JLabel(); JTextField tsplace = new JTextField(); JButton btn1 = new JButton(); JButton btn2 = new JButton(); JButton btn3 = new JButton(); JComboBox jcb_sex = new JComboBox(); JComboBox jcb_ethnix = new JComboBox(); public void btn1_actionPerformed(ActionEvent actionEvent) { //定义空的学生信息属性 String sno = ""; String sname = ""; String ssex = ""; String sbirth = ""; String syear = ""; String shome = ""; String smajor = ""; String scollege = ""; if (!tsno.getText().equals("")) { sno = tsno.getText(); } else { //弹出对话框信息 JOptionPane.showMessageDialog(this, "学号不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tsname.getText().equals("")) { sname = tsname.getText(); } else { JOptionPane.showMessageDialog(this, "姓名不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tsbirth.getText().equals("")) { sbirth = tsbirth.getText(); } else { JOptionPane.showMessageDialog(this, "出生日期不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tstime.getText().equals("")) { syear = tstime.getText(); } else { JOptionPane.showMessageDialog(this, "入学时间不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tscollege.getText().equals("")) { scollege = tscollege.getText(); } else { JOptionPane.showMessageDialog(this, "学院不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tsmajor.getText().equals("")) { smajor = tsmajor.getText(); } else { JOptionPane.showMessageDialog(this, "专业不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } if (!tsplace.getText().equals("")) { shome = tsplace.getText(); } else { JOptionPane.showMessageDialog(this, "籍贯不能为空!", "提示信息", JOptionPane.ERROR_MESSAGE); return; } ssex=jcb_sex.getSelectedItem().toString(); //获得列表中选定的性别信息 String sethnix=jcb_ethnix.getSelectedItem().toString(); //获得民族列表总选到的信息 Student s=new Student(); //定义学生对象s进行封装学生信息 s.setSno(sno); s.setSname(sname); s.setSsex(ssex); s.setSethnix(sethnix); s.setShome(shome); s.setSyear(syear); s.setSmajor(smajor); s.setScollege(scollege); s.setSbirth(sbirth); StudentManager sm=new StudentManager(); //定义学生管理对象sm int i=sm.addStudent(s); //调用添加学生信息方法 if (i > 0) { JOptionPane.showMessageDialog(this, "添加成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE); tsno.setText(""); tsname.setText(""); tsplace.setText(""); tstime.setText(""); tsmajor.setText(""); tscollege.setText(""); tsbirth.setText(""); } else { JOptionPane.showMessageDialog(this, "添加失败!", "提示信息", JOptionPane.ERROR_MESSAGE); } } //退出按钮关闭当前页面 public void btn3_actionPerformed(ActionEvent e) { this.setVisible(false); } //清空按钮清空各文本框中的内容 public void btn2_actionPerformed(ActionEvent e) { tsno.setText(""); tsname.setText(""); tsplace.setText(""); tstime.setText(""); tsmajor.setText(""); tscollege.setText(""); tsbirth.setText(""); }}//监听class Add_studentPanel_btn2_actionAdapter implements ActionListener { private Add_studentPanel adaptee; //编码时双击添加按钮自动生成代码 Add_studentPanel_btn2_actionAdapter(Add_studentPanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn2_actionPerformed(e); }}class Add_studentPanel_btn3_actionAdapter implements ActionListener { private Add_studentPanel adaptee; Add_studentPanel_btn3_actionAdapter(Add_studentPanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn3_actionPerformed(e); }}class Add_studentPanel_btn1_actionAdapter implements ActionListener { private Add_studentPanel adaptee; Add_studentPanel_btn1_actionAdapter(Add_studentPanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent actionEvent) { adaptee.btn1_actionPerformed(actionEvent); }}
类6:Delete_CoursePanel
package cvit.com.view;import cvit.com.manager.CourseManager;import javax.swing.JPanel;import java.awt.Color;import javax.swing.JTextField;import java.awt.Rectangle;import javax.swing.JLabel;import javax.swing.JButton;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JOptionPane;//删除科目public class Delete_coursePanel extends JPanel { public Delete_coursePanel() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); this.setBackground(new Color(168, 233, 216)); tcno.setBounds(new Rectangle(78, 76, 130, 20)); jLabel1.setText("课程编号"); jLabel1.setBounds(new Rectangle(8, 81, 66, 15)); btn_delete.setBounds(new Rectangle(228, 72, 71, 23)); btn_delete.setText("删除"); btn_delete.addActionListener(new Delete_coursePanel_btn_delete_actionAdapter(this)); btn_exit.setBounds(new Rectangle(320, 72, 69, 23)); btn_exit.setText("退出"); btn_exit.addActionListener(new Delete_coursePanel_btn_exit_actionAdapter(this)); this.add(tcno); this.add(jLabel1); this.add(btn_delete); this.add(btn_exit); this.setVisible(true); //可见性 this.setSize(600, 600); //窗体大小 } JTextField tcno = new JTextField(); JLabel jLabel1 = new JLabel(); JButton btn_delete = new JButton(); JButton btn_exit = new JButton(); public void btn_delete_actionPerformed(ActionEvent e) { String cno=tcno.getText(); //获得课程编码 CourseManager cm = new CourseManager(); int ok = JOptionPane.showConfirmDialog(this, "是否删除" + tcno.getText() + "课程?", "提示信息", JOptionPane.OK_CANCEL_OPTION); if (ok == JOptionPane.OK_OPTION) { int i=cm.deleteCourse(cno); //调用删除课程信息方法 if (i > 0) { JOptionPane.showMessageDialog(this, "删除成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this, "你要删除的课程不存在!", "提示信息", JOptionPane.ERROR_MESSAGE); tcno.setText(""); } tcno.setText(""); } } public void btn_exit_actionPerformed(ActionEvent e) { this.setVisible(false); } class Delete_coursePanel_btn_delete_actionAdapter implements ActionListener { private Delete_coursePanel adaptee; Delete_coursePanel_btn_delete_actionAdapter(Delete_coursePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_delete_actionPerformed(e); } }}class Delete_coursePanel_btn_exit_actionAdapter implements ActionListener { private Delete_coursePanel adaptee; Delete_coursePanel_btn_exit_actionAdapter(Delete_coursePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_exit_actionPerformed(e); }}
类7:Delete_studentPanel
package cvit.com.view;import cvit.com.manager.StudentManager;import javax.swing.JPanel;import java.awt.Color;import javax.swing.JTextField;import java.awt.Rectangle;import javax.swing.JLabel;import javax.swing.JButton;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JOptionPane;//删除学生信息public class Delete_studentPanel extends JPanel { public Delete_studentPanel() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); this.setBackground(new Color(168, 233, 216)); tsno.setBounds(new Rectangle(78, 76, 130, 20)); jLabel1.setText("学号"); jLabel1.setBounds(new Rectangle(32, 81, 42, 15)); btn_delete.setBounds(new Rectangle(235, 76, 81, 23)); btn_delete.setText("删除"); btn_delete.addActionListener(new Delete_studentPanel_btn_delete_actionAdapter(this)); btn_exit.setBounds(new Rectangle(233, 111, 81, 23)); btn_exit.setText("退出"); btn_exit.addActionListener(new Delete_studentPanel_btn_exit_actionAdapter(this)); this.add(tsno); this.add(jLabel1); this.add(btn_delete); this.add(btn_exit); this.setVisible(true); //可见性 this.setSize(600, 600); //窗体大小 } JTextField tsno = new JTextField(); JLabel jLabel1 = new JLabel(); JButton btn_delete = new JButton(); JButton btn_exit = new JButton(); public void btn_delete_actionPerformed(ActionEvent e) { String sno=tsno.getText(); //获得学号文本框中的学号 StudentManager sm=new StudentManager(); //定义学生管理对象sm int ok = JOptionPane.showConfirmDialog(this, "是否删除" + tsno.getText() + "同学? ", "提示信息", JOptionPane.OK_CANCEL_OPTION); if (ok == JOptionPane.OK_OPTION) { int i=sm.deleteUser(sno); //调用删除学生方法 if (i > 0) { JOptionPane.showMessageDialog(this, "删除成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this, "你要删除的同学不存在!", "提示信息", JOptionPane.ERROR_MESSAGE); } tsno.setText(""); } } //退出按钮关闭页面 public void btn_exit_actionPerformed(ActionEvent e) { this.setVisible(false); }}class Delete_studentPanel_btn_exit_actionAdapter implements ActionListener { private Delete_studentPanel adaptee; Delete_studentPanel_btn_exit_actionAdapter(Delete_studentPanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_exit_actionPerformed(e); }}class Delete_studentPanel_btn_delete_actionAdapter implements ActionListener { private Delete_studentPanel adaptee; Delete_studentPanel_btn_delete_actionAdapter(Delete_studentPanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_delete_actionPerformed(e); }}
类8:Select_coursecnamePanel
package cvit.com.view;import javax.swing.JPanel;import java.awt.Color;import javax.swing.JLabel;import java.awt.Rectangle;import javax.swing.JTextField;import javax.swing.JButton;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import cvit.com.manager.CourseManager;import cvit.com.model.Course;import javax.swing.JComboBox;import java.util.Vector;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;//根据课程名称查询public class Select_coursecnamePanel extends JPanel { public Select_coursecnamePanel() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); this.setBackground(new Color(168, 233, 216)); jLabel1.setToolTipText(""); jLabel1.setText("授课教师"); jLabel1.setBounds(new Rectangle(18, 86, 53, 15)); jLabel2.setText("课程类别"); jLabel2.setBounds(new Rectangle(198, 87, 62, 15)); tctime.setEnabled(false); tctime.setBounds(new Rectangle(265, 123, 77, 20)); tcno.setEnabled(false); tcno.setBounds(new Rectangle(266, 40, 77, 20)); tcteacher.setEnabled(false); tcteacher.setBounds(new Rectangle(91, 85, 93, 20)); jLabel3.setText("课程名称"); jLabel3.setBounds(new Rectangle(20, 45, 60, 15)); jLabel4.setText("课程编码"); jLabel4.setBounds(new Rectangle(193, 42, 65, 15)); jLabel5.setText("上课时间"); jLabel5.setBounds(new Rectangle(199, 124, 57, 15)); tctype.setEnabled(false); tctype.setBounds(new Rectangle(265, 85, 77, 20)); tcplace.setEnabled(false); tcplace.setBounds(new Rectangle(93, 123, 90, 20)); jLabel6.setText("上课地点"); jLabel6.setBounds(new Rectangle(19, 124, 67, 15)); btn_exit.setBounds(new Rectangle(22, 172, 81, 23)); btn_exit.setText("退出"); btn_exit.addActionListener(new Select_coursePanel_btn_exit_actionAdapter(this)); jcb_cname.setBounds(new Rectangle(89, 41, 97, 23)); jcb_cname.addItemListener(new Select_coursecnamePanel_jcb_cname_itemAdapter(this)); this.add(jLabel2); this.add(tctime); this.add(tcteacher); this.add(jLabel5); this.add(tctype); this.add(tcplace); this.add(jLabel6); this.add(tcno); this.add(jLabel4); this.add(jLabel3); this.add(jLabel1); this.add(jcb_cname); this.add(btn_exit); Vector v1=cm.selectCourse(); //调用查询所有课程名称方法 for (int i = 0; i < v1.size();i++) { jcb_cname.addItem(v1.get(i).toString()); //将查询结果添加到下拉列表中 } this.setVisible(true); this.setSize(600, 600); } JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JTextField tctime = new JTextField(); JTextField tcno = new JTextField(); JTextField tcteacher = new JTextField(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JTextField tctype = new JTextField(); JTextField tcplace = new JTextField(); JLabel jLabel6 = new JLabel(); JButton btn_exit = new JButton(); JComboBox jcb_cname = new JComboBox(); CourseManager cm = new CourseManager(); public void btn_exit_actionPerformed(ActionEvent e) { this.setVisible(false); } public void jcb_cname_itemStateChanged(ItemEvent e) { //调用按课程名称查询课程信息 Course c = cm.findOneCourse(jcb_cname.getSelectedItem().toString()); tcno.setText(c.getCno()); tcteacher.setText(c.getCteacher()); tcplace.setText(c.getCplace()); tctype.setText(c.getCtype()); tcplace.setText(c.getCplace()); tctime.setText(c.getCtime()); }}class Select_coursecnamePanel_jcb_cname_itemAdapter implements ItemListener { private Select_coursecnamePanel adaptee; Select_coursecnamePanel_jcb_cname_itemAdapter(Select_coursecnamePanel adaptee) { this.adaptee = adaptee; } public void itemStateChanged(ItemEvent e) { adaptee.jcb_cname_itemStateChanged(e); }}class Select_coursePanel_btn_exit_actionAdapter implements ActionListener { private Select_coursecnamePanel adaptee; Select_coursePanel_btn_exit_actionAdapter(Select_coursecnamePanel adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.btn_exit_actionPerformed(e); }}
view包一共19个类。
本篇介绍了1至8类,由于博客字数限制,后半部分代码放到下篇。
下篇从第9个类开始,介绍9至19类:
下篇:学生选课与成绩管理系统2(剩余代码)
四、运行
连接好数据库之后,在view包下的StuviewFrame右键点击运行。通过IDEA的图形界面,进行增删改查等操作。
以增加学生信息为例:
1:增加学生信息
2:在IDEA中的数据库表中验证,确实添加成功。
3:回到mysql数据库中的student表中验证,也确实添加成功了。
经过多次测试,表明此程序运行成功!
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。