Java+Swing+MySQL学生成绩管理系统

目录

一、系统介绍

1.运行环境

2.技术说明

3.系统功能

4.数据库ER图表

二、系统实现

1.整体结构 

2.用户注册、登录

3.增加学生

4.模糊查询学生信息

5.成绩管理和查询

6.查询成绩 

7.课程更改

  

 三、部分代码

1.ChangePasswordView.java

2.LoginView.java

3.RegisterView.java

4.GradeView.java

5.备注


一、系统介绍

1.运行环境

开发工具:Eclipse/IDEA

JDK版本:jdk1.8

Mysql版本:5.7

2.技术说明

Java+Swing+Mysql

3.系统功能

学生成绩管理系统是广泛使用的一种信息管理系统,在学生成绩管理系统中,需要能处理各班信息、各班下属学生的个人信息及每个学生学习成绩信息。班级信息包括班级编号、班级名称,学生信息包括学号、姓名、性别、所属班级、联系方式等,及各门课程的成绩。与班级相关的操作是查看已有班级、新增班级、修改班级名称、删除班级、录入新生信息等;与学生相关的操作是查看已有学生、修改学生基本信息、删除学生、录入成绩、修改成绩、根据姓名、学号、班级、课程等多条件查询学生成绩,并可以依据多种条件进行排序。

4.数据库ER图表

二、系统实现

1.整体结构 

2.用户注册、登录

对所有的输入都有进行正则校验,用户名有2-6位中文和6-18位英文或数字限制,且不能输入特殊符号;密码有6-18位英文或数字限制。如下图 

3.增加学生

可以对学生的信息进行修改、学生查询、增加删除。所有输入操作均有防暴力输入和正则判断。

4.模糊查询学生信息

5.成绩管理和查询

6.查询成绩 

 

7.课程更改

 三、部分代码

1.ChangePasswordView.java

package com.Lauguobin.www.view.Login;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.Lauguobin.www.po.User;
import com.Lauguobin.www.service.UserService;
import com.Lauguobin.www.util.ViewUtil;

public class ChangePasswordView extends JDialog
{
	private static final long serialVersionUID = 1L;
	private JPanel all;
	private JPanel buttons;
	
	private JLabel user;
	private JLabel oldPassword;
	private JLabel newPassword;
	
	private JTextField tu;
	private JPasswordField top;
	private JPasswordField tnp;
	
	private JButton yes;
	private JButton no;
	
	ChangePasswordView()
	 {
		 all = new JPanel(new GridLayout(3,1));
		 user = new JLabel("     用户名");
		 oldPassword = new JLabel("     旧密码");
		 newPassword = new JLabel("     新密码");
		 tu = new JTextField(13);
		 top = new JPasswordField(13);
		 tnp = new JPasswordField(13);
		 tnp.addActionListener(new ChangePasswordMonitor());
		 
		 all.add(user);
		 all.add(tu);
		 all.add(oldPassword);
		 all.add(top);
		 all.add(newPassword);
		 all.add(tnp);
		 
		 yes = new JButton("确定修改");
		 yes.addActionListener(new ChangePasswordMonitor());
		 no = new JButton("取消");
		 no.addActionListener(new ChangePasswordMonitor());
		 buttons = new JPanel();
		 buttons.add(yes);
		 buttons.add(no);
		 
		 add(all,"North");
		 add(buttons,"South");
		 
		int screenHeight = ViewUtil.geScreenHeight();
		int screenWidth = ViewUtil.geScreenWidth();
		
		setTitle("密码修改");
		setLocation(screenWidth / 3 , screenHeight  / 2);
		setResizable(false);
		pack();
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setModal(true);
		setVisible(true);
	 }
	 
	 class ChangePasswordMonitor  implements ActionListener
	 {

