Java课程设计学生信息管理系统】(基于java的学生信息管理系统课程设计)

网友投稿 991 2022-05-30

@TOC

一、问题描述

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

二、基本要求

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

三、需求分析

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

四、概要设计

1、类之间的调用关系

2、学生信息模块

3、系统管理模块

4、详细设计

①主程序LoginGUI的代码

主要实现了系统的登录窗口和登录之后进入的信息管理窗口,还有增删改查功能。

package 学生信息管理系统; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; 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.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; 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(); } } class MyJTable extends JTable{ /** * */ private static final long serialVersionUID = -3083638370004874364L; public MyJTable(TableModel dm){ super(dm); } //设置表格不可编辑 public boolean isCellEditable(int rowIndex,int columnIndex){ return false; } } class StudentService{ private Student[] students=new Student[50]; //添加学生信息 public void insert(Student s){ for(int i=0;i=0;i--){ service.delete((String)tableModel.getValueAt(selected[i], 0)); tableModel.removeRow(selected[i]); } } } } }); //修改按钮的监听器 update.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ int row=table.getSelectedRow(); //如果要进行修改,就将id=要修改的学号 id=String.valueOf(table.getValueAt(row, 0)); //设置tId的内容 tId.setText(id); //设置tId不可修改 tId.setEditable(false); tName.setText(String.valueOf(table.getValueAt(row, 1))); String sex=(String) table.getValueAt(row, 2); //如果性别是"男",则将单选框中的男选中,否则选中女 if(sex.equals("男")){ bSex.setSelected(boy.getModel(),true); }else{ bSex.setSelected(girl.getModel(),true); } tAge.setText(String.valueOf(table.getValueAt(row, 3))); tPhone.setText(String.valueOf(table.getValueAt(row, 4))); tQq.setText(String.valueOf(table.getValueAt(row, 5))); tMajor.setText(String.valueOf(table.getValueAt(row, 6))); //设置dialog可见 dialog.setVisible(true); } }); jf.setLayout(new BorderLayout()); //设置pSelect在jf的北面 jf.add(pSelect,BorderLayout.NORTH); //设置pSelect在jf的中心 jf.add(tableScrollPane,BorderLayout.CENTER ); //设置pSelelct在jf的南面 jf.add(buttonPanel,BorderLayout.SOUTH); jf.pack(); 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); pPhoto.add(lPhoto); pPhoto.add(tPhoto); pPhoto.add(upload); pId.add(lId); pId.add(tId); pName.add(lName); pName.add(tName); pSex.add(lSex); bSex.add(boy); bSex.add(girl); pSex.add(boy); pSex.add(girl); pAge.add(lAge); pAge.add(tAge); pPhone.add(lPhone); pPhone.add(tPhone); pQq.add(lQq); pQq.add(tQq); pMajor.add(lMajor); pMajor.add(tMajor); pButton.add(confirm); pButton.add(cancel); //确定按钮的监听器 confirm.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Student student=new Student(); student.setUserId(tId.getText()); student.setPassword(tId.getText()); student.setId(tId.getText()); student.setName(tName.getText()); String sex=null; if(boy.isSelected()){ sex="男"; } if(girl.isSelected()){ sex="女"; } student.setSex(sex); student.setAge(tAge.getText()); student.setPhone(tPhone.getText()); student.setQq(tQq.getText()); student.setMajor(tMajor.getText()); if(id!=null){ service.update(student); }else{ service.insert(student); } dialog.dispose(); } }); //取消按钮的监听器 cancel.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dialog.dispose(); } }); box.add(pPhoto); box.add(pId); box.add(pName); box.add(pPhone); box.add(pSex); box.add(pAge); box.add(pPhone); box.add(pQq); box.add(pMajor); box.add(pButton); box.add(pButton); dialog.add(box); dialog.setBounds((int)(screenSize.width-280)/2,(int)(screenSize.height-300)/2,280,350); } public void insertTable(Student student){ if(student!=null){ String[]newCell=new String[7]; newCell[0]=student.getId(); newCell[1]=student.getName(); newCell[2]=student.getSex(); newCell[3]=student.getAge(); newCell[4]=student.getPhone(); newCell[5]=student.getQq(); newCell[6]=student.getMajor(); tableModel.addRow(newCell); } } public void clearTable(){ int rows=tableModel.getRowCount(); for(int i=rows-1;i>=0;i++){ tableModel.removeRow(i); } } public void selectFailure(){ JOptionPane.showMessageDialog(jf,"不存在该学号的学生!","提示",JOptionPane.WARNING_MESSAGE ); } }

②程序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的代码

创建User表和Student表,先定义各个变量,然后加上set和get方法。

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的代码

主要实现数据库的连接,存储学生信息。直接声明数据库的登录名和密码,和连接使用的URL,用try-catch来加载数据库驱动和给出提示信息。

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)程序设计的任务是先设计出一个登录窗口,输入用户名和密码。

如果输入错误,会像如图所示输出“用户名或密码输入不正确”的对话框:

==我给出的代码为了方便调试,用户名和密码全是0,记住运行的是LoginGUI这个类==

(2)如果正确,则成功登录,进入学生信息管理界面,如图所示:

2、添加

(1)点击“添加”按钮,进入学生信息添加界面:

(2)在以上界面输入要添加的学生信息,首先点击“上传照片”按钮,然后弹出如下窗口:

(3)点击浏览选项,选择本地文件里需要上传的图片,这里我们选择Taylor Swift的图片,然后点击上传,会显示“上传成功”的对话框。

(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数据库技术详解》 李刚 著

十、源码下载

学生信息管理系统的整体代码界面展示如下:

1、Main.java类

Java课程设计【学生信息管理系统】(基于java的学生信息管理系统课程设计)

2、View.java类

3、Student.java类

4、ConnectSQLServer类

5、LoginGUI类

这个系统也是当时为数不多的优秀课程设计,需要源码的同学可以关注博主的微信公众号,回复:==学生信息管理系统==,可以获得源码学习。

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

Java

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:NewSQL数据库之TiDB简介(newsql tidb)
下一篇:欺骗的艺术——社会工程学(社会工程学也是一种欺骗的手段)
相关文章