Java课程设计【学生信息管理系统】

一、问题描述

如何实现一个功能简单的学生信息管理系统,能够对学生信息(包括照片)进行添加、删除、修改和查询等操作。

二、基本要求

实现一个功能简单的学生信息管理系统,该系统具有按照账户名密码登录功能,登录后,可以添加,删除,修改、查询(显示学生相片)学生信息,添加学生信息时,要求能添加学生的相片信息(实现相片文件的上传和下载功能)。

三、需求分析

程序设计的任务是实现对学生信息的管理。用户名和密码都默认设置为0,用户名或密码输入错误会弹出“用户名或密码输入不正确”的对话框。在用户名和密码输入正确后进入学生信息管理系统,然后进行添加、修改、删除等操作。在添加操作里面可以上传和下载照片,这是File类型的。输入的其他学号、姓名、性别、电话、QQ和专业都是String类型,输出的也是String类型。点击确认后会弹出“添加成功”。

四、概要设计

1、类之间的调用关系

在这里插入图片描述

2、学生信息模块

在这里插入图片描述

3、管理系统模块

在这里插入图片描述

4、详细设计

①主程序LoginGUI的代码

public class LoginGUI{
	private JFrame jf;
	//水平box
	private Box center=Box.createVerticalBox();
	//学号的JPanel
	private JPanel idPanel=new JPanel();
	//密码的JPanel
	private JPanel passwordPanel=new JPanel();
	private JLabel lUserId=new JLabel("用户名");
//设置用户名的文本框
	private JTextField tUserId=new JTextField(15);
	private JLabel lPassword=new JLabel("密   码");
//设置密码的文本框
	private JPasswordField tPassword=new JPasswordField(15);
	//按钮的JPanel
	private JPanel buttonPanel=new JPanel();
	private JButton bLogin=new JButton("登录");
	private JButton bCancel=new JButton("取消");
	//设置运行时窗口的大小
	Dimension faceSize=new Dimension(350,150);
	//获得屏幕的大小
	Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
	public void init(){
		jf=new JFrame("学生信息管理系统");
		//设置JFrame的名称
		jf.setTitle("登录");
		//将lUserId,tUserId放在idPanel中,idPanel默认水平放置
		idPanel.add(lUserId);
		idPanel.add(tUserId);
		passwordPanel.add(lPassword);
		passwordPanel.add(tPassword);
		center.add(idPanel);
		center.add(passwordPanel);
		buttonPanel.add(bLogin);
		buttonPanel.add(bCancel);
		//登录按钮的监听器
		bLogin.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String userId=tUserId.getText();
				String password=String.valueOf(tPassword.getPassword());
				//开启接受数据的线程
				if(userId.trim().equals("")||userId==null||password.trim().equals("")||password==null){
					JOptionPane.showMessageDialog(jf,"用户名或密码不能为空!","提示",JOptionPane.WARNING_MESSAGE);
				}else{
					if(userId.equals("0")&&password.equals("0")){
						new StudentManageView().init();
					}else{
						loginFailure();
					}
				}
			}
			
		});
		//取消按钮的监听器
		bCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
			}
		});
		center.add(buttonPanel);
		jf.add(center);
		jf.pack();
		//设置JFame运行时的大小
		jf.setSize(faceSize);
		//设置JFame运行时的位置
		jf.setLocation((int)(screenSize.width-faceSize.getWidth())/2,(int)(screenSize.height-faceSize.getHeight())/2);
		//设置JFrame不可最大化
		jf.setResizable(false);
		//设置JFrame单机X时结束程序
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置JFrame可见
		jf.setVisible(true);
	}
	public void loginFailure(){
		JOptionPane.showMessageDialog(jf, "用户名或密码输入不正确!","提示",JOptionPane.WARNING_MESSAGE);
	}
	public static void main(String args[])throws Exception{
		new LoginGUI().init();
	}
}

②程序View的代码

