Java课程设计
前言
来csdn也有两年了,在这期间学了很多的知识,也跟着许多的大神做了几个小项目。但自己也在想什么时候能够有个自己的项目,之前在学校做过一个基于集合存储数据的学生信息管理系统,现在想着改进一下,于是趁着暑假做了一个小项目吧(基于mysql+jdbc+java swing的学生成绩信息管理系统)。一来是巩固自己的学习成果,二来是记录自己的学习历程,期待后面能做出更多的项目吧。
设计需求和思路
设计需求
1.利用jdbc将java程序与mysql建立连接
2.利用java swing将学生信息和成绩信息显现出来
3.通过使用sql指令来实现对学生信息和成绩信息的crud(增查改删)操作
设计思路
1.利用Navicat创建两个数据表(stu和classes),同时两个数据表用外键约束连接(
CONSTRAINT `fk_classes_stu` FOREIGN KEY (`class_id`) REFERENCES `stu` (`stu_num`) ON DELETE CASCADE
)
2.使用sql指令对数据表中数据进行增删改查,并通过java swing显示
3.查询所有学生信息,利用连接查询将两个数据表的信息一并显示
详细操作
1.设计主界面(登录注册界面)
账号和密码通过HashMap来存储,账号为键密码为值,利用其基本方法来添加账号密码(注册),以及登录。
package view;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;public class MainView extends JFrame {private Map<String,String> userhashmap=new HashMap<>();JPanel p1=new JPanel();JPanel p2=new JPanel();JPanel p3=new JPanel();JPanel p4=new JPanel();JLabel welcome=new JLabel("欢迎来到学生信息管理系统");JLabel username=new JLabel("账号:");JLabel password=new JLabel("密码:");JTextField usernametext=new JTextField(10);JPasswordField passwordField=new JPasswordField(10);JButton confirm=new JButton("确认");JButton exit=new JButton("退出");JButton register=new JButton("注册");public MainView() {this.setTitle("学生信息管理系统");this.setSize(800, 500);this.setLayout(new GridLayout(4, 1,10,10));this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);p4.add(welcome);p1.add(username);p1.add(usernametext);p2.add(password);p2.add(passwordField);p3.add(confirm);p3.add(register);p3.add(exit);this.add(p4);this.add(p1);this.add(p2);this.add(p3);register.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==register){if(userhashmap.containsKey(usernametext.getText())){JOptionPane.showMessageDialog(null,"注册失败,您的账号已存在","警告",JOptionPane.WARNING_MESSAGE);return;}userhashmap.put(usernametext.getText(),passwordField.getText());JOptionPane.showMessageDialog(null,"注册成功","通知",JOptionPane.INFORMATION_MESSAGE);}}});confirm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == confirm) {if (userhashmap.containsKey(usernametext.getText())) {if (userhashmap.get(usernametext.getText()).equals(passwordField.getText())) {JOptionPane.showMessageDialog(null,"登陆成功","通知",JOptionPane.INFORMATION_MESSAGE);dispose();ManagerView mv=new ManagerView();mv.setVisible(true);} else {JOptionPane.showMessageDialog(null,"密码错误","警告",JOptionPane.WARNING_MESSAGE);}}else{JOptionPane.showMessageDialog(null,"该用户不存在","警告",JOptionPane.WARNING_MESSAGE);}}}});exit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});}public static void main(String[] args) {MainView m=new MainView();m.setVisible(true);}
}
2.设计学生信息管理界面和成绩信息管理界面
使用GridBagLayout布局设置左边为学生信息管理的按钮,右边为成绩信息管理的按钮
package view;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class ManagerView extends JFrame{JButton insert=new JButton("添加学生信息");JButton delete=new JButton("删除学生");JButton update=new JButton("学生信息修改");JButton query=new JButton("查询学生信息");JButton queryall=new JButton("查询所有学生信息");JButton insertclass=new JButton("添加成绩信息");JButton deleteclass=new JButton("删除成绩信息");JButton updateclass=new JButton("修改成绩信息");JLabel labelstu=new JLabel("学生管理系统");//设置“学生管理系统”标签JLabel labelclass=new JLabel("成绩管理系统");//设置“成绩管理系统”标签public ManagerView(){this.setTitle("学生成绩管理");this.setSize(1200,500);this.setLayout(new GridBagLayout());this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置每个按钮长,高为140,120Dimension dimension=new Dimension(140,30);//通过setPreferredSize()方法来设置按钮和标签大小,但要传入Dimension对象insert.setPreferredSize(dimension);delete.setPreferredSize(dimension);update.setPreferredSize(dimension);query.setPreferredSize(dimension);queryall.setPreferredSize(dimension);insertclass.setPreferredSize(dimension);deleteclass.setPreferredSize(dimension);updateclass.setPreferredSize(dimension);labelstu.setPreferredSize(dimension);labelclass.setPreferredSize(dimension);GridBagConstraints constraintsleft=new GridBagConstraints();constraintsleft.gridx = 0; // 列为0constraintsleft.gridy = GridBagConstraints.RELATIVE; // 按钮的行相对于前一个按钮的行constraintsleft.anchor = GridBagConstraints.CENTER; // 设置水平方向上的位置居中constraintsleft.fill = GridBagConstraints.HORIZONTAL; // 填充水平方向/*constraintsleft.weightx = 0.5; // 水平方向上的权重为0.5*/constraintsleft.insets = new Insets(5, 5, 5, 200); // 设置按钮的内边距constraintsleft.ipadx = 10;this.add(labelstu,constraintsleft);this.add(insert,constraintsleft);this.add(delete,constraintsleft);this.add(update,constraintsleft);this.add(query,constraintsleft);this.add(queryall,constraintsleft);GridBagConstraints constraintsright=new GridBagConstraints();constraintsright.gridx=1;constraintsleft.gridy = GridBagConstraints.RELATIVE; // 按钮的行相对于前一个按钮的行constraintsleft.anchor = GridBagConstraints.CENTER; // 设置水平方向上的位置居中constraintsleft.fill = GridBagConstraints.HORIZONTAL; // 填充水平方向/*constraintsleft.weightx = 0.5; // 水平方向上的权重为0.5*/constraintsleft.insets = new Insets(5, 200, 5, 5); // 设置按钮的内边距constraintsleft.ipadx = 10;this.add(labelclass,constraintsright);this.add(insertclass,constraintsright);this.add(deleteclass,constraintsright);this.add(updateclass,constraintsright);insert.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {InsertStuView insertStuView=new InsertStuView();insertStuView.setVisible(true);}});delete.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {DeleteStuView deleteStuView=new DeleteStuView();deleteStuView.setVisible(true);}});update.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {UpdateStuview updateStuview=new UpdateStuview();updateStuview.setVisible(true);}});query.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {QueryStuView queryStuView=new QueryStuView();queryStuView.setVisible(true);}});insertclass.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {InsertClassView insertClassView=new InsertClassView();insertClassView.setVisible(true);}});deleteclass.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {DeleteClassView deleteClassView=new DeleteClassView();deleteClassView.setVisible(true);}});updateclass.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {UpdateClassView updateClassView =new UpdateClassView();updateClassView.setVisible(true);}});
/* p1.add(insert);p2.add(delete);p3.add(query);p4.add(update);p5.add(orderby);*/queryall.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {QueryAllView queryAllView=new QueryAllView();queryAllView.setVisible(true);}});}
}
3.设计学生信息操作
添加学生信息
package view;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import service.StuManage;public class InsertStuView extends JFrame {JLabel labelname=new JLabel("请输入您的姓名");JLabel labelgender=new JLabel("请输入您的性别");JLabel labelid=new JLabel("请输入您的学号");JTextField textname=new JTextField(15);JTextField textgender=new JTextField(15);JTextField textid=new JTextField(15);JButton confirm=new JButton("确定");JButton cancel=new JButton("取消");JPanel p1=new JPanel();JPanel p2=new JPanel();JPanel p3=new JPanel();JPanel p4=new JPanel();public InsertStuView(){this.setSize(500,300);this.setTitle("请添加学生信息");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLayout(new GridLayout(4,1,10,10));p1.add(labelname);p1.add(textname);p2.add(labelgender);p2.add(textgender);p3.add(labelid);p3.add(textid);p4.add(confirm);p4.add(cancel);this.add(p1);this.add(p2);this.add(p3);this.add(p4);confirm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {StuManage.insertStudata(textname.getText(),textgender.getText(),textid.getText());} catch (Exception ex) {JOptionPane.showMessageDialog(null,"添加失败","警告",JOptionPane.WARNING_MESSAGE);}dispose();}});cancel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();}});}
}
通过学号删除学生,因为学号不会重复
package view;import service.StuManage;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class DeleteStuView extends JFrame {JLabel labelid=new JLabel("请输入您想删除学生的学号");JTextField textField=new JTextField(15);JPanel p1=new JPanel();JPanel p2=new JPanel();JButton confirm=new JButton("确定");JButton cancel=new JButton("取消");public DeleteStuView(){this.setSize(500,300);this.setLayout(new GridLayout(2,1,0,0));this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setTitle("删除学生");p1.add(labelid);p1.add(textField);p2.add(confirm);p2.add(cancel);p1.setBorder(BorderFactory.createEmptyBorder(70,0,0,0));this.add(p1);this.add(p2);confirm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try{StuManage.deleteStudata(textField.getText());}catch(Exception ex){JOptionPane.showMessageDialog(null,"删除失败","警告",JOptionPane.WARNING_MESSAGE);}dispose();}});cancel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();}});}
}
学生信息修改
package view;
import service.StuManage;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class UpdateStuview extends JFrame{
JLabel labeloldid=new JLabel("请输入您要修改学生的学号");
JLabel labelname=new JLabel("请输入您要修改后的姓名");
JLabel labelgender=new JLabel("请输入您要修改后的性别");
JLabel labelid=new JLabel("请输入您要修改后的学号");
JTextField textFieldoldid=new JTextField(15);
JTextField textname=new JTextField(15);
JTextField textgender=new JTextField(15);
JTextField textid=new JTextField(15);
JButton confirm=new JButton("确定");
JButton cancel=new JButton("取消");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
public UpdateStuview(){
this.setSize(500,300);
this.setTitle("请修改学生信息");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(5,1,10,10));
p1.add(labeloldid);
p1.add(textFieldoldid);
p2.add(labelname);
p2.add(textname);
p3.add(labelgender);
p3.add(textgender);
p4.add(labelid);
p4.add(textid);
p5.add(confirm);
p5.add(cancel);this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
StuManage.updateStudata(textname.getText(),textgender.getText(),textid.getText(),textFieldoldid.getText());
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"修改失败","警告",JOptionPane.WARNING_MESSAGE);
}
dispose();
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
查询学生信息
package view;import service.ClassManage;import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class QueryStuView extends JFrame {JLabel id=new JLabel("请输入学生的id");JTextField textid=new JTextField(15);JButton confirm=new JButton("确定");JButton cancel=new JButton("取消");JLabel labelstu=new JLabel("学生信息表");JLabel labelscore=new JLabel("成绩信息表");JPanel p1=new JPanel();JPanel p2=new JPanel();JPanel p3=new JPanel();JPanel p4=new JPanel();String[] stucolumnname={"姓名","性别","学号"};String[] scorecolumnname={"学科","分数","教师"};
public QueryStuView(){this.setLayout(new GridLayout(4,4,10,10));this.setTitle("学生信息查询");this.setSize(1000,700);DefaultTableModel stumodel=new DefaultTableModel();DefaultTableModel scoremodel=new DefaultTableModel();stumodel.setColumnIdentifiers(stucolumnname);scoremodel.setColumnIdentifiers(scorecolumnname);JTable stutable = new JTable(stumodel);JTable scoretable=new JTable(scoremodel);JScrollPane scrollstu=new JScrollPane(stutable);JScrollPane scrollscore=new JScrollPane(scoretable);p1.setBorder(BorderFactory.createEmptyBorder(70,0,0,0));p1.add(id);p1.add(textid);p2.add(confirm);p2.add(cancel);p3.add(labelstu);p3.add(scrollstu);p4.add(labelscore);p4.add(scrollscore);this.add(p1);this.add(p2);this.add(p3);this.add(p4);confirm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try{stumodel.setRowCount(0);scoremodel.setRowCount(0);ClassManage.querystu(textid.getText(),stumodel,scoremodel);}catch(Exception ex){JOptionPane.showMessageDialog(null,"查找失败","警告",JOptionPane.WARNING_MESSAGE);}}});cancel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dispose();}});}
}
查询所有学生信息
package view;import service.ClassManage;import javax.swing.*;
import javax.swing.table.DefaultTableModel;public class QueryAllView extends JFrame{JLabel text=new JLabel("学生信息查询");DefaultTableModel model=new DefaultTableModel();JTable queryall=new JTable(model);JScrollPane jScrollPane=new JScrollPane(queryall);JPanel p1=new JPanel();JPanel p2=new JPanel();public QueryAllView(){this.setTitle("学生成绩查询");this.setSize(1000,700);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);String[] columnname={"姓名","性别","学号","科目","分数"};model.setColumnIdentifiers(columnname);p1.add(text);p2.add(jScrollPane);this.add(p1);this.add(p2);try{ClassManage.queryall(model);}catch(Exception ex){JOptionPane.showMessageDialog(null,"查询失败","警告",JOptionPane.WARNING_MESSAGE);}}
}
功能效果展示
1.项目结构
2.stu数据表和classes数据表初始数据
3.登录和注册功能演示
4.添加学生信息
5. 删除学生
6.学生信息修改
7.查询学生信息
8.查询所有学生信息
9.添加学生成绩
10.删除学生成绩信息(学号为20210004的学生的Java成绩信息全被删除)
11.修改学生成绩信息(修改学号为20210004的学生的C++成绩,修改为100)
mysql数据表结构
1.学生数据表(stu)结构
前言:先创建名为pro_stumanage的数据库,再使用改数据,其次再创建两个数据表
2.成绩数据表(classes)结构
tips:通过外键约束将classes的class_id和stu的stu_num关联,并且添加了删除的级联操作,这样删除学生,其成绩也会删除
总结
花了三四天的时间做了个小项目,基本上我想要做的功能已基本实现了,能够基本实现对学生信息的增删改查操作。同时,我学到了很多之前没学到的知识,也对近期学的知识进行了巩固和复习。不过我是学完之后再做,所以可能有些bug和小问题,也欢迎各位来提问指正问题,也期待自己能够作出更多更优质的项目。如果想要源码可以直接私信或者通过百度网盘下载源码。