		@Override
		public void actionPerformed(ActionEvent e)
		{
			//点击确定或者在新密码行敲击回车响应
			if(e.getSource() == yes || e.getSource() == tnp)
			{
				UserService us = new UserService();
				
				//封装数据
				String uname =  tu.getText();
				String pold = new String(top.getPassword());
				String pnew = new String(tnp.getPassword());
				User in = new User(uname,pold);
				
				try
				{
					//调用service判断并由其决定是否执行
					int flag = us.IfAlter(in,pnew);
					switch(flag)
					{
						case 0:
						{
							JOptionPane.showMessageDialog(null, "操作成功", "标题", JOptionPane.PLAIN_MESSAGE);
							dispose();
							break;
						}
						case 1:JOptionPane.showMessageDialog(null, "新密码长度必须位于6 - 18位", "警告", JOptionPane.WARNING_MESSAGE); 	break;
						case 2:JOptionPane.showMessageDialog(null, "原账户信息验证错误", "警告", JOptionPane.WARNING_MESSAGE); 	break;
						default :JOptionPane.showMessageDialog(null, "未知错误", "警告", JOptionPane.ERROR_MESSAGE); 	break;
					}
				}
				catch (Exception e1)
				{
					e1.printStackTrace();
				}
			}
			
			if(e.getSource() == no)
				dispose();
		}
		 
	 }
}

2.LoginView.java

package com.Lauguobin.www.view.Login;

import  java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.Lauguobin.www.po.*;
import com.Lauguobin.www.service.*;
import com.Lauguobin.www.util.*;
import com.Lauguobin.www.view.StudentMenagement.StudentMenageView;
/**
 * 登录界面的类
 * @author GB菌
 */
public class LoginView extends JFrame
{
	private static final long serialVersionUID = 1L;
	//当前登录用户的身份信息
	public static String USING_USER;
	public static String USING_USER_PASSWORD;
	public static boolean USER_IDENTITY;
	//总的Panel
	private JPanel buttonPanel;
	private JLabel picture;
	private JPanel all;
	private ButtonGroup group;
	private JPanel radios;
	private JRadioButton oldDriver;
	private JRadioButton newbie;
	private JPanel loginName;
	private JPanel loginPassword;
	private JLabel labelName;
	private JLabel labelPw;
	private JTextField name;
	private JPasswordField password;
	private JButton login;
	private JButton zc;
	private JButton quit;
	private JButton clean;
	private JButton change;
	
	/**
	 * main方法在这里
	 * @param args
	 */
	public static void main(String[] args) 
	{
		EventQueue.invokeLater
		(
				new Runnable()
				{
					public void run()
					{
						new LoginView();
					}
				}
		);
	}
	//构造方法
	public LoginView()
	{	
		int screenHeight = ViewUtil.geScreenHeight();
		int screenWidth = ViewUtil.geScreenWidth();
		
		//创建按钮并且连接
		login = new JButton("登录");
		login.addActionListener(new LoginMonitor());
		zc = new JButton("注册");
		zc.addActionListener(new LoginMonitor());
		quit = new JButton("退出");
		quit.addActionListener(new LoginMonitor());
		clean = new JButton("清除信息");
		clean.addActionListener(new LoginMonitor());
		change = new JButton("修改密码");
		change.addActionListener(new LoginMonitor());
		
		//实例化容器
		buttonPanel = new JPanel();
		loginName = new JPanel();
		loginPassword = new JPanel();
		all = new JPanel(new GridLayout(3,1));
		picture  = new JLabel(new ImageIcon("picture.jpg"));
		group =  new ButtonGroup();
		radios = new JPanel();
		labelName = new JLabel("      账号");
		name = new JTextField(13);
		name.addActionListener(new LoginMonitor());
		labelPw = new JLabel("      密码");
		password = new JPasswordField(13);
		password.addActionListener(new LoginMonitor());
		oldDriver = new JRadioButton("管理员");
		newbie = new JRadioButton("用户");
		
		loginName.add(labelName);
		loginName.add(name);
		loginName.add(clean);
		loginPassword.add(labelPw);
		loginPassword.add(password);
		loginPassword.add(change);
		group.add(oldDriver);
		group.add(newbie);
		radios.add(oldDriver);
		oldDriver.addActionListener(new LoginMonitor());
		radios.add(newbie);
		newbie.addActionListener(new LoginMonitor());
		all.add(loginName);
		all.add(loginPassword);
		all.add(radios);
		buttonPanel.add(login);
		buttonPanel.add(zc);
		buttonPanel.add(quit);
		
		add(picture,"North");
		add(all);
		add(buttonPanel , "South");
		
		setTitle("学生成绩管理系统");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setLocation(screenWidth  / 3 , screenHeight  / 5);
		setResizable(false);
		setVisible(true);
		
		pack();
	}
	
