用java编写学生信息管理系统,只需有添加,删除,修改,查询,输出等功能即可!无需写入数据库!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java编写学生信息管理系统,只需有添加,删除,修改,查询,输出等功能即可!无需写入数据库!相关的知识,希望对你有一定的参考价值。
添加的信息,输出信息按学号有序排列的。希望添加时可以直接用有序插入!!
这次回答直接发到我邮箱772589582@qq.com
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
class Student implements java.io.Serializable
String number,name,specialty,grade,borth,sex;
public Student();
public void setNumber(String number) this.number=number;
public String getNumber() return number;
public void setName(String name) this.name=name;
public String getName() return name;
public void setSex(String sex) this.sex=sex;
public String getSex() return sex;
public void setSpecialty(String specialty) this.specialty=specialty;
public String getSpecialty() return specialty;
public void setGrade(String grade) this.grade=grade;
public String getGrade() return grade;
public void setBorth(String borth) this.borth=borth;
public String getBorth() return borth;
public class StudentManager extends JFrame
JLabel lb=new JLabel("录入请先输入记录,查询、删除请先输入学号,修改是对查询" +
"内容改后的保存!");
static JTextField Jnumber,Jname,Jspecialty,Jgrade,Jborth;
static JRadioButton boy,girl;
static ButtonGroup group=null;
static JButton Login,select,delete,update,show;
static JPanel p1,p2,p3,p4,p5,p6,pv,ph;
static Student student=null;
static Hashtable Shashtable=null;
static File file=null;
static FileInputStream inOne=null;
static ObjectInputStream inTwo=null;
static FileOutputStream outOne=null;
static ObjectOutputStream outTwo=null;
public StudentManager()
super("学生基本信息管理系统");
Jnumber=new JTextField(10);
Jname =new JTextField(10);
Jspecialty=new JTextField(10);
Jgrade=new JTextField(10);
Jborth=new JTextField(10);
group=new ButtonGroup();
boy=new JRadioButton("男",true);
girl=new JRadioButton("女",false);
group.add(boy);
group.add(girl);
Login=new JButton("录入");
select=new JButton("查询");
delete=new JButton("删除");
update=new JButton("修改");
show=new JButton("显示");
Login.addActionListener(new InputAct());
select.addActionListener(new InquestAct());
update.addActionListener(new ModifyAct());
delete.addActionListener(new DeleteAct());
show.addActionListener(new ShowAct());
update.setEnabled(false);
p1=new JPanel();
p1.add(new JLabel("学号:",JLabel.CENTER));
p1.add(Jnumber);
p2=new JPanel();
p2.add(new JLabel("姓名:",JLabel.CENTER));
p2.add(Jname);
p3=new JPanel();
p3.add(new JLabel("性别:",JLabel.CENTER));
p3.add(boy);
p3.add(girl);
p4=new JPanel();
p4.add(new JLabel("专业:",JLabel.CENTER));
p4.add(Jspecialty);
p5=new JPanel();
p5.add(new JLabel("年级:",JLabel.CENTER));
p5.add(Jgrade);
p6=new JPanel();
p6.add(new JLabel("出生:",JLabel.CENTER));
p6.add(Jborth);
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(Login);
ph.add(select);
ph.add(update);
ph.add(delete);
ph.add(show);
file=new File("学生信息.txt");
Shashtable=new Hashtable();
if(!file.exists())
try
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(Shashtable);
objectOut.close();
out.close();
catch(IOException e)
Container con=getContentPane();
con.setLayout(new BorderLayout());
con.add(lb, BorderLayout.NORTH);
con.add(pv, BorderLayout.CENTER);
con.add(ph, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100,100,600,300);
setVisible(true);
public static void main(String[] args) new StudentManager();
class InputAct implements ActionListener
public void actionPerformed(ActionEvent e)
update.setEnabled(false);
String number="";
number=Jnumber.getText();
if(number.length()>0)
try
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
Shashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
catch(Exception ee)System.out.println("创建散列表出现问题!");
if(Shashtable.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)
String name=Jname.getText();
String specialty=Jspecialty.getText();
String grade=Jgrade.getText();
String borth=Jborth.getText();
String sex=null;
if(boy.isSelected())sex=boy.getText();
elsesex=girl.getText();
student=new Student();
student.setNumber(number);
student.setName(name);
student.setSpecialty(specialty);
student.setGrade(grade);
student.setBorth(borth);
student.setSex(sex);
try
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
Shashtable.put(number,student);
outTwo.writeObject(Shashtable);
outTwo.close();
outOne.close();
catch(Exception ee)System.out.println("输出散列表出现问题!");
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
//end else1
//end if0
else
String warning="必须输入学号!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
//end else0
//end actionPerformed
//end class
class InquestAct implements ActionListener
public void actionPerformed(ActionEvent e)
String number="";
number=Jnumber.getText();
if(number.length()>0)
try
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
Shashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
catch(Exception ee)System.out.println("散列表有问题!");
if(Shashtable.containsKey(number))
update.setEnabled(true);
Student stu=(Student)Shashtable.get(number);
Jname.setText(stu.getName());
Jspecialty.setText(stu.getSpecialty());
Jgrade.setText(stu.getGrade());
Jborth.setText(stu.getBorth());
if(stu.getSex().equals("男"))boy.setSelected(true);
elsegirl.setSelected(true);
else
update.setEnabled(false);
String warning="该学号不存在!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
else
update.setEnabled(false);
String warning="必须输入学号!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
class ModifyAct implements ActionListener
public void actionPerformed(ActionEvent e)
String number=Jnumber.getText();
String name=Jname.getText();
String specialty=Jspecialty.getText();
String grade=Jgrade.getText();
String borth=Jborth.getText();
String sex=null;
if(boy.isSelected())sex=boy.getText();
elsesex=girl.getText();
Student 学生=new Student();
学生.setNumber(number);
学生.setName(name);
学生.setSpecialty(specialty);
学生.setGrade(grade);
学生.setBorth(borth);
学生.setSex(sex);
try
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
Shashtable.put(number, 学生);
outTwo.writeObject(Shashtable);
outTwo.close();
outOne.close();
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
catch(Exception ee)
System.out.println("录入修改出现异常!");
update.setEnabled(false);
class DeleteAct implements ActionListener
public void actionPerformed(ActionEvent e)
update.setEnabled(false);
String number=Jnumber.getText();
if(number.length()>0)
try
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
Shashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
catch(Exception ee)
if(Shashtable.containsKey(number))
Student stu=(Student)Shashtable.get(number);
Jname.setText(stu.getName());
Jspecialty.setText(stu.getSpecialty());
Jgrade.setText(stu.getGrade());
Jborth.setText(stu.getBorth());
if(stu.getSex().equals("男"))boy.setSelected(true);
elsegirl.setSelected(true);
String m="确定要删除该学生的记录吗?";
int ok=JOptionPane.showConfirmDialog(null,m,"确认",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
Shashtable.remove(number);
try
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
outTwo.writeObject(Shashtable);
outTwo.close();
outOne.close();
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
catch(Exception ee)System.out.println(ee);
else if(ok==JOptionPane.NO_OPTION)
Jnumber.setText(null);
Jname.setText(null);
Jspecialty.setText(null);
Jgrade.setText(null);
Jborth.setText(null);
else
String warning="该学号不存在!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
else
String warning="必须输入学号!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
class ShowAct implements ActionListener
public void actionPerformed(ActionEvent e)
new StudentShow(file);
class StudentShow extends JDialog
Hashtable 学生散列表= null;
JTextArea 显示=null;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
File file=null;
public StudentShow(File file)
super(new JFrame(),"显示对话框");
this.file=file;
显示=new JTextArea(16,30);
try
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
学生散列表=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
catch(Exception ee)
if(学生散列表.isEmpty())显示.append("目前还没有学生的信息记录!\n");
else
显示.setText("学号 姓名 性别 专业 年级 出生\n");
for(Enumeration enm=学生散列表.elements();enm.hasMoreElements();)
Student stu=(Student)enm.nextElement();
String sex="";
if(stu.getSex().equals("男"))sex="男";
else sex="女";
String str=stu.getNumber()+","+stu.getName()+","+sex+","
+stu.getSpecialty()+","+stu.getGrade()+","+stu.getBorth()+"\n";
显示.append(str);
JScrollPane scroll=new JScrollPane(显示);
Container con=getContentPane();
con.add("Center",scroll);
con.validate();
setVisible(true);
setBounds(200,200,400,300);
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)setVisible(false);
);
参考技术A 可以通过Baidu Hi提示我
有时间可能完成你所面临的任务
相关的要求也可能提示我
ES:\\5F97D3CBAC6B9B2690D1D8E74380C342
交易提醒:预付定金是诈骗追问
你理解错了,是添加时就是按有序添加进入程序中,你编写过有序链表吗?就是跟有序插入的!
Java 项目 学生信息管理系统 超详细 超完整 保姆级讲解(主页面 + 添加学生信息 + 删除学生信息 +...
Java 学生信息管理系统
关注博主不迷路,博主带你码代码!
1. 项目概述
这是一个学生信息管理系统,学生信息主要包括:学号,姓名,性别,出生日期,班级;暂时就只有这些信息,若后续需要,可以相应的进行添加。
该项目主要采用的技术是 Java GUI 设计,这个地方主要用 Java 工具 windowsbuilder 进行布局,(windowsbuilder没有安装或者不会安装的,可以看看我的这篇博客 Eclipse 安装 windowbuilder插件 ),MySql 数据库(MySql 数据库没有安装的 或者数据库使用软件 Navicat 没有安装的可以看看我的这篇博客 2021 MySql 8.0.26 安装教程(最详细的傻瓜教程) ),以及使用 Java 中的 JDBC 将数据库链接起来。
2. 数据库的建立
由于在项目中没有创建学生数据库的功能,所以学生数据库必须要先建立好,下面给出如何建立好MySQL学生数据库的相关代码 + 图片
-- 创建数据库 CREATE DATABASE niit DEFAULT CHARACTER SET utf8mb4; SHOW DATABASES; -- 使用数据库niit USE niit; -- 创建表Student CREATE TABLE Student ( ID INT PRIMARY KEY, NAME VARCHAR(4), GENDER CHAR(1), DOB DATE, BATCH INT ); -- 插入记录 INSERT INTO Student VALUES (1,'独孤求败','男','1988-1-2',1), (2,'周芷若','女','1988-12-3',1), (3,'金毛狮王','男','1978-11-1',1), (4,'蒙挚','男','1989-2-5',2), (5,'夏春','女','1998-4-3',2), (6,'梅长苏','男','1999-12-6',2); -- 查看表中所有记录 语法:select * from 表名 SELECT * FROM Student;
3. 项目页面
主页面
添加学生
修改信息
删除学生
查询信息
4. 项目代码 + 分析
主页面
package studentsmanager; import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class MainFrame extends JFrame private JPanel contentPane; /** * Launch the application. */ // 有了main函数才可以单独运行 public static void main(String[] args) EventQueue.invokeLater(new Runnable() public void run() try MainFrame frame = new MainFrame(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); ); /** * Create the frame. */ public MainFrame() // 设置大小不可改变 setResizable(false); // 设置标题 setTitle("学生管理系统"); // 由于这是主页面,所有当主页面关闭的时候,程序就直接退出 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setBounds(int x,int y,int width,int height); // x 和 y 是窗口打开时的位置 // width 和 height 是窗口打宽度和高度 setBounds(100, 100, 450, 350); // setLocationRelativeTo(c) // 设置窗口相对于 c 的位置,当 c 为空或者 null 时,默认为是相对于屏幕中央 setLocationRelativeTo(null); // 实例化一个 pane contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("学生管理系统"); lblNewLabel.setForeground(Color.RED); lblNewLabel.setFont(new Font("宋体", Font.BOLD, 25)); lblNewLabel.setBounds(140, 10, 163, 44); contentPane.add(lblNewLabel); JButton addButton = new JButton("添加学生"); // addActionListener 注册监听器 addButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) // 设置新窗口可见 new AddFrame().setVisible(true); ); addButton.setFont(new Font("宋体", Font.PLAIN, 18)); addButton.setBounds(167, 64, 114, 37); contentPane.add(addButton); JButton changeButton = new JButton("修改信息"); changeButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) // 同上 new ChangeFrame().setVisible(true); ); changeButton.setFont(new Font("宋体", Font.PLAIN, 18)); changeButton.setBounds(167, 121, 114, 37); contentPane.add(changeButton); JButton deleteButton = new JButton("删除学生"); deleteButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) // 同上 new DeleteFrame().setVisible(true); ); deleteButton.setFont(new Font("宋体", Font.PLAIN, 18)); deleteButton.setBounds(167, 180, 114, 37); contentPane.add(deleteButton); JButton queryButton = new JButton("查询信息"); queryButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) // 同上 new QueryFrame().setVisible(true); ); queryButton.setFont(new Font("宋体", Font.PLAIN, 18)); queryButton.setBounds(167, 240, 114, 37); contentPane.add(queryButton);
添加学生
package studentsmanager; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class AddFrame extends JFrame private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; /** * Launch the application. */ public static void main(String[] args) EventQueue.invokeLater(new Runnable() public void run() try AddFrame frame = new AddFrame(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); ); /** * Create the frame. */ public AddFrame() // 和前面类似 setResizable(false); setTitle("添加学生"); // 这个地方,添加学生只是这个程序的一部分,所以当关闭这部分的时候,程序不直接退出,只是关闭该部分程序 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel TitleLabel = new JLabel("请输入新学生的信息:"); TitleLabel.setFont(new Font("宋体", Font.BOLD, 20)); TitleLabel.setBounds(71, 34, 208, 45); panel.add(TitleLabel); JLabel idLabel = new JLabel("学号:"); idLabel.setFont(new Font("宋体", Font.PLAIN, 16)); idLabel.setBounds(71, 105, 50, 30); panel.add(idLabel); idField = new JTextField(); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel nameLabel = new JLabel("姓名:"); nameLabel.setFont(new Font("宋体", Font.PLAIN, 16)); nameLabel.setBounds(71, 160, 50, 30); panel.add(nameLabel); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel genderLabel = new JLabel("性别:"); genderLabel.setFont(new Font("宋体", Font.PLAIN, 16)); genderLabel.setBounds(71, 215, 50, 30); panel.add(genderLabel); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel dobLabel = new JLabel("出生日期:"); dobLabel.setFont(new Font("宋体", Font.PLAIN, 16)); dobLabel.setBounds(41, 270, 80, 30); panel.add(dobLabel); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel batchLabel = new JLabel("班级:"); batchLabel.setFont(new Font("宋体", Font.PLAIN, 16)); batchLabel.setBounds(71, 325, 50, 30); panel.add(batchLabel); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); JButton addButton = new JButton("添加"); // 添加鼠标监听事件 addButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) // 先取出数据 // getText() 返回字符串类型的 // Integer.parseInt 数据类型转换 int id = Integer.parseInt(idField.getText()); String nameString = nameField.getText(); String genderString = genderField.getText(); String dobfieldString = dobField.getText(); int batch = Integer.parseInt(batchField.getText()); // 输出一下学生的信息,方便修改 System.out.println(id + "\\t" + nameString + "\\t" + genderString + "\\t" + dobfieldString + "\\t" + batch); // SQLHelp 是自己写的一个工具类 SQLHelp sqlHelp = new SQLHelp(); try // 调用添加学生信息的方法 sqlHelp.addStudent(id, nameString, genderString, dobfieldString, batch); // 弹出对话框,提示用户添加成功 JOptionPane.showMessageDialog(AddFrame.this, "添加成功!"); catch (SQLException e1) // 这里捕获一下异常,因为学生的学号是唯一的,所以当学号已经存在的时候,提示用户,该学生信息已存在 // 异常代码为 23000 if(e1.getSQLState().equals("23000")) JOptionPane.showMessageDialog(AddFrame.this, "添加失败!该学生已存在"); e1.printStackTrace(); ); addButton.setFont(new Font("宋体", Font.PLAIN, 18)); addButton.setBounds(182, 389, 97, 33); panel.add(addButton);
添加成功:
添加失败:
修改信息
package studentsmanager; import java.awt.EventQueue; import java.awt.Font; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import java.awt.event.ActionListener; import java.sql.SQLException; import java.awt.event.ActionEvent; public class ChangeFrame extends JFrame private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; private JTextField searchField; /** * Launch the application. */ public static void main(String[] args) EventQueue.invokeLater(new Runnable() public void run() try ChangeFrame frame = new ChangeFrame(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); ); /** * Create the frame. */ public ChangeFrame() setResizable(false); setTitle("修改信息"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("请输入要修改的学生的学号:"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel.setBounds(71, 0, 208, 29); panel.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("学号:"); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1.setBounds(71, 105, 50, 30); panel.add(lblNewLabel_1); idField = new JTextField(); idField.setEditable(false); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel lblNewLabel_1_1 = new JLabel("姓名:"); lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1.setBounds(71, 160, 50, 30); panel.add(lblNewLabel_1_1); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel lblNewLabel_1_1_1 = new JLabel("性别:"); lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1.setBounds(71, 215, 50, 30); panel.add(lblNewLabel_1_1_1); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:"); lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30); panel.add(lblNewLabel_1_1_1_1); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:"); lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30); panel.add(lblNewLabel_1_1_1_2); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); JButton changeButton = new JButton("修改"); changeButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) int id = Integer.parseInt(searchField.getText()); String nameString = nameField.getText(); String gendeString = genderField.getText(); String dobString = dobField.getText(); int batch = Integer.parseInt(batchField.getText()); SQLHelp sqlHelp = new SQLHelp(); try sqlHelp.changeStudent(id, nameString, gendeString, dobString, batch); JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!"); catch (SQLException e) JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!"); e.printStackTrace(); ); changeButton.setFont(new Font("宋体", Font.PLAIN, 18)); changeButton.setBounds(182, 389, 97, 33); panel.add(changeButton); searchField = new JTextField(); searchField.setBounds(71, 39, 208, 45); panel.add(searchField); searchField.setColumns(10); JButton searchButton = new JButton("查找"); searchButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int id = Integer.parseInt(searchField.getText()); try SQLHelp sqlHelp = new SQLHelp(); Student student = sqlHelp.queryStudent(id); if(student != null) // id 是直接读入的,所以就不需要从数据库中读出了 idField.setText(String.valueOf(id)); nameField.setText(student.getName()); genderField.setText(student.getGender()); dobField.setText(student.getDob()); batchField.setText(String.valueOf(student.getBatch())); else JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户"); catch (SQLException e1) e1.printStackTrace(); ); searchButton.setFont(new Font("宋体", Font.PLAIN, 18)); searchButton.setBounds(289, 42, 97, 39); panel.add(searchButton);
修改前查询到学生信息
修改成功
修改失败
失败的情况是数据链接出现了问题
未查询到学生信息
删除学生
package studentsmanager; import java.awt.EventQueue; import java.awt.Font; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import java.awt.event.ActionListener; import java.sql.SQLException; import java.awt.event.ActionEvent; public class DeleteFrame extends JFrame private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; private JTextField searchField; /** * Launch the application. */ public static void main(String[] args) EventQueue.invokeLater(new Runnable() public void run() try DeleteFrame frame = new DeleteFrame(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); ); /** * Create the frame. */ public DeleteFrame() setResizable(false); setTitle("删除学生"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("请输入要删除的学生的学号:"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel.setBounds(71, 0, 208, 29); panel.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("学号:"); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1.setBounds(71, 105, 50, 30); panel.add(lblNewLabel_1); idField = new JTextField(); idField.setEditable(false); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel lblNewLabel_1_1 = new JLabel("姓名:"); lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1.setBounds(71, 160, 50, 30); panel.add(lblNewLabel_1_1); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel lblNewLabel_1_1_1 = new JLabel("性别:"); lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1.setBounds(71, 215, 50, 30); panel.add(lblNewLabel_1_1_1); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:"); lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30); panel.add(lblNewLabel_1_1_1_1); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:"); lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30); panel.add(lblNewLabel_1_1_1_2); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); JButton deleteButton = new JButton("删除"); deleteButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int id = Integer.parseInt(searchField.getText()); try SQLHelp sqlHelp = new SQLHelp(); sqlHelp.deletestudent(id); JOptionPane.showMessageDialog(DeleteFrame.this, "删除成功!"); catch (SQLException e1) JOptionPane.showMessageDialog(DeleteFrame.this, "删除失败"); e1.printStackTrace(); ); deleteButton.setFont(new Font("宋体", Font.PLAIN, 18)); deleteButton.setBounds(182, 389, 97, 33); panel.add(deleteButton); searchField = new JTextField(); searchField.setBounds(71, 39, 208, 45); panel.add(searchField); searchField.setColumns(10); JButton searchButton = new JButton("查找"); searchButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) int id = Integer.parseInt(searchField.getText()); try SQLHelp sqlHelp = new SQLHelp(); Student student = sqlHelp.queryStudent(id); if(student != null) idField.setText(String.valueOf(id)); nameField.setText(student.getName()); genderField.setText(student.getGender()); dobField.setText(student.getDob()); batchField.setText(String.valueOf(student.getBatch())); else JOptionPane.showMessageDialog(DeleteFrame.this, "无此学生"); catch (SQLException e) e.printStackTrace(); ); searchButton.setFont(new Font("宋体", Font.PLAIN, 18)); searchButton.setBounds(289, 42, 97, 39); panel.add(searchButton);
删除成功
删除失败
删除失败的时候是数据库链接出现了问题
查询到学生信息
未查询到学生
查询信息
package studentsmanager; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class QueryFrame extends JFrame private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; private JTextField searchField; /** * Launch the application. */ public static void main(String[] args) EventQueue.invokeLater(new Runnable() public void run() try QueryFrame frame = new QueryFrame(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); ); /** * Create the frame. */ public QueryFrame() setResizable(false); setTitle("查询信息"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel.setBounds(71, 0, 208, 29); panel.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("学号:"); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1.setBounds(71, 105, 50, 30); panel.add(lblNewLabel_1); idField = new JTextField(); idField.setEditable(false); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel lblNewLabel_1_1 = new JLabel("姓名:"); lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1.setBounds(71, 160, 50, 30); panel.add(lblNewLabel_1_1); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel lblNewLabel_1_1_1 = new JLabel("性别:"); lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1.setBounds(71, 215, 50, 30); panel.add(lblNewLabel_1_1_1); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:"); lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30); panel.add(lblNewLabel_1_1_1_1); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:"); lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16)); lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30); panel.add(lblNewLabel_1_1_1_2); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); searchField = new JTextField(); searchField.setBounds(71, 39, 208, 45); panel.add(searchField); searchField.setColumns(10); JButton searchButton = new JButton("查找"); searchButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) // 1.读出要查询的学生输入的id // 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据 // 转化一下数据类型 int id = Integer.parseInt(searchField.getText()); // 2.执行JDBC语句 try SQLHelp sqlHelp = new SQLHelp(); Student student = sqlHelp.queryStudent(id); // 3.将查询结果填到文本框中 // 前提是学生存在 if(student != null) idField.setText(String.valueOf(id)); nameField.setText(student.getName()); genderField.setText(student.getGender()); dobField.setText(student.getDob()); batchField.setText(String.valueOf(student.getBatch())); else // 此时学生不存在,提示用户,不存在该学生 JOptionPane.showMessageDialog(QueryFrame.this, "无此学生"); catch (SQLException e) e.printStackTrace(); ); searchButton.setFont(new Font("宋体", Font.PLAIN, 18)); searchButton.setBounds(289, 42, 97, 39); panel.add(searchButton);
SQLHelp 工具类
package studentsmanager; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class SQLHelp public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; public static final String DB_URL = "jdbc:mysql://localhost:3306/niit?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; public static final String username = "root"; public static final String password = "011216"; private Connection connection = null; private PreparedStatement pStatement = null; private ResultSet rSet = null; // 加载驱动 // 静态初始化块(只执行一次) static try Class.forName(JDBC_DRIVER); catch (ClassNotFoundException e) // TODO Auto-generated catch block e.printStackTrace(); // 链接数据库 public void connectDB() try connection = DriverManager.getConnection(DB_URL,username,password); System.out.println("数据库链接成功"); catch (SQLException e) System.out.println("数据库链接失败"); e.printStackTrace(); // 关闭资源 public void close() if(rSet != null) try rSet.close(); catch (SQLException e) // TODO Auto-generated catch block e.printStackTrace(); if(pStatement != null) try pStatement.close(); catch (SQLException e) // TODO Auto-generated catch block e.printStackTrace(); if(connection != null) try connection.close(); catch (SQLException e) // TODO Auto-generated catch block e.printStackTrace(); public void addStudent(int id, String nameString, String genderString, String dobString, int batch) throws SQLException // try finally 无论是否抛出异常都将执行 finally 中的语句 try // 先链接到数据库 connectDB(); // sql 语句 // 静态 sql 语句需要进行字符串拼接 // 动态 sql 语句 String addsql = "insert into student values(?, ?, ?, ?, ?)"; pStatement = connection.prepareStatement(addsql); pStatement.setInt(1, id); pStatement.setString(2, nameString); pStatement.setString(3, genderString); pStatement.setString(4, dobString); pStatement.setInt(5, batch); pStatement.executeUpdate(); finally close(); public void changeStudent(int id, String nameString, String genderString, String dobString, int batch) throws SQLException try connectDB(); String changesql = "update student set name = ?, gender = ?, dob = ?, batch = ? where id = ?"; pStatement = connection.prepareStatement(changesql); pStatement.setString(1, nameString); pStatement.setString(2, genderString); pStatement.setString(3, dobString); pStatement.setInt(4, batch); pStatement.setInt(5, id); pStatement.executeUpdate(); finally close(); public void deletestudent(int id) throws SQLException try connectDB(); String deleteString = "select * from student where id = ?"; pStatement = connection.prepareStatement(deleteString); pStatement.setInt(1, id); pStatement.execute(); finally close(); public Student queryStudent(int id) throws SQLException try connectDB(); String querysql = "select * from student where id = ?"; pStatement = connection.prepareStatement(querysql); pStatement.setInt(1, id); // 三种方法执行 // execute() 都可以使用,返回true或false // executeQuery() 专门用于查询,返回结果集 // executeUpdate() 专门用于删除、更新 rSet = pStatement.executeQuery(); if(rSet.next()) String nameString = rSet.getString(2); String genderString = rSet.getString(3); String dobString = rSet.getString(4); int batchString = rSet.getInt(5); // 查询到学生信息返回结果集 return new Student(nameString, genderString, dobString, batchString); else // 没有查询到学生信息,返回null return null; finally close();
Student 工具类
package studentsmanager; public class Student private String name; private String gender; private String dob; private int batch; public Student(String name, String gender, String dob, int batch) this.name = name; this.gender = gender; this.dob = dob; this.batch = batch; public String getName() return name; public String getGender() return gender; public String getDob() return dob; public int getBatch() return batch;
5. 项目总结
这算一个比较完整的项目了,既有 GUI 又有 MySQL 数据库的已经 Java 的 JDBC ,虽然已经有了 GUI 界面,但很明显,没有进行任何美化,就只是简单的一个界面,勉勉强强可以看的过去,使用 JDBC 链接数据库的时候,没有实现可以直接在程序创建数据库,只能从数据库方创建数据库,这是一个缺点,同时使用 JDBC 链接数据库的时候,所使用的代码依旧是 MySQL 数据库的代码,(我 MySQL 学的不好,增删改查啥的数据库代码都还记不清楚 哭了…)还有就是当程序在进行修改和删除的时候,修改失败和删除失败的时候,都是因为数据链接失败了才进行弹窗,不知道怎么改成无法修改或者无法删除的时候提示,(又哭了…)
欢迎个位大佬指出错误,同时也欢迎各位小白借鉴,有问题的话,评论或者私聊都是可以的,看了就会回复!
以上是关于用java编写学生信息管理系统,只需有添加,删除,修改,查询,输出等功能即可!无需写入数据库!的主要内容,如果未能解决你的问题,请参考以下文章
用java编写一个学生成绩管理系统. //1.学生类 学号 姓名 年龄 语数外三科成绩(三科成绩)?