public class View {
private JFrame jf=new JFrame();
//页面的总JPanel
private JPanel total=new JPanel(new BorderLayout());
//上传
private JPanel pUpload=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel lFileName=new JLabel("请选择上传的照片");
private JTextField tFileName=new JTextField(15);
private JButton bBrowse=new JButton("浏   览");
private JFileChooser uploadChooser=new JFileChooser();
private JButton bUpload=new JButton("上  传");
private JPanel pDownload=new JPanel(new FlowLayout(FlowLayout.LEFT));
private JLabel lDownload=new JLabel("下载下面的图片:");
private JFileChooser downloadChooser=new JFileChooser();
private JButton bDownload=new JButton("下  载");
private JPanel pIcon=new JPanel(new FlowLayout(FlowLayout.LEFT));
//使用本地图片文件作为图标
private ImageIcon icon=new ImageIcon(new ImageIcon("D:/Saved Pictures/009.jpg").getImage().getScaledInstance(400,320,0));
private JLabel lIcon=new JLabel(icon);
Dimension faceSize=new Dimension(500,450);
//设置运行时窗口的位置
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
public void use(){
	pUpload.add(lFileName);
	pUpload.add(tFileName);
	pUpload.add(bBrowse);
	pUpload.add(bUpload);
	//浏览按钮的监听器
	bBrowse.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			downloadChooser.setCurrentDirectory(new File("."));
			int result=downloadChooser.showOpenDialog(jf);
			if(result==JFileChooser.APPROVE_OPTION ){
				String path=downloadChooser.getSelectedFile().getPath();
				tFileName.setText(path);
			}
		}
	});
	//上传按钮的监听器
	bUpload.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			String fromFileName=tFileName.getText();
			String toFileName="D:/Config/"+System.currentTimeMillis()+".jpg";
			write(fromFileName,toFileName);
			JOptionPane.showMessageDialog(jf, "上传成功!","提示",JOptionPane.WARNING_MESSAGE );
		}
	});
	pDownload.add(lDownload);
	pDownload.add(bDownload);
	//下载按钮的监听器
	bDownload.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			downloadChooser.setCurrentDirectory(new File("."));
			int result=uploadChooser.showOpenDialog(jf);
			if(result==JFileChooser.APPROVE_OPTION ){
			String path=uploadChooser.getSelectedFile().getPath();
			String fromFileName="D:/Saved Pictures/009.jpg";
			write(fromFileName,path);
			JOptionPane.showMessageDialog(jf, "下载成功!","提示",JOptionPane.WARNING_MESSAGE );
		}
	  }
	});
//放置按钮的位置
    pIcon.add(lIcon);
	total.add(pUpload,BorderLayout.NORTH);
	total.add(pDownload,BorderLayout.CENTER);
	total.add(pIcon,BorderLayout.SOUTH);
	jf.add(total);
	jf.setSize(faceSize);
	jf.setLocation((int)(screenSize.width-faceSize.getWidth())/2,(int)(screenSize.height-faceSize.getHeight())/2);
	jf.setResizable(false);
	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
	jf.setVisible(true);
}
//fromFile是源文件,toFile是目的文件
public void write(String fromFile,String toFile){
	FileInputStream fis=null;
	FileOutputStream fos=null;
//用try和catch捕获异常
	try{
		fis=new FileInputStream(fromFile);
		fos=new FileOutputStream(toFile);
		byte[] buf=new byte[1024];
		int hasRead=0;
		while((hasRead=fis.read(buf))>0){
			fos.write(buf,0,hasRead);
		}
	}catch(FileNotFoundException e){
		e.printStackTrace();
	}catch(IOException e){
		e.printStackTrace();  
	}finally{
		try{
			fis.close();
			fos.close();
		}catch(IOException e){
			e.printStackTrace();  
		}
	}
}
public static void main(String args[]){
//用View的引用调用use方法
	new View().use();
}
}

③程序Student的代码