	/**
	 * 方便事件响应管理,把这个类设置成内部类
	 * @author GB菌
	 *
	 */
	class LoginMonitor  implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			 UserService us = new  UserService();
			 
			 //点击登录或在密码文本域中敲击回车
			if((e.getSource() == login)||(e.getSource() == password))
			{
				if(oldDriver.isSelected())
					USER_IDENTITY = true;
				else	if(newbie.isSelected())
					USER_IDENTITY = false;
				else 
				{
					JOptionPane.showMessageDialog(null, "请选择用户类型", "输入错误", JOptionPane.ERROR_MESSAGE);
					return ;
				}
				
				String n = name.getText(); 
				String p = new String(password.getPassword());

				password.setText("");
				User user = new User(n,p,USER_IDENTITY);
				try
				{
					if(us.IfHaveUser(user))
					{
						dispose();
						USING_USER = n;
						USING_USER_PASSWORD = p;
						new StudentMenageView();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "用户名、密码或用户身份选择错误", "输入错误", JOptionPane.ERROR_MESSAGE);
					}
				}
				catch (Exception e1)
				{
					e1.printStackTrace();
				}
			}
			
			//注册事件
			if(e.getSource() == zc)
			{
				new RegisterView();
			}
			
			//清除信息事件
			if(e.getSource() == clean)
			{
				name.setText("");
				password.setText("");
			}
			
			//更改密码事件
			if(e.getSource() == change)
			{
				new ChangePasswordView ();
			}
			
			//退出事件
			if(e.getSource() == quit)
			{
				int n = JOptionPane.showConfirmDialog(null, "是否退出?", "退出",JOptionPane.YES_NO_OPTION);
				if(n==0)
					System.exit(0);
			}
		}
	}
	
}

3.RegisterView.java

package com.Lauguobin.www.view.Login;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.Lauguobin.www.po.User;
import com.Lauguobin.www.service.UserService;
import com.Lauguobin.www.util.ViewUtil;

/** 这个窗口只有在登录界面才会用到
 * 这个类用于构建注册窗口。并没有太大卵用。
 * @author GB菌
 *
 */
public class RegisterView extends JDialog
{

	private static final long serialVersionUID = 1L;
	private JPanel all;
	private JPanel buttons;
	private JPanel tips;
	private JPanel one;
	private JPanel two;
	private JPanel three;
	
	private ButtonGroup group;
	private JPanel radios;
	
	private JLabel user;
	private JLabel password;
	private JLabel affirmPassword;
	private JLabel tip1;
	private JLabel tip2;
	private JLabel tip3;
	
	private JTextField tu;
	private JPasswordField tp;
	private JPasswordField atp;
	
	private JButton yes;
	private JButton no;
	private JRadioButton oldDriver;
	private JRadioButton newbie;
	
