Java+gui+mysql实现学生管理系统
学完JavaSE和数据库部分就可以动手做一个简单的学生管理系统,其实也就是涉及到一些简单的数据库增删查改的内容,学艺不精项目中有些功能可能不是很完善,内容有点多看完可能需要点耐心,但都是干货 ;话不多说,下面进入项目实现步骤…
1. 需求:
学生:
输入学号、初始密码登录,查看个人各科成绩
设置:个人密码修改/退出系统
教师(兼管理员):
学生管理:
新增学生
修改学生
查看所有学生
查找学生(根据学号、根据姓名)
注销学生(修改状态0->1)
成绩管理:
查看成绩
录入成绩
课程管理:
查看课程
新增课程
修改课程
2.实现步骤
步骤1:表设计
这部分就不细讲了,后面我会把数据库文件放在后面,也就创建几张简单的表,我添加的内容也很少,主要能实现功能即可;
步骤2:实体类 (创建的实体类的属性要和表里的一样)
Student:
package com.etc.entity;
public class Student {
private String studentNo;
private String studentName;
private String gender;
private String birth;
private String password;
private int status;
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String studentNo, String studentName, String gender, String birth, String password, int status) {
super();
this.studentNo = studentNo;
this.studentName = studentName;
this.gender = gender;
this.birth = birth;
this.password = password;
this.status = status;
}
@Override
public String toString() {
return "Student [student_no=" + studentNo + ", student_name=" + studentName + ", gender=" + gender
+ ", birth=" + birth + ", password=" + password + ", status=" + status + "]";
}
}
Teacher:
package com.etc.entity;
public class Teacher {
private int teacherId;
private String teacherNo;
private String teacherName;
private String password;
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
public String getTeacherNo() {
return teacherNo;
}
public void setTeacherNo(String teacherNo) {
this.teacherNo = teacherNo;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
public Teacher(int teacherId, String teacherNo, String teacherName, String password) {
super();
this.teacherId = teacherId;
this.teacherNo = teacherNo;
this.teacherName = teacherName;
this.password = password;
}
@Override
public String toString() {
return "Teacher [teacherId=" + teacherId + ", teacherNo=" + teacherNo + ", teacherName=" + teacherName
+ ", password=" + password + "]";
}
}
StuCoSc:
package com.etc.entity;
public class StuCoSc {
private String courseName;
private float courseScore;
private float Score;
private int ScoreId;
private String StudentNo;
private String StudentName;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public float getCourseScore() {
return courseScore;
}
public void setCourseScore(float courseScore) {
this.courseScore = courseScore;
}
public float getScore() {
return Score;
}
public void setScore(float score) {
Score = score;
}
public int getScoreId() {
return ScoreId;
}
public void setScoreId(int scoreId) {
ScoreId = scoreId;
}
public String getStudentNo() {
return StudentNo;
}
public void setStudentNo(String studentNo) {
StudentNo = studentNo;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public StuCoSc() {
super();
// TODO Auto-generated constructor stub
}
public StuCoSc(String courseName, float courseScore, float score) {
super();
this.courseName = courseName;
this.courseScore = courseScore;
Score = score;
}
public StuCoSc(String courseName, float score, int scoreId, String studentNo, String studentName) {
super();
this.courseName = courseName;
Score = score;
ScoreId = scoreId;
StudentNo = studentNo;
StudentName = studentName;
}
@Override
public String toString() {
return "StuCoSc [courseName=" + courseName + ", Score=" + Score + ", ScoreId=" + ScoreId + ", StudentNo="
+ StudentNo + ", StudentName=" + StudentName + "]";
}
public String show() {
return "StuCoSc [courseName=" + courseName + ", courseScore=" + courseScore + ", Score=" + Score + "]";
}
}
Score:
package com.etc.entity;
public class Score {
private int scoreId;
private String studentNo;
private int courseNo;
private float score;
private String pubDate;
public int getScoreId() {
return scoreId;
}
public void setScoreId(int scoreId) {
this.scoreId = scoreId;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public int getCourseNo() {
return courseNo;
}
public void setCourseNo(int courseNo) {
this.courseNo = courseNo;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public Score() {
super();
// TODO Auto-generated constructor stub
}
public Score(int scoreId, String studentNo, int courseNo, float score, String pubDate) {
super();
this.scoreId = scoreId;
this.studentNo = studentNo;
this.courseNo = courseNo;
this.score = score;
this.pubDate = pubDate;
}
@Override
public String toString() {
return "Score [scoreId=" + scoreId + ", studentNo=" + studentNo + ", courseNo=" + courseNo + ", score=" + score
+ ", pubDate=" + pubDate + "]";
}
public Score(int scoreId, String studentNo, float score, String pubDate) {
super();
this.scoreId = scoreId;
this.studentNo = studentNo;
this.score = score;
this.pubDate = pubDate;
}
}
Course:
package com.etc.entity;
public class Course {
private int courseNo;
private String courseName;
private float courseScore;
public int getCourseNo() {
return courseNo;
}
public void setCourseNo(int courseNo) {
this.courseNo = courseNo;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public float getCourseScore() {
return courseScore;
}
public void setCourseScore(float courseScore) {
this.courseScore = courseScore;
}
public Course() {
super();
// TODO Auto-generated constructor stub
}
public Course(int courseNo, String courseName, float courseScore) {
super();
this.courseNo = courseNo;
this.courseName = courseName;
this.courseScore = courseScore;
}
@Override
public String toString() {
return "Course [courseNo=" + courseNo + ", courseName=" + courseName + ", courseScore=" + courseScore + "]";
}
}
步骤3:Dao类
StudentDao:
package com.etc.dao;
import java.util.List;
import com.etc.entity.Student;
public interface StudentDao {
/**
* 注册学生
* @param student 新增学生信息
* @return true->成功 false->失败
*/
boolean insertStu(Student student);
/**
* 修改学生信息
* @param student
* @return
*/
boolean updateStu(Student student);
/**
* 注销学生
* @param student_no
* @return true->成功 false->失败
*/
boolean updateStatus(String student_no);
/**
* 学号密码登录
* @param sno 学号
* @param pwd 密码
* @return
*/
Student stuLogin(String sno, String pwd);
/**
* 查询所有学生
* @return 学生对象列表
*/
List<Student> queryAllStu();
/**
* 学号查询
* @param sno 学号
* @return 学生对象
*/
Student queryStuByNo(String sno);
/**
* 学号姓名查询
* @param sno 学号
* @param sname 姓名
* @return 学生对象
*/
List<Student> queryStuByNoName(String sno,String sname);
/**
* 模糊查询
* @param sname 学生姓名关键字
* @return
*/
List<Student> queryStuByName(String sname);
}
TeacherDao:
package com.etc.dao;
import com.etc.entity.Teacher;
public interface TeacherDao {
/**
* 教师工号密码登录
* @param teacher_no 工号
* @param password 密码
* @return 教师信息
*/
Teacher teaLogin(String teacher_no,String password);
}
ScoreDao:
package com.etc.dao;
import java.util.List;
import com.etc.entity.StuCoSc;
public interface ScoreDao {
/**
* 学生个人成绩查询
* @param sno
* @param sname
* @return
*/
List<StuCoSc> queryScore(String sno,String sname);
/**
* 查询所有成绩管理页面所有字段
* @return 列表
*/
List<StuCoSc> querySCSAll();
/**
* 按学号查询
* @return
*/
List<StuCoSc> querySCSByNo(String sno);
/**
* 按姓名查询
* @param sname
* @return
*/
List<StuCoSc> querySCSByName(String sname);
/**
* 按学号姓名查询
* @param sno
* @param sname
* @return
*/
List<StuCoSc> querySCSByNoName(String sno,String sname);
/**
* 按课程名称查询
* @param courseName
* @return
*/
List<StuCoSc> querySCSByCName(String courseName);
/**
* 按学号课程名查询
* @param sno
* @param courseName
* @return
*/
List<StuCoSc> querySCSByNoCName(String sno,String courseName);
/**
* 录入功能
* @return
*/
boolean insertSCS(StuCoSc stuCoSc);
}
CourseDao:
package com.etc.dao;
import java.util.List;
import com.etc.entity.Course;
public interface CourseDao {
/**
* 查询所有课程
* @return
*/
List<Course> queryCoAll();
/**
* 分别按课程编号和课程名查询
* @return
*/
List<Course> queryCoByCNoName(int cno,String cname);
/**
* 添加课程
* @return
*/
boolean insertCourse(Course course);
/**
* 删除课程
* @param courseNo
* @return
*/
boolean deleteCourse(int courseNo);
/**
* 修改课程
* @param courseNo
* @return
*/
boolean updateCourse(Course course);
}
步骤4:Dao实现类
StudentDaoImpl:
package com.etc.daoimpl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.rowset.CachedRowSet;
import com.etc.dao.StudentDao;
import com.etc.entity.Student;
import com.etc.util.DButil;
public class StudentDaoImpl implements StudentDao {
//注册学生
public boolean insertStu(Student student) {
String sql="insert into tab_student values(?,?,?,?,?,?)";
return DButil.execUpdate(sql,student.getStudentNo(),student.getStudentName(),student.getGender(),
student.getBirth(),student.getPassword(),student.getStatus());
}
//注销学生
public boolean updateStatus(String student_no) {
String sql="update tab_student set status=1 where student_no=?";
return DButil.execUpdate(sql, student_no);
}
//学号密码登录
public Student stuLogin(String sno, String pwd) {
String sql="select * from tab_student where student_no=? and password=?";
CachedRowSet crs=DButil.execQuery(sql, sno,pwd);
Student student=null;
try {
while (crs.next()) {
String student_no = crs.getString(1);
String student_name = crs.getString(2);
String gender = crs.getString(3);
String birth = crs.getString(4);
String password=crs.getString(5);
int status=crs.getInt(6);
student=new Student(student_no, student_name, gender, birth, password, status);
}
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
//查询所有学生
public List<Student> queryAllStu() {
String sql="select * from tab_student";
CachedRowSet crs=DButil.execQuery(sql);
List<Student> list=new ArrayList<Student>();
Student student=null;
try {
while (crs.next()) {
String student_no = crs.getString(1);
String student_name = crs.getString(2);
String gender = crs.getString(3);
String birth = crs.getString(4);
String password=crs.getString(5);
int status=crs.getInt(6);
student=new Student(student_no, student_name, gender, birth, password, status);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//学号查询
public Student queryStuByNo(String sno) {
String sql="select * from tab_student where student_no=?";
CachedRowSet crs=DButil.execQuery(sql, sno);
Student student=null;
try {
while (crs.next()) {
String student_no = crs.getString(1);
String student_name = crs.getString(2);
String gender = crs.getString(3);
String birth = crs.getString(4);
String password=crs.getString(5);
int status=crs.getInt(6);
student=new Student(student_no, student_name, gender, birth, password, status);
}
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
//模糊查询
public List<Student> queryStuByName(String sname) {
String sql="select * from tab_student where student_name like ?";
CachedRowSet crs=DButil.execQuery(sql,"%"+sname+"%");
List<Student> list=new ArrayList<Student>();
Student student=null;
try {
while (crs.next()) {
String student_no = crs.getString(1);
String student_name = crs.getString(2);
String gender = crs.getString(3);
String birth = crs.getString(4);
String password=crs.getString(5);
int status=crs.getInt(6);
student=new Student(student_no, student_name, gender, birth, password, status);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//修改学生信息
@Override
public boolean updateStu(Student student) {
String sql="update tab_student set student_name=?,gender=?,birth=?,password=?,status=? where student_no=?";
return DButil.execUpdate(sql, student.getStudentName(),student.getGender(),student.getBirth(),
student.getPassword(),student.getStatus(),student.getStudentNo());
}
//学号姓名查询
@Override
public List<Student> queryStuByNoName(String sno, String sname) {
String sql="select * from tab_student where student_no=? and student_name=?";
CachedRowSet crs=DButil.execQuery(sql, sno,sname);
List<Student> list=new ArrayList<Student>();
Student student=null;
try {
while (crs.next()) {
String student_no = crs.getString(1);
String student_name = crs.getString(2);
String gender = crs.getString(3);
String birth = crs.getString(4);
String password=crs.getString(5);
int status=crs.getInt(6);
student=new Student(student_no, student_name, gender, birth, password, status);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
TeacherDaoImpl:
package com.etc.daoimpl;
import java.sql.SQLException;
import javax.sql.rowset.CachedRowSet;
import com.etc.dao.TeacherDao;
import com.etc.entity.Teacher;
import com.etc.util.DButil;
public class TeacherDaoImpl implements TeacherDao {
//教师工号密码登录
@Override
public Teacher teaLogin(String teano, String pwd) {
String sql="select * from tab_teacher where teacher_no=? and password=?";
CachedRowSet crs=DButil.execQuery(sql,teano,pwd);
Teacher teacher=null;
try {
while(crs.next()) {
int teacher_id=crs.getInt(1);
String teacher_no=crs.getString(2);
String teacher_name=crs.getString(3);
String password=crs.getString(4);
teacher = new Teacher(teacher_id, teacher_no, teacher_name, password);
}
} catch (SQLException e) {
e.printStackTrace();
}
return teacher;
}
}
ScoreDaoImpl:
package com.etc.daoimpl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.rowset.CachedRowSet;
import com.etc.dao.ScoreDao;
import com.etc.entity.StuCoSc;
import com.etc.util.DButil;
public class ScoreDaoImpl implements ScoreDao {
//学生个人成绩查询
@Override
public List<StuCoSc> queryScore(String sno, String sname) {
String sql="select b.course_name,b.course_score,c.score from tab_student a,tab_course b,tab_score c "
+ "where a.student_no=c.student_no and b.course_no=c.course_no and a.student_no=? and a.student_name=?";
CachedRowSet crs=DButil.execQuery(sql, sno,sname);
List<StuCoSc> list=new ArrayList<StuCoSc>();
StuCoSc stuCoSc=null;
try {
while (crs.next()) {
String courseName=crs.getString(1);
float courseScore=crs.getFloat(2);
float score=crs.getFloat(3);
stuCoSc=new StuCoSc(courseName, courseScore, score);
list.add(stuCoSc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//查询所有成绩管理页面所有字段
@Override
public List<StuCoSc> querySCSAll() {
String sql="select c.score_id,a.student_no,a.student_name,b.course_name,c.score from tab_student a,tab_course b,tab_score c\r\n" +
"where a.student_no=c.student_no and b.course_no=c.course_no order by c.score_id";
CachedRowSet crs=DButil.execQuery(sql);
List<StuCoSc> list=new ArrayList<StuCoSc>();
StuCoSc stuCoSc=null;
try {
while (crs.next()) {
int scoreId=crs.getInt(1);
String studentNo=crs.getString(2);
String studentName=crs.getString(3);
String courseName=crs.getString(4);
float score=crs.getFloat(5);
stuCoSc=new StuCoSc(courseName, score, scoreId, studentNo, studentName);
list.add(stuCoSc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//按学号查询
@Override
public List<StuCoSc> querySCSByNo(String sno) {
String sql="select c.score_id,a.student_no,a.student_name,b.course_name,c.score from tab_student a,tab_course b,tab_score c\r\n" +
"where a.student_no=c.student_no and b.course_no=c.course_no and a.student_no=?";
CachedRowSet crs=DButil.execQuery(sql,sno);
List<StuCoSc> list=new ArrayList<StuCoSc>();
StuCoSc stuCoSc=null;
try {
while (crs.next()) {
int scoreId=crs.getInt(1);
String studentNo=crs.getString(2);
String studentName=crs.getString(3);
String courseName=crs.getString(4);
float score=crs.getFloat(5);
stuCoSc=new StuCoSc(courseName, score, scoreId, studentNo, studentName);
list.add(stuCoSc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//按姓名查询
@Override
public List<StuCoSc> querySCSByName(String sname) {
String sql="select c.score_id,a.student_no,a.student_name,b.course_name,c.score from tab_student a,tab_course b,tab_score c\r\n" +
"where a.student_no=c.student_no and b.course_no=c.course_no and a.student_name=?";
CachedRowSet crs=DButil.execQuery(sql,sname);
List<StuCoSc> list=new ArrayList<StuCoSc>();
StuCoSc stuCoSc=null;
try {
while (crs.next()) {
int scoreId=crs.getInt(1);
String studentNo=crs.getString(2);
String studentName=crs.getString(3);
String courseName=crs.getString(4);
float score=crs.getFloat(5);
stuCoSc=new StuCoSc(courseName, score, scoreId, studentNo, studentName);
list.add(stuCoSc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//按学号姓名查询
@Override
public List<StuCoSc> querySCSByNoName(String sno, String sname) {
String sql="select c.score_id,a.student_no,a.student_name,b.course_name,c.score from tab_student a,tab_course b,tab_score c\r\n" +
"where a.student_no=c.student_no and b.course_no=c.course_no and a.student_no=? and a.student_name=?";
CachedRowSet crs=DButil.execQuery(sql,sno,sname);
List<StuCoSc> list=new ArrayList<StuCoSc>();
StuCoSc stuCoSc=null;
try {
while (crs.next()) {
int scoreId=crs.getInt(1);
String studentNo=crs.getString(2);
String studentName=crs.getString(3);
String courseName=crs.getString(4);
float score=crs.getFloat(5);
stuCoSc=new StuCoSc(courseName, score, scoreId, studentNo, studentName);
list.add(stuCoSc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//按课程名称查询
@Override
public List<StuCoSc> querySCSByCName(String CName) {
String sql="select c.score_id,a.student_no,a.student_name,b.course_name,c.score from tab_student a,tab_course b,tab_score c\r\n" +
"where a.student_no=c.student_no and b.course_no=c.course_no and b.course_name=?";
CachedRowSet crs=DButil.execQuery(sql,CName);
List<StuCoSc> list=new ArrayList<StuCoSc>();
StuCoSc stuCoSc=null;
try {
while (crs.next()) {
int scoreId=crs.getInt(1);
String studentNo=crs.getString(2);
String studentName=crs.getString(3);
String courseName=crs.getString(4);
float score=crs.getFloat(5);
stuCoSc=new StuCoSc(courseName, score, scoreId, studentNo, studentName);
list.add(stuCoSc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//录入功能
@Override
public boolean insertSCS(StuCoSc stuCoSc) {
String sql="insert into tab_score value(null,?,(select course_no from tab_course where course_name=?),?,now());";
return DButil.execUpdate(sql,stuCoSc.getStudentNo(),stuCoSc.getCourseName(),stuCoSc.getScore());
}
//按学号课程名查询
@Override
public List<StuCoSc> querySCSByNoCName(String sno, String courseName1) {
String sql="select c.score_id,a.student_no,a.student_name,b.course_name,c.score from tab_student a,tab_course b,tab_score c\r\n" +
"where a.student_no=c.student_no and b.course_no=c.course_no and a.student_no=? and b.course_name=?";
CachedRowSet crs=DButil.execQuery(sql,sno,courseName1);
List<StuCoSc> list=new ArrayList<StuCoSc>();
StuCoSc stuCoSc=null;
try {
while (crs.next()) {
int scoreId=crs.getInt(1);
String studentNo=crs.getString(2);
String studentName=crs.getString(3);
String courseName=crs.getString(4);
float score=crs.getFloat(5);
stuCoSc=new StuCoSc(courseName, score, scoreId, studentNo, studentName);
list.add(stuCoSc);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
CourseDaoImpl:
package com.etc.daoimpl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.rowset.CachedRowSet;
import com.etc.dao.CourseDao;
import com.etc.entity.Course;
import com.etc.util.DButil;
public class CourseDaoImpl implements CourseDao {
//查询所有课程
@Override
public List<Course> queryCoAll() {
String sql="select * from tab_course";
CachedRowSet crs=DButil.execQuery(sql);
List<Course> list=new ArrayList<Course>();
Course course=null;
try {
while (crs.next()) {
int courseNo=crs.getInt(1);
String courseName=crs.getString(2);
float courseScore=crs.getFloat(3);
course=new Course(courseNo, courseName, courseScore);
list.add(course);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//添加课程
@Override
public boolean insertCourse(Course course) {
String sql="insert into tab_course value(null,?,?)";
return DButil.execUpdate(sql,course.getCourseName(),course.getCourseScore());
}
//删除课程
@Override
public boolean deleteCourse(int courseNo) {
String sql="delete from tab_course where course_no=?";
return DButil.execUpdate(sql, courseNo);
}
//修改课程
@Override
public boolean updateCourse(Course course) {
String sql="update tab_course set course_name=?,course_score=? where course_no=?";
return DButil.execUpdate(sql, course.getCourseName(),course.getCourseScore(),course.getCourseNo());
}
//按课程编号和课程名查询
@Override
public List<Course> queryCoByCNoName(int cno,String cname) {
CachedRowSet crs=null;
if (cno!=0 && "".equals(cname.trim())) {
String sql="select * from tab_course where course_no=?";
crs=DButil.execQuery(sql,cno);
} else if (cno==0 && !"".equals(cname.trim())) {
String sql="select * from tab_course where course_name=?";
crs=DButil.execQuery(sql,cname);
} else if (cno!=0 && !"".equals(cname.trim())) {
String sql="select * from tab_course where course_no=? and course_name=?";
crs=DButil.execQuery(sql,cno,cname);
}
List<Course> list=new ArrayList<Course>();
Course course=null;
try {
while (crs.next()) {
int courseNo=crs.getInt(1);
String courseName=crs.getString(2);
float courseScore=crs.getFloat(3);
course=new Course(courseNo, courseName, courseScore);
list.add(course);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
步骤5:gui界面实现
StuTeaLogin(登录界面):
package com.etc.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.etc.daoimpl.StudentDaoImpl;
import com.etc.daoimpl.TeacherDaoImpl;
import com.etc.entity.Student;
import com.etc.entity.Teacher;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class StuTeaLogin {
private JFrame frame;
private JTextField snoinput;
private JPasswordField pwdinput;
private StudentDaoImpl sImpl=new StudentDaoImpl();
private TeacherDaoImpl tImpl=new TeacherDaoImpl();
private JTextField teanoinput;
private JPasswordField teapwdinput;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
StuTeaLogin window = new StuTeaLogin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public StuTeaLogin() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//设置窗口页面中央居中
frame.setLocationRelativeTo(null);
//不允许调整窗口的大小
frame.setResizable(false);
//设置窗口的名称
frame.setTitle("登录界面");
//显示窗口
frame.setVisible(true);
JLabel StuMenTitle = new JLabel("学生信息管理系统");
StuMenTitle.setBounds(165, 33, 107, 15);
frame.getContentPane().add(StuMenTitle);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(105, 70, 248, 168);
frame.getContentPane().add(tabbedPane);
JPanel studentPan = new JPanel();
tabbedPane.addTab("学生登录", null, studentPan, null);
studentPan.setLayout(null);
JLabel snolab = new JLabel("学号:");
snolab.setBounds(29, 20, 39, 15);
studentPan.add(snolab);
snoinput = new JTextField();
snoinput.setBounds(80, 14, 122, 28);
studentPan.add(snoinput);
snoinput.setColumns(10);
JLabel pwdlab = new JLabel("密码:");
pwdlab.setBounds(29, 61, 39, 15);
studentPan.add(pwdlab);
JButton loginbtn = new JButton("登录");
loginbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取学号和密码
String sno=snoinput.getText();
char[] ch = pwdinput.getPassword();
String password=new String(ch);
if (!sno.equals("") && !password.equals("")) {
Student student =sImpl.stuLogin(sno, password);
if (student!=null) {
StuMainFrame stuMainFrame=new StuMainFrame(student);
frame.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "登录失败");
}
} else {
JOptionPane.showConfirmDialog(null, "用户名和密码不能为空");
}
}
});
loginbtn.setBounds(76, 106, 71, 23);
studentPan.add(loginbtn);
pwdinput = new JPasswordField();
pwdinput.setBounds(80, 55, 122, 28);
studentPan.add(pwdinput);
JPanel teacherPan = new JPanel();
teacherPan.setOpaque(false);
tabbedPane.addTab("教师登录", null, teacherPan, null);
teacherPan.setLayout(null);
JLabel teanolab = new JLabel("工号:");
teanolab.setBounds(24, 25, 34, 15);
teacherPan.add(teanolab);
teanoinput = new JTextField();
teanoinput.setBounds(68, 19, 137, 27);
teacherPan.add(teanoinput);
teanoinput.setColumns(10);
JLabel teapwdlab = new JLabel("密码:");
teapwdlab.setBounds(24, 62, 44, 15);
teacherPan.add(teapwdlab);
teapwdinput = new JPasswordField();
teapwdinput.setBounds(68, 56, 137, 27);
teacherPan.add(teapwdinput);
JButton tealogbtn = new JButton("登录");
tealogbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取工号和密码
String teano=teanoinput.getText();
char[] ch1 = teapwdinput.getPassword();
String teapwd=new String(ch1);
if (!teano.equals("") && !teapwd.equals("")) {
Teacher teacher =tImpl.teaLogin(teano, teapwd);
if (teacher!=null) {
TeaMainFrame teaMainFrame=new TeaMainFrame();
frame.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "登录失败");
}
} else {
JOptionPane.showConfirmDialog(null, "用户名和密码不能为空");
}
}
});
tealogbtn.setBounds(68, 106, 72, 23);
teacherPan.add(tealogbtn);
}
}
StuMainFrame(学生查询成绩界面):
package com.etc.ui;
import java.awt.EventQueue;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.etc.daoimpl.ScoreDaoImpl;
import com.etc.daoimpl.StudentDaoImpl;
import com.etc.entity.StuCoSc;
import com.etc.entity.Student;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class StuMainFrame {
private JFrame frame;
private JTextField snoinput;
private JTextField snameinput;
private JTable table;
private StudentDaoImpl sImpl=new StudentDaoImpl();
private ScoreDaoImpl scoreDaoImpl=new ScoreDaoImpl();
/**
* Launch the application.
*/
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StuMainFrame window = new StuMainFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
/**
* Create the application.
*/
public StuMainFrame(Student student) {
initialize(student);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(Student student) {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setTitle("学生个人成绩查询");
frame.getContentPane().setLayout(null);
JLabel snolab = new JLabel("学号:");
snolab.setBounds(72, 32, 38, 15);
frame.getContentPane().add(snolab);
snoinput = new JTextField();
snoinput.setBounds(102, 26, 81, 28);
frame.getContentPane().add(snoinput);
snoinput.setColumns(10);
JLabel snamelab = new JLabel("姓名:");
snamelab.setBounds(221, 32, 38, 15);
frame.getContentPane().add(snamelab);
snameinput = new JTextField();
snameinput.setBounds(257, 26, 92, 28);
frame.getContentPane().add(snameinput);
snameinput.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(72, 92, 310, 139);
frame.getContentPane().add(scrollPane);
table = new JTable();
snoinput.setText(student.getStudentNo());
snameinput.setText(student.getStudentName());
String sno=student.getStudentNo();
String sname=student.getStudentName();
List<StuCoSc> list =scoreDaoImpl.queryScore(sno, sname);
String[] col= {"课程名","课程学分","成绩"};
Object[][] data=new Object[list.size()][3];
for (int i = 0; i < list.size(); i++) {
StuCoSc stuCoSc =list.get(i);
data[i][0]=stuCoSc.getCourseName();
data[i][1]=stuCoSc.getCourseScore();
data[i][2]=stuCoSc.getScore();
}
table.setModel(new DefaultTableModel(
data,
col
));
scrollPane.setViewportView(table);
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 105, 21);
frame.getContentPane().add(menuBar);
JMenu menu = new JMenu("首页");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("退出");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StuTeaLogin stuTeaLogin=new StuTeaLogin();
frame.setVisible(false);
}
});
menu.add(menuItem);
//显示窗口
frame.setVisible(true);
}
}
TeaMainFrame(教师管理界面):
package com.etc.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.etc.daoimpl.StudentDaoImpl;
import com.etc.daoimpl.TeacherDaoImpl;
import com.etc.entity.StuCoSc;
import com.etc.entity.Student;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTabbedPane;
public class TeaMainFrame {
private JFrame frame;
private JTextField snoqueinput;
private JTextField snamequeinput;
private JScrollPane scrollPane;
private JTable table;
private JTextField snoinsinput;
private JTextField snameinsinput;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField birthinput;
private JPasswordField pwdinsinput;
private JTextField snoupdinput;
private JTextField snameupdinput;
private JTextField birthupdinput;
private JPasswordField pwdupdinput;
private final ButtonGroup buttonGroup_1 = new ButtonGroup();
private StudentDaoImpl sImpl=new StudentDaoImpl();
private JRadioButton manupd=null;
private JRadioButton womanupd=null;
private JLabel birthupdlab=null;
private JLabel pwdupdlab=null;
private JButton updatebtn=null;
private JRadioButton man=null;
/**
* Launch the application.
*/
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TeaMainFrame window = new TeaMainFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
/**
* Create the application.
*/
public TeaMainFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 511, 627);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setTitle("学生管理界面");
frame.setVisible(true);
frame.getContentPane().setLayout(null);
JLabel snolab = new JLabel("学号:");
snolab.setBounds(26, 20, 42, 15);
frame.getContentPane().add(snolab);
snoqueinput = new JTextField();
snoqueinput.setBounds(67, 14, 90, 28);
frame.getContentPane().add(snoqueinput);
snoqueinput.setColumns(10);
JLabel snamelab = new JLabel("学生姓名:");
snamelab.setBounds(173, 20, 70, 15);
frame.getContentPane().add(snamelab);
snamequeinput = new JTextField();
snamequeinput.setBounds(231, 14, 90, 28);
frame.getContentPane().add(snamequeinput);
snamequeinput.setColumns(10);
JButton querybtn = new JButton("查询");
querybtn.setBounds(331, 16, 65, 23);
querybtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取学号和姓名
String snoquery=snoqueinput.getText();
String snamequery=snamequeinput.getText();
//按学号查询
if (snoquery.trim().length()!=0 && snamequery.trim().length()==0) {
Student student=sImpl.queryStuByNo(snoquery);
if (student!=null) {
//在表格中显示
List<Student> list=new ArrayList<Student>();
list.add(student);
//调用表格的方法
showTableData(list);
} else {
List<Student> list=new ArrayList<Student>();
showTableData(list);
}
} else if (snoquery.trim().length()==0 && snamequery.trim().length()!=0) {
List<Student> list=sImpl.queryStuByName(snamequery);
showTableData(list);
} else if (snoquery.trim().length()==0 && snamequery.trim().length()==0) {
List<Student> list=sImpl.queryAllStu();
showTableData(list);
} else {
List<Student> list=new ArrayList<Student>();
showTableData(list);
}
}
});
frame.getContentPane().add(querybtn);
scrollPane = new JScrollPane();
scrollPane.setBounds(26, 70, 452, 163);
frame.getContentPane().add(scrollPane);
//默认显示所有学生
List<Student> list =sImpl.queryAllStu();
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//选中哪一行
int row =table.getSelectedRow();//从0开始
//获取列的值
String sno=(String)table.getValueAt(row, 0);
String sname=(String)table.getValueAt(row, 1);
String gender=(String)table.getValueAt(row, 2);
String birth=(String)table.getValueAt(row, 3);
String pwd=(String)table.getValueAt(row, 4);
//将获取的值赋值给输入框
snoupdinput.setText(sno);
snameupdinput.setText(sname);
if ("男".equals(gender)) {
manupd.setSelected(true);
}else {
womanupd.setSelected(true);
}
birthupdinput.setText(birth);
pwdupdinput.setText(pwd);
}
});
showTableData(list);
JButton scoremanagebtn = new JButton("成绩管理");
scoremanagebtn.setBounds(406, 10, 89, 23);
scoremanagebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ScoreManageFrame sf=new ScoreManageFrame();
frame.setVisible(false);
}
});
frame.getContentPane().add(scoremanagebtn);
JButton coumanbtn = new JButton("课程管理");
coumanbtn.setBounds(406, 37, 89, 23);
coumanbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CourseManage cManage=new CourseManage();
frame.setVisible(false);
}
});
frame.getContentPane().add(coumanbtn);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(26, 243, 452, 168);
frame.getContentPane().add(tabbedPane);
JPanel insertpan = new JPanel();
tabbedPane.addTab("新增学生", null, insertpan, null);
insertpan.setLayout(null);
JLabel snoinslab = new JLabel("学号");
snoinslab.setBounds(21, 20, 38, 15);
insertpan.add(snoinslab);
snoinsinput = new JTextField();
snoinsinput.setBounds(54, 14, 76, 28);
insertpan.add(snoinsinput);
snoinsinput.setColumns(10);
JLabel snameinslab = new JLabel("学生姓名");
snameinslab.setBounds(155, 20, 54, 15);
insertpan.add(snameinslab);
snameinsinput = new JTextField();
snameinsinput.setBounds(213, 14, 76, 28);
insertpan.add(snameinsinput);
snameinsinput.setColumns(10);
JLabel sexlab = new JLabel("性别");
sexlab.setBounds(311, 20, 38, 15);
insertpan.add(sexlab);
man = new JRadioButton("男");
man.setSelected(true);
buttonGroup.add(man);
man.setBounds(349, 16, 38, 23);
insertpan.add(man);
JRadioButton woman = new JRadioButton("女");
buttonGroup.add(woman);
woman.setBounds(396, 16, 54, 23);
insertpan.add(woman);
JLabel birthlab = new JLabel("出生日期");
birthlab.setBounds(21, 68, 54, 15);
insertpan.add(birthlab);
birthinput = new JTextField();
birthinput.setBounds(83, 62, 95, 28);
insertpan.add(birthinput);
birthinput.setColumns(10);
JLabel pwdinslab = new JLabel("密码");
pwdinslab.setBounds(213, 68, 37, 15);
insertpan.add(pwdinslab);
pwdinsinput = new JPasswordField();
pwdinsinput.setBounds(260, 62, 102, 28);
insertpan.add(pwdinsinput);
JButton insertbtn = new JButton("新增学生");
insertbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取输入框的值
String sno=snoinsinput.getText();
String sname=snameinsinput.getText();
String birth=birthinput.getText();
char[] ch=pwdinsinput.getPassword();
String password=new String(ch);
String gender=null;
if (man.isSelected()) {
gender="男";
} else {
gender="女";
}
//创建对象
Student student=new Student(sno, sname, gender, birth, password,0);
//调用dao添加功能
boolean flag=sImpl.insertStu(student);
if (flag) {
JOptionPane.showMessageDialog(null, "添加成功");
} else {
JOptionPane.showMessageDialog(null, "添加失败");
}
//查询所有或取集合
List<Student> list = sImpl.queryAllStu();
showTableData(list);
}
});
insertbtn.setBounds(334, 106, 88, 23);
insertpan.add(insertbtn);
JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
tabbedPane_1.setBounds(26, 421, 452, 168);
frame.getContentPane().add(tabbedPane_1);
JPanel updatepan = new JPanel();
tabbedPane_1.addTab("修改学生", null, updatepan, null);
updatepan.setLayout(null);
JLabel snouplab = new JLabel("学号");
snouplab.setBounds(20, 21, 38, 15);
updatepan.add(snouplab);
snoupdinput = new JTextField();
snoupdinput.setColumns(10);
snoupdinput.setBounds(51, 14, 76, 29);
updatepan.add(snoupdinput);
JLabel snameupdlab = new JLabel("学生姓名");
snameupdlab.setBounds(147, 21, 54, 15);
updatepan.add(snameupdlab);
snameupdinput = new JTextField();
snameupdinput.setColumns(10);
snameupdinput.setBounds(200, 14, 76, 29);
updatepan.add(snameupdinput);
JLabel sexupdlab = new JLabel("性别");
sexupdlab.setBounds(313, 21, 38, 15);
updatepan.add(sexupdlab);
manupd = new JRadioButton("男");
buttonGroup_1.add(manupd);
manupd.setSelected(true);
manupd.setBounds(348, 17, 38, 23);
updatepan.add(manupd);
womanupd = new JRadioButton("女");
buttonGroup_1.add(womanupd);
womanupd.setBounds(392, 17, 54, 23);
updatepan.add(womanupd);
birthupdlab = new JLabel("出生日期");
birthupdlab.setBounds(20, 67, 54, 15);
updatepan.add(birthupdlab);
birthupdinput = new JTextField();
birthupdinput.setColumns(10);
birthupdinput.setBounds(84, 60, 99, 29);
updatepan.add(birthupdinput);
pwdupdlab = new JLabel("密码");
pwdupdlab.setBounds(212, 67, 38, 15);
updatepan.add(pwdupdlab);
pwdupdinput = new JPasswordField();
pwdupdinput.setBounds(261, 60, 102, 29);
updatepan.add(pwdupdinput);
updatebtn = new JButton("修改学生");
updatebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取输入框的值
String sno=snoupdinput.getText();
String sname=snameupdinput.getText();
String birth=birthupdinput.getText();
char[] ch=pwdupdinput.getPassword();
String password=new String(ch);
String gender=null;
if (man.isSelected()) {
gender="男";
} else {
gender="女";
}
//调用Dao修改方法
boolean flag=sImpl.updateStu(new Student(sno, sname, gender, birth, password, 0));
if (flag) {
JOptionPane.showMessageDialog(null, "修改成功");
} else {
JOptionPane.showMessageDialog(null, "修改失败");
}
//查所有
List<Student> list = sImpl.queryAllStu();
showTableData(list);
}
});
updatebtn.setBounds(247, 106, 88, 23);
updatepan.add(updatebtn);
JButton statusbtn = new JButton("注销学生");
statusbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取学号
String sno=snoupdinput.getText();
//调用dao注销学生方法
boolean flag=sImpl.updateStatus(sno);
if (flag) {
JOptionPane.showMessageDialog(null, "注销成功");
} else {
JOptionPane.showMessageDialog(null, "注销失败");
}
//查所有
List<Student> list = sImpl.queryAllStu();
showTableData(list);
}
});
statusbtn.setBounds(345, 106, 92, 23);
updatepan.add(statusbtn);
//显示窗口
frame.setVisible(true);
}
/**
* 对表格进行填充
* @param list
*/
public void showTableData(List<Student> list) {
//列字段
String[] col= {"学号","学生姓名","性别","出生日期","密码","状态"};
//数据
Object[][] data=new Object[list.size()][6];
for (int i = 0; i < list.size(); i++) {
Student student =list.get(i);
data[i][0]=student.getStudentNo();
data[i][1]=student.getStudentName();
data[i][2]=student.getGender();
data[i][3]=student.getBirth();
data[i][4]=student.getPassword();
if (student.getStatus()==0) {
data[i][5]="正常";
} else {
data[i][5]="注销";
}
}
table.setModel(new DefaultTableModel(
data,
col
));
scrollPane.setViewportView(table);
}
}
ScoreManageFrame(成绩管理界面):
package com.etc.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.etc.daoimpl.CourseDaoImpl;
import com.etc.daoimpl.ScoreDaoImpl;
import com.etc.entity.Course;
import com.etc.entity.Score;
import com.etc.entity.StuCoSc;
import com.etc.entity.Student;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTabbedPane;
public class ScoreManageFrame {
private JFrame frame;
private JTextField snoinput_que;
private JTextField snameinput_que;
private JTable table;
private JTextField snoinput_ent;
private JTextField snameinpu_ent;
private JTextField scoreinput_ent;
private JScrollPane scrollPane=null;
private ScoreDaoImpl scoreDaoImpl=new ScoreDaoImpl();
private CourseDaoImpl cImpl=new CourseDaoImpl();
/**
* Launch the application.
*/
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ScoreManageFrame window = new ScoreManageFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
/**
* Create the application.
*/
public ScoreManageFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 542, 501);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setTitle("成绩管理界面");
frame.getContentPane().setLayout(null);
JLabel snolab_que = new JLabel("学号:");
snolab_que.setBounds(20, 14, 41, 15);
frame.getContentPane().add(snolab_que);
snoinput_que = new JTextField();
snoinput_que.setBounds(59, 7, 93, 30);
frame.getContentPane().add(snoinput_que);
snoinput_que.setColumns(10);
JLabel label = new JLabel("姓名:");
label.setBounds(161, 14, 41, 15);
frame.getContentPane().add(label);
snameinput_que = new JTextField();
snameinput_que.setBounds(197, 7, 99, 30);
frame.getContentPane().add(snameinput_que);
snameinput_que.setColumns(10);
JLabel courselab_que = new JLabel("课程:");
courselab_que.setBounds(316, 14, 41, 15);
frame.getContentPane().add(courselab_que);
//下拉框数据填充
JComboBox coursebox = new JComboBox();
//获取所有课程
List<Course> courselist=cImpl.queryCoAll();
//创建填充数据数组
String[] courseNamelist=new String[courselist.size()+1];
//设置默认数据
courseNamelist[0]="全部课程";
//遍历获取课程将所有课程名填充进字符串数组
for (int i = 0; i < courselist.size(); i++) {
courseNamelist[i+1]=courselist.get(i).getCourseName();
}
//将填充完的数组放到下拉框中
coursebox.setModel(new DefaultComboBoxModel(courseNamelist));
coursebox.setBounds(345, 11, 86, 21);
frame.getContentPane().add(coursebox);
JButton querybtn = new JButton("查询");
querybtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取学号和姓名和课程名
String snoquery=snoinput_que.getText();
String snamequery=snameinput_que.getText();
String courseName=coursebox.getSelectedItem().toString().trim();
//按学号查询
if (snoquery.trim().length()!=0 && snamequery.trim().length()==0 && "全部课程".equals(courseName)) {
List<StuCoSc> list=scoreDaoImpl.querySCSByNo(snoquery);
if (list!=null) {
//调用表格的方法
showTableData(list);
} else {
List<StuCoSc> list1=new ArrayList<StuCoSc>();
showTableData(list1);
}
//按姓名查询
} else if (snoquery.trim().length()==0 && snamequery.trim().length()!=0 && "全部课程".equals(courseName)) {
List<StuCoSc> list=scoreDaoImpl.querySCSByName(snamequery);
showTableData(list);
//按课程名查询
} else if (snoquery.trim().length()==0 && snamequery.trim().length()==0 ) {
if ("全部课程".equals(courseName)) {
List<StuCoSc> list=scoreDaoImpl.querySCSAll();
showTableData(list);
} else {
List<StuCoSc> list=scoreDaoImpl.querySCSByCName(courseName);
showTableData(list);
}
//按学号和姓名查询
} else if (snoquery.trim().length()!=0 && snamequery.trim().length()!=0 && "全部课程".equals(courseName)) {
List<StuCoSc> list=scoreDaoImpl.querySCSByNoName(snoquery, snamequery);
showTableData(list);
//按学号课程名查询
} else if (snoquery.trim().length()!=0 && snamequery.trim().length()==0 && !"全部课程".equals(courseName)) {
List<StuCoSc> list=scoreDaoImpl.querySCSByNoCName(snoquery, courseName);
showTableData(list);
}else {
List<StuCoSc> list=new ArrayList<StuCoSc>();
showTableData(list);
}
}
});
querybtn.setBounds(454, 10, 72, 23);
frame.getContentPane().add(querybtn);
scrollPane = new JScrollPane();
scrollPane.setBounds(20, 56, 506, 188);
frame.getContentPane().add(scrollPane);
//默认显示所有学生
List<StuCoSc> list =scoreDaoImpl.querySCSAll();
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//选中哪一行
int row =table.getSelectedRow();//从0开始
//获取列的值
String sno=(String)table.getValueAt(row, 1);
String sname=(String)table.getValueAt(row, 2);
//将获取的值赋值给输入框
snoinput_ent.setText(sno);
snameinpu_ent.setText(sname);
}
});
showTableData(list);
//获取所有课程
List<Course> courselist1=cImpl.queryCoAll();
//创建填充数据数组
String[] courseNamelist1=new String[courselist1.size()+1];
//设置默认数据
courseNamelist1[0]="全部课程";
//遍历获取课程将所有课程名填充进字符串数组
for (int i = 0; i < courselist1.size(); i++) {
courseNamelist1[i+1]=courselist1.get(i).getCourseName();
}
JButton button = new JButton("返回上一页");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeaMainFrame teaMainFrame=new TeaMainFrame();
frame.setVisible(false);
}
});
button.setBounds(20, 440, 93, 23);
frame.getContentPane().add(button);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(20, 271, 506, 154);
frame.getContentPane().add(tabbedPane);
JPanel enterpanl = new JPanel();
tabbedPane.addTab("录入成绩", null, enterpanl, null);
enterpanl.setLayout(null);
JLabel snolab_ent = new JLabel("学号:");
snolab_ent.setBounds(20, 27, 54, 15);
enterpanl.add(snolab_ent);
snoinput_ent = new JTextField();
snoinput_ent.setBounds(64, 21, 105, 28);
enterpanl.add(snoinput_ent);
snoinput_ent.setColumns(10);
JLabel snamelab_ent = new JLabel("姓名");
snamelab_ent.setBounds(205, 27, 54, 15);
enterpanl.add(snamelab_ent);
snameinpu_ent = new JTextField();
snameinpu_ent.setBounds(241, 21, 105, 28);
enterpanl.add(snameinpu_ent);
snameinpu_ent.setColumns(10);
JLabel courselab_ent = new JLabel("课程名:");
courselab_ent.setBounds(20, 80, 54, 15);
enterpanl.add(courselab_ent);
JComboBox coursebox_ent = new JComboBox();
//将填充完的数组放到下拉框中
coursebox_ent.setModel(new DefaultComboBoxModel(courseNamelist1));
coursebox_ent.setBounds(64, 77, 83, 21);
enterpanl.add(coursebox_ent);
JLabel scorelab_ent = new JLabel("成绩:");
scorelab_ent.setBounds(205, 80, 54, 15);
enterpanl.add(scorelab_ent);
scoreinput_ent = new JTextField();
scoreinput_ent.setBounds(241, 74, 105, 28);
enterpanl.add(scoreinput_ent);
scoreinput_ent.setColumns(10);
JButton enterbtn = new JButton("录入");
enterbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取输入框的值
String sno=snoinput_ent.getText();
String sname=snameinpu_ent.getText();
String courseName=coursebox_ent.getSelectedItem().toString().trim();
float score=Float.parseFloat(scoreinput_ent.getText());
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String pubDate=simpleDateFormat.format(date);
//调用Dao录入方法
boolean flag=scoreDaoImpl.insertSCS(new StuCoSc(courseName, score, 0, sno, sname));
if (flag) {
JOptionPane.showMessageDialog(null, "录入成功");
} else {
JOptionPane.showMessageDialog(null, "录入失败");
}
//调用查询全部
List<StuCoSc> list = scoreDaoImpl.querySCSAll();
showTableData(list);
}
});
enterbtn.setBounds(388, 52, 74, 23);
enterpanl.add(enterbtn);
frame.setVisible(true);
}
/**
* 对表格进行填充
* @param list
*/
public void showTableData(List<StuCoSc> list) {
//列字段
String[] col= {"成绩编号","学生学号","学生姓名","课程名","成绩"};
//数据
Object[][] data=new Object[list.size()][5];
for (int i = 0; i < list.size(); i++) {
StuCoSc stuCoSc =list.get(i);
data[i][0]=stuCoSc.getScoreId();
data[i][1]=stuCoSc.getStudentNo();
data[i][2]=stuCoSc.getStudentName();
data[i][3]=stuCoSc.getCourseName();
data[i][4]=stuCoSc.getScore();
}
table.setModel(new DefaultTableModel(
data,
col
));
scrollPane.setViewportView(table);
}
}
CourseManage(课程管理界面):
package com.etc.ui;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.etc.daoimpl.CourseDaoImpl;
import com.etc.entity.Course;
import com.etc.entity.StuCoSc;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CourseManage {
private JFrame frame;
private JTextField cnoinput_que;
private JTextField cnameinput_que;
private JTable table;
private JTextField cnameinput_ins;
private JTextField cnoinput_del;
private JTextField cnameinput_del;
private JTextField cnoinput_upd;
private JTextField cnameinput_upd;
private JScrollPane scrollPane=null;
private CourseDaoImpl cImpl=new CourseDaoImpl();
private JTextField cscoreinput_ins;
private JTextField cscoreinput_del;
private JTextField cscoreinput_upd;
/**
* Launch the application.
*/
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CourseManage window = new CourseManage();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
/**
* Create the application.
*/
public CourseManage() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 455, 488);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setTitle("课程管理界面");
frame.setVisible(true);
JLabel cnolab_que = new JLabel("课程编号:");
cnolab_que.setBounds(22, 24, 63, 15);
frame.getContentPane().add(cnolab_que);
cnoinput_que = new JTextField();
cnoinput_que.setBounds(80, 14, 85, 29);
frame.getContentPane().add(cnoinput_que);
cnoinput_que.setColumns(10);
JLabel cnamelab_que = new JLabel("课程名:");
cnamelab_que.setBounds(186, 24, 54, 15);
frame.getContentPane().add(cnamelab_que);
cnameinput_que = new JTextField();
cnameinput_que.setBounds(233, 14, 85, 29);
frame.getContentPane().add(cnameinput_que);
cnameinput_que.setColumns(10);
JButton quebtn = new JButton("查询");
quebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取课程编号和课程名
int cno=0;
String cname=null;
if (cnoinput_que.getText().trim().length()==0) {
JOptionPane.showMessageDialog(null, "课程编号不能为空");
List<Course> list=cImpl.queryCoAll();
showTableData(list);
} else {
cno=Integer.parseInt(cnoinput_que.getText());
cname=cnameinput_que.getText();
if (cnameinput_que.getText().trim().length()!=0) {
List<Course> list=cImpl.queryCoByCNoName(0,cname);
showTableData(list);
} else {
List<Course> list=cImpl.queryCoByCNoName(cno,cname);
showTableData(list);
}
}
}
});
quebtn.setBounds(351, 20, 73, 23);
frame.getContentPane().add(quebtn);
scrollPane = new JScrollPane();
scrollPane.setBounds(22, 64, 402, 180);
frame.getContentPane().add(scrollPane);
//默认显示所有学生
List<Course> list =cImpl.queryCoAll();
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//选中哪一行
int row =table.getSelectedRow();//从0开始
//获取列的值
String cno=((Integer)table.getValueAt(row, 0)).toString();
String cname=(String)table.getValueAt(row, 1);
String coursescore=Float.toString((float)table.getValueAt(row, 2));
//将获取的值赋值给输入框
cnoinput_del.setText(cno);
cnoinput_upd.setText(cno);
cnameinput_del.setText(cname);
cnameinput_upd.setText(cname);
cscoreinput_del.setText(coursescore);
cscoreinput_upd.setText(coursescore);
}
});
showTableData(list);;
scrollPane.setViewportView(table);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(22, 266, 402, 143);
frame.getContentPane().add(tabbedPane);
JPanel insertpan = new JPanel();
tabbedPane.addTab("添加课程", null, insertpan, null);
insertpan.setLayout(null);
JLabel cnamelab_ins = new JLabel("课程名:");
cnamelab_ins.setBounds(36, 26, 54, 15);
insertpan.add(cnamelab_ins);
cnameinput_ins = new JTextField();
cnameinput_ins.setColumns(10);
cnameinput_ins.setBounds(89, 18, 85, 31);
insertpan.add(cnameinput_ins);
JButton button = new JButton("添加课程");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取输入框的值
String cname=cnameinput_ins.getText();
float courseScore=Float.parseFloat(cscoreinput_ins.getText());
//调用Dao添加课程方法
boolean flag=cImpl.insertCourse(new Course(0, cname, courseScore));
if (flag) {
JOptionPane.showMessageDialog(null, "添加成功");
} else {
JOptionPane.showMessageDialog(null, "添加失败");
}
//调用查询全部
List<Course> list = cImpl.queryCoAll();
showTableData(list);
}
});
button.setBounds(270, 68, 95, 23);
insertpan.add(button);
JLabel cscorelab_ins = new JLabel("课程学分:");
cscorelab_ins.setBounds(210, 26, 63, 15);
insertpan.add(cscorelab_ins);
cscoreinput_ins = new JTextField();
cscoreinput_ins.setBounds(270, 18, 85, 31);
insertpan.add(cscoreinput_ins);
cscoreinput_ins.setColumns(10);
JButton clearbtn = new JButton("清空");
clearbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//将输入框清空
cnameinput_ins.setText("");
cscoreinput_ins.setText("");
}
});
clearbtn.setBounds(183, 68, 77, 23);
insertpan.add(clearbtn);
JPanel deletepan = new JPanel();
tabbedPane.addTab("删除课程", null, deletepan, null);
deletepan.setLayout(null);
JLabel cnolab_del = new JLabel("课程编号:");
cnolab_del.setBounds(26, 24, 59, 15);
deletepan.add(cnolab_del);
cnoinput_del = new JTextField();
cnoinput_del.setColumns(10);
cnoinput_del.setBounds(84, 17, 85, 29);
deletepan.add(cnoinput_del);
JLabel cnamelab_del = new JLabel("课程名:");
cnamelab_del.setBounds(190, 24, 54, 15);
deletepan.add(cnamelab_del);
cnameinput_del = new JTextField();
cnameinput_del.setColumns(10);
cnameinput_del.setBounds(236, 17, 85, 29);
deletepan.add(cnameinput_del);
JButton button_1 = new JButton("删除课程");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取课程编号
int cno=Integer.parseInt(cnoinput_del.getText());
//调用Dao删除课程方法
boolean flag=cImpl.deleteCourse(cno);
if (flag) {
JOptionPane.showMessageDialog(null, "删除成功");
} else {
JOptionPane.showMessageDialog(null, "删除失败");
}
//调用查询全部
List<Course> list = cImpl.queryCoAll();
showTableData(list);
}
});
button_1.setBounds(280, 66, 93, 23);
deletepan.add(button_1);
JLabel cscorelab_del = new JLabel("课程学分:");
cscorelab_del.setBounds(26, 70, 63, 15);
deletepan.add(cscorelab_del);
cscoreinput_del = new JTextField();
cscoreinput_del.setColumns(10);
cscoreinput_del.setBounds(84, 63, 85, 29);
deletepan.add(cscoreinput_del);
JPanel updatepan = new JPanel();
tabbedPane.addTab("修改课程", null, updatepan, null);
updatepan.setLayout(null);
JLabel cnolab_upd = new JLabel("课程编号:");
cnolab_upd.setBounds(28, 24, 62, 15);
updatepan.add(cnolab_upd);
cnoinput_upd = new JTextField();
cnoinput_upd.setColumns(10);
cnoinput_upd.setBounds(85, 18, 85, 28);
updatepan.add(cnoinput_upd);
JLabel cnamelab_upd = new JLabel("课程名:");
cnamelab_upd.setBounds(192, 24, 54, 15);
updatepan.add(cnamelab_upd);
cnameinput_upd = new JTextField();
cnameinput_upd.setColumns(10);
cnameinput_upd.setBounds(235, 18, 85, 28);
updatepan.add(cnameinput_upd);
JButton button_2 = new JButton("修改课程");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取输入框的值
int cno=Integer.parseInt(cnoinput_upd.getText());
String cname=cnameinput_upd.getText();
float courseScore=Float.parseFloat(cscoreinput_upd.getText());
//调用Dao修改课程方法
boolean flag=cImpl.updateCourse(new Course(cno, cname, courseScore));
if (flag) {
JOptionPane.showMessageDialog(null, "修改成功");
} else {
JOptionPane.showMessageDialog(null, "修改失败");
}
//调用查询全部
List<Course> list = cImpl.queryCoAll();
showTableData(list);
}
});
button_2.setBounds(281, 66, 91, 23);
updatepan.add(button_2);
JLabel cscorelab_upd = new JLabel("课程学分:");
cscorelab_upd.setBounds(28, 71, 63, 15);
updatepan.add(cscorelab_upd);
cscoreinput_upd = new JTextField();
cscoreinput_upd.setColumns(10);
cscoreinput_upd.setBounds(85, 64, 85, 28);
updatepan.add(cscoreinput_upd);
JButton button_3 = new JButton("返回上一页");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeaMainFrame teaMainFrame=new TeaMainFrame();
frame.setVisible(false);
}
});
button_3.setBounds(22, 427, 93, 23);
frame.getContentPane().add(button_3);
}
/**
* 对表格进行填充
* @param list
*/
public void showTableData(List<Course> list) {
//列字段
String[] col= {"课程编号","课程名","课程学分"};
//数据
Object[][] data=new Object[list.size()][3];
for (int i = 0; i < list.size(); i++) {
Course course =list.get(i);
data[i][0]=course.getCourseNo();
data[i][1]=course.getCourseName();
data[i][2]=course.getCourseScore();
}
table.setModel(new DefaultTableModel(
data,
col
));
scrollPane.setViewportView(table);
}
}
终于复制粘贴完了,我有点后悔学java了…
开玩笑的…下面是实现界面
时间有限,具体功能不一一实现了…溜了