Java+gui+mysql实现学生管理系统

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);
				St
录入=new JButton("录入"); 查询=new JButton("查询");删除=new JButton("删除"); 修改=new JButton("修改");显示=new JButton("显示");录入.addActionListener(new InputAct());查询.addActionListener(new InquestAct()); 修改.addActionListener(new ModifyAct());删除.addActionListener(new DeleteAct());显示.addActionListener(new ShowAct()); 修改.setEnabled(false); p1=new JPanel(); p1.add(new JLabel("学号:",JLabel.CENTER)); p1.add(学号); p2=new JPanel(); p2.add(new JLabel("姓名:",JLabel.CENTER)); p2.add(姓名); p3=new JPanel(); p3.add(new JLabel("性别:",JLabel.CENTER)); p3.add(男); p3.add(女); p4=new JPanel(); p4.add(new JLabel("专业:",JLabel.CENTER)); p4.add(专业); p5=new JPanel(); p5.add(new JLabel("年级:",JLabel.CENTER)); p5.add(年级); p6=new JPanel(); p6.add(new JLabel("出生:",JLabel.CENTER)); p6.add(出生); pv=new JPanel(); pv.setLayout(new GridLayout(6,1)); pv.add(p1); pv.add(p2); pv.add(p3); pv.add(p4); pv.add(p5); pv.add(p6); ph=new JPanel(); ph.add(录入); ph.add(查询); ph.add(修改); ph.add(删除); ph.add(显示); file=new File("学生信息.txt"); 学生散列表=new Hashtable(); if(!file.exists()){ try{ FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(学生散列表); objectOut.close(); out.close(); } catch(IOException e){} } Container con=getContentPane(); con.setLayout(new BorderLayout()); con.add(lb, BorderLayout.WEST); con.add(pv, BorderLayout.CENTER); con.add(ph, BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setBounds(100,100,600,300); setVisible(true); Statement stmt; ResultSet rs; } public static void main(String[] args) {StudentManager ff=new StudentManager();} //添加学生信息,的Mysql操作 public class InputAct implements ActionListener{ public void actionPerformed(ActionEvent e){ 修改.setEnabled(false); String number=""; number=学号.getText(); if(number.length()>0){ if(学生散列表.containsKey(number)){ String warning="该生信息已存在,请到修改页面修改!"; JOptionPane.showMessageDialog(null,warning,"警告", JOptionPane.WARNING_MESSAGE); }//end if1 else{ String m="该生信息将被录入!"; int ok=JOptionPane.showConfirmDialog(null,m,"确认", JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE); if(ok==JOptionPane.YES_OPTION) { try { Class.forName("org.gjt.mm.mysql.Driver"); } catch(java.lang.ClassNotFoundException e11) { System.out.println("Class not found exception occir.Message is:"); System.out.println(e11.getMessage()); } try { PreparedStatement prepstmt1; Statement stmt; ResultSet rs; String url = "jdbc:mysql://localhost/test?user=root&password=0&useUnicode=true&&characterEncoding=8859_1"; String user="root"; String password="0"; Connection conn=null; try { conn=DriverManager.getConnection(url,user,password); } catch(Exception ex) { ex.printStackTrace(); } //对制定的数据库添加信息 String sql1="INSERT INTO students(student_id,name,sex,job,data,grade)"+"VALUES(?,?,?,?,?,?)"; prepstmt1=(PreparedStatement)conn.prepareStatement(sql1); prepstmt1.setString(1,学号.getText()); prepstmt1.setString(2,姓名.getText()); if(男.isSelected()){prepstmt1.setString(3,"男.getText()");} else{prepstmt1.setString(3,女.getText());} prepstmt1.setString(4,专业.getText()); prepstmt1.setString(5,年级.getText()); prepstmt1.setString(6,出生.getText()); prepstmt1.executeUpdate(); } catch(SQLException e1) { System.out.println("SQL Exception occur.Message is:"); System.out.println(e1.getMessage()); } } else{ String warning="必须输入学号!"; JOptionPane.showMessageDialog(null,warning, "警告",JOptionPane.WARNING_MESSAGE); }//end else0 } } else{ String warning="必须输入学号!"; JOptionPane.showMessageDialog(null,warning, "警告",JOptionPane.WARNING_MESSAGE); } } }
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值