	 RegisterView()
	 {
		all = new JPanel(new GridLayout(4,1));
		tips = new JPanel(new GridLayout(3,0));
		group =  new ButtonGroup();
		radios = new JPanel();
		one = new JPanel();
		two = new JPanel();
		three = new JPanel();
		 user = new JLabel("用户名称");
		 password = new JLabel("用户密码");
		 affirmPassword = new JLabel("确认密码");
		 tip1 = new JLabel("	 温馨提示:用户名只允许有字母、中文、数字  ");
		 tip2 = new JLabel("      用户名允许的长度为4 - 20位  ");
		 tip3 = new JLabel("      密码长度必须位于6 - 18位");
		tu = new JTextField(10);
		tp = new JPasswordField(10);
		tp.addActionListener(new RegisterMonitor());
		atp= new JPasswordField(10);
		atp.addActionListener(new RegisterMonitor());
		oldDriver = new JRadioButton("管理员");
		newbie = new JRadioButton("用户");
		
		group.add(oldDriver);
		group.add(newbie);
		radios.add(oldDriver);
		oldDriver.addActionListener(new RegisterMonitor());
		radios.add(newbie);
		newbie.addActionListener(new RegisterMonitor());
		one.add(user);
		one.add(tu);
		two.add(password);
		two.add(tp);
		three.add(affirmPassword);
		three.add(atp);
		all.add(one);
		all.add(two);
		all.add(three);
		all.add(radios);
		tips.add(tip1);
		tips.add(tip2);
		tips.add(tip3);
		 
		 yes = new JButton("注册");
		 yes.addActionListener(new RegisterMonitor());
		 no = new JButton("取消");
		 no.addActionListener(new RegisterMonitor());
		 buttons = new JPanel();
		 buttons.add(yes);
		 buttons.add(no);
		 
		 add(all,"North");
		 add(tips);
		 add(buttons,"South");
		 
		int screenHeight = ViewUtil.geScreenHeight();
		int screenWidth = ViewUtil.geScreenWidth();
		
		setTitle("新手上路");
		setLocation(screenWidth / 3 , screenHeight  / 2);
		setResizable(false);
		pack();
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setModal(true);
		setVisible(true);

	 }
	 
	 class RegisterMonitor  implements ActionListener
	 {

		@Override
		public void actionPerformed(ActionEvent e)
		{
			UserService us = new UserService();
			
			//点击确定或者在密码文本域敲击回车
			if(e.getSource() == yes || e.getSource() == tp)
			{
				String u = tu.getText();
				String p = new String(tp.getPassword());
				String p2 = new String(atp.getPassword());
				if(!p.equals(p2))
				{	
					JOptionPane.showMessageDialog(null, "两次密码输入不一致!", "输入错误", JOptionPane.ERROR_MESSAGE);
					tp.setText("");
					atp.setText("");
					return;
				}
				boolean identity;
				if(oldDriver.isSelected())
					identity = true;
				else	if(newbie.isSelected())
					identity = false;
				else 
				{
					JOptionPane.showMessageDialog(null, "请选择用户类型", "输入错误", JOptionPane.ERROR_MESSAGE);
					return ;
				}
				try
				{
					//把数据封装成对象操作
					User user = new User(u,p,identity);
					//调用service判断并由service决定是否注册
					int flag = us.IfAddUserSuccess(user);
					switch(flag)
					{
						case 0:
						{
							JOptionPane.showMessageDialog(null, "注册成功", "信息", JOptionPane.PLAIN_MESSAGE);
							dispose();
							break;
						}
						case 1:JOptionPane.showMessageDialog(null, "密码长度必须在6 - 18位", "警告", JOptionPane.WARNING_MESSAGE);break;
						case 2:JOptionPane.showMessageDialog(null, "用户名不合法", "警告", JOptionPane.WARNING_MESSAGE);break;
						case 3:JOptionPane.showMessageDialog(null, "用户已存在", "警告", JOptionPane.WARNING_MESSAGE);break;
						default :JOptionPane.showMessageDialog(null, "未知错误", "错误", JOptionPane.WARNING_MESSAGE);break;
					}
				}
				catch (Exception e1)
				{
					e1.printStackTrace();
				}
			}
			
			//点击取消
			if(e.getSource() == no)
				dispose();
		}
		 
	 }
}

4.GradeView.java

package com.Lauguobin.www.view.ScoreMenagement;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.Lauguobin.www.service.*;
import com.Lauguobin.www.util.ViewUtil;
import com.Lauguobin.www.view.Login.LoginView;

/**
 * 成绩管理窗口,需要一个id来确定是谁的成绩
 * @author GB菌
 *
 */
public class GradeView extends JDialog
{
	private static final long serialVersionUID = 1L;
	private JScrollPane list;
	private JTable tab;
	private JPanel inButton;
	private JPanel search;
	private JTextField text;
	private JButton s;
	private JLabel label;
	private JButton yes;
	private JButton change;
	private JButton f5;
	int id;
	