package 学生信息管理系统;
class User{
	private String userId;
	private String password;
	public String getUserId(){
		return userId;
	}
	public void setUserId(String userId){
		this.userId=userId;
	}
	public String getPassword(){
		return password;
	}
	public void setPassword(String password){
		this.password=password;
	}
}
public class Student extends User{
 private String id;
 private String name;
 private String sex;
 private String age;
 private String phone;
 private String qq;
 private String major;
 private Object photo;
 public Object getPhoto(){
	 return photo;
 }
 public void setPhoto(Object photo){
	 this.photo=photo;
 }
 public String getId(){
	 return id;
 }
 public void setId(String id){
	 this.id=id;
 }
 public String getName(){
	 return name;
 }
 public void setName(String name){
	 this.name=name;
 }
 public String getSex(){
	 return sex;
 }
 public void setSex(String sex){
	 this.sex=sex;
 }
 public String getAge(){
	 return age;
 }
 public void setAge(String age){
	 this.age=age;
 }
 public String getPhone(){
	 return phone;
 }
 public void setPhone(String phone){
	 this.phone=phone;
 }
 public String getQq(){
	 return qq;
 }
 public void setQq(String qq){
	 this.qq=qq;
 }
 public String getMajor(){
	 return major;
 }
 public void setMajor(String major){
	 this.major=major;
 }
}

④程序ConnectSQLServer的代码

package 学生信息管理系统;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

 class DatebaseConnection {
	//数据库连接成功
	public final String DBDRIVER="net.sourceforge.jtds.jdbc.Driver";
	//数据库连接的URL
	public  final String DBURL="jdbc:jtds:sqlserver://127.0.0.1:1433/Competition";
	//数据库登录名
	public final String DBUSER="sa";
	//数据库登录密码
	public final String DBPASS="Ambow99999999";
	private Connection conn=null;
	public DatebaseConnection(){
		try{
			//加载数据库驱动
			Class.forName(DBDRIVER);
			//获取数据库连接
			conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
		}catch(SQLException e){
			JOptionPane.showMessageDialog(null,"数据库连接失败","异常",JOptionPane.ERROR_MESSAGE );
			System.exit(0);
		}catch(ClassNotFoundException e){
			JOptionPane.showMessageDialog(null, "驱动加载失败","异常",JOptionPane.ERROR_MESSAGE );
			System.exit(0);
		}
	}
	public Connection getConnection(){
		return this.conn;
	}
	public void close(){
		//关闭数据库连接
		if(this.conn!=null){
			try{
				this.conn.close();
			}catch(SQLException e){}
		}
	}
 }
	public class ConnectSQLServer{
	public static void main(String[] args) {
		try{
			Connection con=new DatebaseConnection().getConnection();
			if(con!=null){
				JOptionPane.showMessageDialog(null,"数据库连接成功","祝贺",JOptionPane.INFORMATION_MESSAGE );
				System.exit(0);
			}else{
				JOptionPane.showMessageDialog(null, "数据库连接失败","错误",JOptionPane.ERROR_MESSAGE );
				System.exit(0);
			}
			con.close();
		}catch(SQLException e){
			e.printStackTrace();  
			}

	}
	}

五、调试分析

在一开始调试的时候,发现虽然没有错误,但无法运行,找了很久发现是自己在main方法里没有去调用init()方法,然后填上了之后程序可以运行。然后在成功登录后进入学生信息管理系统界面。在里面没有上传照片和下载图片这一选项。我在View这个程序里面首先添加了本地的图片作为图标可供下载,然后选定一个文件夹Config作为上传的路径。这样,我就在LoginGUI类里面的添加按钮监听器里面再增加了调用View类的use()方法的监听器,实现了图片上传和下载的功能。总之,从一开始参考书上例题打出来的程序作为基石,在上面进行雕刻。从设计转化为实现,打基础这一步很困难,因为要花很长时间查资料、看书和看代码来理解程序,然后才能自己灵活进行优化。同学之间也相互讨论帮助,都能给出自己的想法,然后交流之后会得出更好的创意,从第一个星期开始,我已经规划好要怎样做,许多同学也参考了我的规划。我一直按照计划实施并且很顺利地完成了程序设计。

六、用户使用说明

1、登录