	public GradeView(int id)
	{
		this.id = id;
		
		yes = new JButton("返回");
		yes.addActionListener(new GVMonitor());
		f5 = new JButton("刷新");
		f5.addActionListener(new GVMonitor());
		change = new JButton("修改成绩");
		change.addActionListener(new GVMonitor());
		s = new JButton("搜索");
		s.addActionListener(new GVMonitor());
		text = new JTextField(15);
		text.addActionListener(new GVMonitor());
		list = new JScrollPane();
		label = new JLabel("搜索成绩<'%'和'_'模糊>");
		inButton = new JPanel();
		search = new JPanel();
		inButton.add(change);
		inButton.add(f5);
		inButton.add(yes);
		search.add(label);
		search.add(text);
		search.add(s);
		
		list = new JScrollPane();
		ScoreService ss = new ScoreService();
		Object[][] nt = ss.getAllScore(id);
		String[] title = {"学生ID","学生姓名","课程号","课程名称","学科成绩","获得学分"};
		DefaultTableModel model = new DefaultTableModel(nt, title) 
		{ 
			private static final long serialVersionUID = 1L;

				//此处设置单元格时否可以被编辑。
			  public boolean isCellEditable(int row, int column) 
			  { 
			    return false; 
			  } 
		}; 
		tab = new JTable(model);
		tab.getTableHeader();
		ViewUtil.fitTableColumns(tab);
		list.getViewport().add(tab); 
		
		add(search,"North");
		add(list);
		add(inButton,"South");
		
		int screenHeight = ViewUtil.geScreenHeight();
		int screenWidth = ViewUtil.geScreenWidth();
		
		setTitle("查看成绩");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(screenWidth / 3 , screenHeight / 4 , screenWidth / 3 , screenHeight  / 3);
		setModal(true);
		setResizable(false);
		setVisible(true);
	}
	
	class GVMonitor  implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			/*
			 * 返回事件
			 */
			if(e.getSource() == yes)
				dispose();
			
			/*
			 * 搜索事件
			 */
			if(e.getSource() == text || e.getSource() == s)
			{
				String str = text.getText();
				if(str.equals(""))
				{
					JOptionPane.showMessageDialog(null, "请输入搜索条件", "警告", JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					//显示搜索结果的列表
					Object info[][] = new ScoreService().getSearch(str,id);
					 String[] title = {"学生ID","学生姓名","课程号","课程名称","学科成绩","获得学分"};
					DefaultTableModel model = new DefaultTableModel(info,title) 
					{ 
						private static final long serialVersionUID = 1L;

							//此处设置单元格时否可以被编辑。
						  public boolean isCellEditable(int row, int column) 
						  { 
						    return false; 
						  } 
					}; 
					tab.setModel(model);
					tab.getTableHeader();
					ViewUtil.fitTableColumns(tab);
					list.getViewport().add(tab); 
				}
			}
			
			/*
			 * 更改成绩事件
			 */
			if(e.getSource() == change)
			{
				if(LoginView.USER_IDENTITY)
				{
					int selectedRowIndex = tab.getSelectedRow(); 
					if(selectedRowIndex >= 0)
					{
						//调用service
						new SetScoreView(selectedRowIndex,tab,list);
					}
					else
					{
						JOptionPane.showMessageDialog(null, "请选中一行", "警告", JOptionPane.INFORMATION_MESSAGE);
					}
				}
				else
					JOptionPane.showMessageDialog(null, "你根本就不是老司机!", "警告", JOptionPane.WARNING_MESSAGE);
			}
			
			if(e.getSource() == f5)
			{
				ScoreService ss = new ScoreService();
				Object[][] nt = ss.getAllScore(id);
				String[] title = {"学生ID","学生姓名","课程号","课程名称","学科成绩","获得学分"};
				DefaultTableModel model = new DefaultTableModel(nt,title) 
				{ 
					private static final long serialVersionUID = 1L;

						//此处设置单元格时否可以被编辑。
					  public boolean isCellEditable(int row, int column) 
					  { 
					    return false; 
					  } 
				}; 
				tab.setModel(model);
				tab.getTableHeader();
				ViewUtil.fitTableColumns(tab);
				list.getViewport().add(tab);
			}
		}
	}
}

5.备注

如有侵权请联系我删除 



 

猜你喜欢

转载自blog.csdn.net/qq_61827376/article/details/125369457