(1)程序设计的任务是先设计出一个登录窗口,输入学号和密码,如果输入错误,会像如图所示输出“用户名或密码输入不正确”的对话框:
在这里插入图片描述
(2)如果正确,则成功登录,进入学生信息管理界面,如图所示:
在这里插入图片描述

2、添加

(1)点击“添加”按钮,进入学生信息添加界面:
在这里插入图片描述
(2)在以上界面输入要添加的学生信息,首先点击“上传照片”按钮,然后弹出如下窗口:
在这里插入图片描述
(3)点击浏览选项,选择本地文件里需要上传的图片,这里我们选择小猫图片,然后点击上传,会显示“上传成功”的对话框。
在这里插入图片描述
(4)即可上传到本地D:/Config这个文件夹内,如下图所示的小猫图片。
在这里插入图片描述
(5)也可以点击“下载”这个按钮,会提示下载成功的对话框,如图所示:
在这里插入图片描述
(6)它会将演示的图标下载到你指定的文件夹中,都是实现了文件的复制功能,如下图所示是将图标下载到E:/QQ浏览器文件这个文件夹里。
在这里插入图片描述
(7)然后添加其他的学生信息,如图所示:
在这里插入图片描述
(8)点击“确认”按钮,即可保存学生信息。我们再添加一个女学生的信息,如下图所示:
在这里插入图片描述
(9)然后单击“确定”按钮,此时学生信息添加成功。

3、查询

(1)在学生管理界面的学生信息列表中点击“查询”按钮,即可显示已经添加的学生信息记录,如图所示:
在这里插入图片描述
(2)也可以在“查询”的文本框内输入学号,然后点击“查询”按钮,会跳出所对应的学生记录,如图所示:
在这里插入图片描述

4、修改

(1)点击所选的武则天学生记录,再点击“修改”按钮,弹出信息框,修改姓名为“花木兰”,修改手机号为“18816218888”,如图所示:
在这里插入图片描述
(2)点击确认按钮,再点击“查询”按钮进行刷新,显示出修改后的信息,如下图所示:
在这里插入图片描述

5、删除

(1)在学生信息管理系统界面选中某一学生记录,单击“删除”按钮,弹出删除确认界面。如果确认删除,单击“确定”按钮,否则单击“取消”按钮。
在这里插入图片描述
(2)我们选择学生凯的记录,并点击“删除”按钮,再确定删除,会看到这条记录被删除,仅剩学生花木兰的记录,如图:
在这里插入图片描述

6、退出

点击右上角的X,将关闭所有程序窗口。

七、测试结果

测试数据和测试结果在用户使用说明选项中已经详细介绍过,这里不再重复介绍。

八、课程设计总结

这次课程设计总体来说是一次非常有意义的任务,因为在这次课程设计中我学会了很多GUI编程和流类的知识,提高了编程的能力,也增加了对编程的兴趣。虽然这是一个小项目,但是能把它做好也是有很大的满足感。虽然一开始遇到很多问题,但自己都咬牙克服、迎难而上,每天都在钻研程序,然后将自己的思想与同学们交流。可以说,没有付出就没有回报,只要你肯付出,就会有收获。一件事,你只要用心去做了,将它做好,无论结果如何,你都不会留有遗憾的。课程设计让我对所学知识有了更深刻的理解,也让我明白如今对程序员的要求是多么严格,需要掌握各种编程知识,才能够在职场上游刃有余。

九、参考文献

《JAVA核心技术》 马志强 张然 李雷孝著
《JAVA API文档》 Oracle官网文件
《JAVA编程思想》 【美】Bruce Eckel著
《JAVA数据库技术详解》 李刚 著

等你有了新的圈子,别忘了谁陪你走过了人烟稀少的时候;等你过得好时,别忘了谁陪你度过了最艰难的时刻。路上人山人海,不一定都对你好,但肯定会有一个愿意等。朋友不要多,但要最真。你可以不好,但不能背叛;可以不是土豪,但会懂得分享。可以没有势力,但知道护友。最后我们都散了,记得常联系。

猜你喜欢

转载自blog.csdn.net/qq_42257666/article/details/106470477