Swing +Hibernate做的一个简易学生信息管理系统

只贴代码算了:

主窗口文件:
package com.hejianjiao.student.main;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import com.hejianjiao.student.oper.Delete;
import com.hejianjiao.student.oper.Inquest;
import com.hejianjiao.student.oper.ModifySituation;
import com.hejianjiao.student.oper.StudentSituation;

public class StudentManager extends JFrame implements ActionListener {

StudentSituation 基本信息录入 = null;
ModifySituation 基本信息修改 = null;
Inquest 基本信息查询 = null;
Delete 基本信息删除 = null;

JMenuBar bar;
JMenu fileMenu;
JMenuItem 录入, 修改, 查询, 删除;
Container con = null;
Hashtable 基本信息 = null;
// File file = null;
CardLayout card = null;
JLabel label = null;
JPanel pCenter = null;

/**
* 构造器
* */
public StudentManager() {
录入 = new JMenuItem("录入学生基本信息");
修改 = new JMenuItem("修改学生基本信息");
查询 = new JMenuItem("查询学生基本信息");
删除 = new JMenuItem("删除学生基本信息");

bar = new JMenuBar();
fileMenu = new JMenu("菜单选项");
fileMenu.add(录入);
fileMenu.add(修改);
fileMenu.add(查询);
fileMenu.add(删除);

bar.add(fileMenu);

this.setJMenuBar(bar);

label = new JLabel("欢迎使用学生信息管理系统", JLabel.CENTER);
label.setFont(new Font("TimesRoman", Font.BOLD, 24));
label.setForeground(Color.red);

基本信息 = new Hashtable();
录入.addActionListener(this);
修改.addActionListener(this);
查询.addActionListener(this);
删除.addActionListener(this);

card = new CardLayout();
con = getContentPane();
pCenter = new JPanel();
pCenter.setLayout(card);
// file = new File("基本信息.txt");

// if (!file.exists()) {
// try {
// FileOutputStream out = new FileOutputStream(file);
// ObjectOutputStream objectOut = new ObjectOutputStream(out);
// objectOut.writeObject(基本信息);
// objectOut.close();
// out.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }

基本信息录入 = new StudentSituation();
基本信息修改 = new ModifySituation();
基本信息查询 = new Inquest(this);
基本信息删除 = new Delete();

pCenter.add("欢迎语界面", label);
pCenter.add("录入界面", 基本信息录入);
pCenter.add("修改界面", 基本信息修改);
pCenter.add("删除界面", 基本信息删除);
con.add(pCenter, BorderLayout.CENTER);
con.validate();
this.addWindowFocusListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
setBounds(100, 50, 420, 380);
validate();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == 录入) {
card.show(pCenter, "录入界面");
} else if (e.getSource() == 修改) {
card.show(pCenter, "修改界面");
} else if (e.getSource() == 查询) {
// 基本信息查询 窗口设置为独立窗口,以便于用户进行查询
基本信息查询.setVisible(true);
} else if (e.getSource() == 删除) {
card.show(pCenter, "删除界面");
}
}

public static void main(String[] args) {
new StudentManager();
}

}




package com.hejianjiao.student.oper;

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

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.hejianjiao.database.hibernate.StudentOper;
import com.hejianjiao.student.entity.Student;

/**
* 学生信息录入
*/
public class StudentSituation extends JPanel implements ActionListener {

// List 基本信息表 = null;
JTextField 学号, 姓名, 专业, 年级, 出生;
JRadioButton 男, 女;

Student 学生 = null;
ButtonGroup group = null;
JButton 录入, 重置;

// FileInputStream inOne = null;
// ObjectInputStream inTwo = null;
// FileOutputStream outOne = null;
// ObjectOutputStream outTwo = null;
//
// File file = null;

public StudentSituation() {
// this.file = file;
学号 = new JTextField(10);
姓名 = new JTextField(10);
专业 = new JTextField(10);
年级 = new JTextField(10);
出生 = new JTextField(10);

group = new ButtonGroup();

男 = new JRadioButton("男");
女 = new JRadioButton("女");
group.add(女);
group.add(男);

录入 = new JButton("录入");
重置 = new JButton("重置");

录入.addActionListener(this);
重置.addActionListener(this);
Box box1 = Box.createHorizontalBox();// 创建6个水平Box容器
box1.add(new JLabel("学号:", JLabel.CENTER));
box1.add(学号);
Box box2 = Box.createHorizontalBox();
box2.add(new JLabel("姓名:", JLabel.CENTER));
box2.add(姓名);
Box box3 = Box.createHorizontalBox();
box3.add(new JLabel("性别:", JLabel.CENTER));
box3.add(男);
box3.add(女);
Box box4 = Box.createHorizontalBox();
box4.add(new JLabel("专业:", JLabel.CENTER));
box4.add(专业);
Box box5 = Box.createHorizontalBox();
box5.add(new JLabel("年级:", JLabel.CENTER));
box5.add(年级);
Box box6 = Box.createHorizontalBox();
box6.add(new JLabel("出生:", JLabel.CENTER));
box6.add(出生);

Box boxH = Box.createVerticalBox();// 创建一个垂直的box容器,其中从上向下排列6个水平Box容器

boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());// 垂直box容器内的组件靠上对齐

JPanel pCenter = new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter, BorderLayout.CENTER);
JPanel pSourth = new JPanel();
pSourth.add(录入);
pSourth.add(重置);
add(pSourth, BorderLayout.SOUTH);
validate();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == 录入) {
String number = "";
number = 学号.getText();

if (number.length() > 0) {
// try {
// inOne = new FileInputStream(file);
// inTwo = new ObjectInputStream(inOne);
// System.out.println("基本信息表=" + 基本信息表);
// 基本信息表 = (Hashtable) inTwo.readObject();// 首先到文件中读取HashTable对象
// inOne.close();
// inTwo.close();
// } catch (FileNotFoundException e1) {
// e1.printStackTrace();
// } catch (IOException e1) {
// e1.printStackTrace();
// } catch (ClassNotFoundException e1) {
// e1.printStackTrace();
// }

StudentOper oper = new StudentOper();
Student student = oper.getStudent(number);

if (student != null) {// 如果学号对应的学生基本信息已经存在,弹出警告对话框.
String warning = "该生基本信息已经存在,请到修改页面修改!";
JOptionPane.showMessageDialog(this, warning, "警告",
JOptionPane.WARNING_MESSAGE);
} else {
String m = "基本信息将被录入!";
int ok = JOptionPane.showConfirmDialog(this, m, "确认",
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (ok == JOptionPane.YES_OPTION) {
String name = 姓名.getText();
String discipling = 专业.getText();
String grade = 年级.getText();
String borth = 出生.getText();
String sex = null;
if (男.isSelected()) {
sex = 男.getText();
} else {
sex = 女.getText();
}

学生 = new Student();
学生.setName(name);
学生.setId(number);
学生.setDiscipling(discipling);
学生.setBorth(borth);
学生.setSex(sex);
学生.setGrade(grade);

// 存进数据库
boolean flag = oper.saveStudent(学生);
if (flag) {
String success = "数据保存成功!";
JOptionPane.showMessageDialog(this, success,
"成功啦!", JOptionPane.OK_OPTION);
} else {
String fail = "数据保存失败!";
JOptionPane.showMessageDialog(this, fail, "失败啦!",
JOptionPane.CANCEL_OPTION);
}

// // 新增的信息存入散列文件中
// try {
// outOne = new FileOutputStream(file);
// outTwo = new ObjectOutputStream(outOne);
// 基本信息表.put(number, 学生);
// outTwo.close();
// outOne.close();
// 学号.setText(null);
// 姓名.setText(null);
// 专业.setText(null);
// 年级.setText(null);
// 出生.setText(null);
// } catch (FileNotFoundException e1) {
// System.out.println(e1);
// e1.printStackTrace();
// } catch (IOException e1) {
// System.out.println(e1);
// e1.printStackTrace();
// }
}
}
} else {// 还没有输入
String warning = "你必须输入学号!";
JOptionPane.showMessageDialog(this, warning, "警告",
JOptionPane.WARNING_MESSAGE);
}
}

if (e.getSource() == 重置) {
学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}
}

}




package com.hejianjiao.student.oper;

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

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.hejianjiao.database.hibernate.StudentOper;
import com.hejianjiao.student.entity.Student;

/**
* 学生信息修改
*/
public class ModifySituation extends JPanel implements ActionListener {

// List 基本信息表 = null;
JTextField 学号, 姓名, 专业, 年级, 出生;
JRadioButton 男, 女;
ButtonGroup group = null;
JButton 开始修改, 录入修改, 重置;

// FileInputStream inOne = null;
// ObjectInputStream inTwo = null;
// FileOutputStream outOne = null;
// ObjectOutputStream outTwo = null;
//
// File file = null;

public ModifySituation() {
// this.file = file;
学号 = new JTextField(10);
姓名 = new JTextField(10);
专业 = new JTextField(10);
年级 = new JTextField(10);
出生 = new JTextField(10);

男 = new JRadioButton("男", false);
女 = new JRadioButton("女", false);
group = new ButtonGroup();
group.add(男);
group.add(女);

开始修改 = new JButton("开始修改");
录入修改 = new JButton("录入修改");
录入修改.setEnabled(false);

重置 = new JButton("重置");

学号.addActionListener(this);
开始修改.addActionListener(this);
录入修改.addActionListener(this);
重置.addActionListener(this);

Box box1 = Box.createHorizontalBox();// 创建6个水平Box容器
box1.add(new JLabel("输入要修改信息的学号:", JLabel.CENTER));
box1.add(学号);
box1.add(开始修改);
Box box2 = Box.createHorizontalBox();
box2.add(new JLabel("(新)姓名:", JLabel.CENTER));
box2.add(姓名);
Box box3 = Box.createHorizontalBox();
box3.add(new JLabel("(新)性别:", JLabel.CENTER));
box3.add(男);
box3.add(女);
Box box4 = Box.createHorizontalBox();
box4.add(new JLabel("(新)专业:", JLabel.CENTER));
box4.add(专业);
Box box5 = Box.createHorizontalBox();
box5.add(new JLabel("(新)年级:", JLabel.CENTER));
box5.add(年级);
Box box6 = Box.createHorizontalBox();
box6.add(new JLabel("(新)出生:", JLabel.CENTER));
box6.add(出生);

Box boxH = Box.createVerticalBox();// 创建一个垂直的box容器,其中从上向下排列6个水平Box容器

boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());// 垂直box容器内的组件靠上对齐

JPanel pCenter = new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter, BorderLayout.CENTER);
JPanel pSourth = new JPanel();
pSourth.add(录入修改);
pSourth.add(重置);
add(pSourth, BorderLayout.SOUTH);
validate();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == 开始修改 || e.getSource() == 学号) {
String number = 学号.getText();

if (number.length() > 0) {
// try {
// inOne = new FileInputStream(file);
// inTwo = new ObjectInputStream(inOne);
// 基本信息表 = (Hashtable) inTwo.readObject();// 首先到文件中读取HashTable对象
// inOne.close();
// inTwo.close();
// } catch (FileNotFoundException e1) {
// e1.printStackTrace();
// } catch (IOException e1) {
// e1.printStackTrace();
// } catch (ClassNotFoundException e1) {
// e1.printStackTrace();
// }

StudentOper oper = new StudentOper();
Student student = oper.getStudent(number);

if (student != null) {// 存在信息
录入修改.setEnabled(true);
Student stu = (Student) oper.getStudent(number);
姓名.setText(stu.getName());
专业.setText(stu.getDiscipling());
年级.setText(stu.getGrade());
出生.setText(stu.getBorth());
if (stu.getSex().equals("男")) {
男.setSelected(true);
} else {
女.setSelected(true);
}
} else {
录入修改.setEnabled(false);
String m = "该学号不存在!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);

学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}
} else {
录入修改.setEnabled(false);
String m = "你必须输入学号!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);
学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}
} else if (e.getSource() == 录入修改) {
String number = 学号.getText();

if (number.length() > 0) {

// try {
// inOne = new FileInputStream(file);
// inTwo = new ObjectInputStream(inOne);
// 基本信息表 = (Hashtable) inTwo.readObject();// 首先到文件中读取HashTable对象
// inOne.close();
// inTwo.close();
// } catch (FileNotFoundException e1) {
// e1.printStackTrace();
// } catch (IOException e1) {
// e1.printStackTrace();
// } catch (ClassNotFoundException e1) {
// e1.printStackTrace();
// }

StudentOper oper = new StudentOper();
Student student = oper.getStudent(number);

if (student != null) {// 存在信息
String question = "该生基本信息已经存在,你想修改它吗?";
JOptionPane.showMessageDialog(this, question, "警告",
JOptionPane.QUESTION_MESSAGE);

// 弹出确认信息
String m = "该生的信息将会被修改!";
int ok = JOptionPane.showConfirmDialog(this, m, "确认",
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);

if (ok == JOptionPane.YES_OPTION) {
String name = 姓名.getText();
String discipling = 专业.getText();
String grade = 年级.getText();
String borth = 出生.getText();
String sex = null;
if (男.isSelected()) {
sex = 男.getText();
} else {
sex = 女.getText();
}

Student 学生 = new Student();
学生.setName(name);
学生.setDiscipling(discipling);
学生.setBorth(borth);
学生.setSex(sex);
学生.setGrade(grade);

// 保存到数据库中
boolean flag = oper.updateStudent(学生);
if (flag) {
String success = "数据修改成功!";
JOptionPane.showMessageDialog(this, success,
"成功啦!", JOptionPane.OK_OPTION);
} else {
String fail = "数据修改失败!";
JOptionPane.showMessageDialog(this, fail, "失败啦!",
JOptionPane.CANCEL_OPTION);
}

// // 修改后的信息存入散列文件中
// try {
// outOne = new FileOutputStream(file);
// outTwo = new ObjectOutputStream(outOne);
// 基本信息表.put(number, 学生);
// outTwo.close();
// outOne.close();
// 学号.setText(null);
// 姓名.setText(null);
// 专业.setText(null);
// 年级.setText(null);
// 出生.setText(null);
// } catch (FileNotFoundException e1) {
// System.out.println(e1);
// e1.printStackTrace();
// } catch (IOException e1) {
// System.out.println(e1);
// e1.printStackTrace();
// }
录入修改.setEnabled(false);
} else if (ok == JOptionPane.CANCEL_OPTION) {
录入修改.setEnabled(true);
}
} else {
录入修改.setEnabled(false);
String m = "没有该学生基本信息,不能修改!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);

学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}
} else {
录入修改.setEnabled(false);
String m = "你必须输入学号!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);
}
}

if (e.getSource() == 重置) {
学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}
}

}




package com.hejianjiao.student.oper;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.ButtonGroup;
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.JRadioButton;
import javax.swing.JTextField;

import com.hejianjiao.database.hibernate.StudentOper;
import com.hejianjiao.student.entity.Student;

/**
* 学生信息查询
*/
public class Inquest extends JDialog implements ActionListener {

// Hashtable 基本信息表 = null;
JTextField 学号, 姓名, 专业, 年级, 出生;
JRadioButton 男, 女;
JButton 查询;
ButtonGroup group = null;

// FileInputStream inOne = null;
// ObjectInputStream inTwo = null;
// File file = null;

public Inquest(JFrame f) {
super(f, "查询对话框", false);
// this.file = file;
学号 = new JTextField(10);
姓名 = new JTextField(10);
专业 = new JTextField(10);
年级 = new JTextField(10);
出生 = new JTextField(10);
查询 = new JButton("查询");

学号.addActionListener(this);
查询.addActionListener(this);

姓名.setEditable(false);
专业.setEditable(false);
年级.setEditable(false);
出生.setEditable(false);

男 = new JRadioButton("男", false);
女 = new JRadioButton("女", false);
group = new ButtonGroup();
group.add(男);
group.add(女);

Box box1 = Box.createHorizontalBox();// 创建6个水平Box容器,从左到右
box1.add(new JLabel("输入要查询的学号:", JLabel.CENTER));
box1.add(学号);
box1.add(查询);
Box box2 = Box.createHorizontalBox();
box2.add(new JLabel("姓名:", JLabel.CENTER));
box2.add(姓名);
Box box3 = Box.createHorizontalBox();
box3.add(new JLabel("性别:", JLabel.CENTER));
box3.add(男);
box3.add(女);
Box box4 = Box.createHorizontalBox();
box4.add(new JLabel("专业:", JLabel.CENTER));
box4.add(专业);
Box box5 = Box.createHorizontalBox();
box5.add(new JLabel("年级:", JLabel.CENTER));
box5.add(年级);
Box box6 = Box.createHorizontalBox();
box6.add(new JLabel("出生:", JLabel.CENTER));
box6.add(出生);

Box boxH = Box.createVerticalBox();// 创建一个垂直的box容器,其中从上向下排列6个水平Box容器

boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());// 垂直box容器内的组件靠上对齐

JPanel pCenter = new JPanel();
pCenter.add(boxH);
Container con = getContentPane();
con.add(pCenter, BorderLayout.CENTER);
con.validate();
setVisible(false);
setBounds(100, 200, 360, 270);

this.addWindowFocusListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
}

public void actionPerformed(ActionEvent e) {
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);

if (e.getSource() == 查询 || e.getSource() == 学号) {
String number = "";
number = 学号.getText();

if (number.length() > 0) {

// try {
// inOne = new FileInputStream(file);
// inTwo = new ObjectInputStream(inOne);
// 基本信息表 = (Hashtable) inTwo.readObject();// 首先到文件中读取HashTable对象
// inOne.close();
// inTwo.close();
// } catch (FileNotFoundException e1) {
// e1.printStackTrace();
// } catch (IOException e1) {
// e1.printStackTrace();
// } catch (ClassNotFoundException e1) {
// e1.printStackTrace();
// }

StudentOper oper = new StudentOper();
Student student = oper.getStudent(number);

if (student != null) {// 存在信息
姓名.setText(student.getName());
专业.setText(student.getDiscipling());
年级.setText(student.getGrade());
出生.setText(student.getBorth());
if (student.getSex().equals("男")) {
男.setSelected(true);
} else {
女.setSelected(true);
}
} else {
String m = "该学号不存在!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);
}

} else {
String m = "你必须输入学号!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);
}
}
}

}




package com.hejianjiao.student.oper;

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

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.hejianjiao.database.hibernate.StudentOper;
import com.hejianjiao.student.entity.Student;

/**
* 学生信息删除
* */
public class Delete extends JPanel implements ActionListener {

// Hashtable 基本信息表 = null;
JTextField 学号, 姓名, 专业, 年级, 出生;
JRadioButton 男, 女;
JButton 删除;
ButtonGroup group = null;

// FileInputStream inOne = null;
// ObjectInputStream inTwo = null;
// FileOutputStream outOne = null;
// ObjectOutputStream outTwo = null;
//
// File file = null;

public Delete() {
// this.file = file;
学号 = new JTextField(10);
姓名 = new JTextField(10);
专业 = new JTextField(10);
年级 = new JTextField(10);
出生 = new JTextField(10);

删除 = new JButton("删除");

姓名.setEditable(false);
专业.setEditable(false);
年级.setEditable(false);
出生.setEditable(false);

group = new ButtonGroup();

男 = new JRadioButton("男");
女 = new JRadioButton("女");
group.add(女);
group.add(男);

学号.addActionListener(this);
删除.addActionListener(this);

Box box1 = Box.createHorizontalBox();// 创建6个水平Box容器
box1.add(new JLabel("输入要删除的学号:", JLabel.CENTER));
box1.add(学号);
box1.add(删除);
Box box2 = Box.createHorizontalBox();
box2.add(new JLabel("姓名:", JLabel.CENTER));
box2.add(姓名);
Box box3 = Box.createHorizontalBox();
box3.add(new JLabel("性别:", JLabel.CENTER));
box3.add(男);
box3.add(女);
Box box4 = Box.createHorizontalBox();
box4.add(new JLabel("专业:", JLabel.CENTER));
box4.add(专业);
Box box5 = Box.createHorizontalBox();
box5.add(new JLabel("年级:", JLabel.CENTER));
box5.add(年级);
Box box6 = Box.createHorizontalBox();
box6.add(new JLabel("出生:", JLabel.CENTER));
box6.add(出生);

Box boxH = Box.createVerticalBox();// 创建一个垂直的box容器,其中从上向下排列6个水平Box容器

boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());// 垂直box容器内的组件靠上对齐

JPanel pCenter = new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter, BorderLayout.CENTER);
validate();
}

public void actionPerformed(ActionEvent e) {
// 用户点击删除
if (e.getSource() == 删除 || e.getSource() == 学号) {
String number = 学号.getText();

if (number.length() > 0) {
// try {
// inOne = new FileInputStream(file);
// inTwo = new ObjectInputStream(inOne);
// 基本信息表 = (Hashtable) inTwo.readObject();// 首先到文件中读取HashTable对象
// inOne.close();
// inTwo.close();
// } catch (FileNotFoundException e1) {
// e1.printStackTrace();
// } catch (IOException e1) {
// e1.printStackTrace();
// } catch (ClassNotFoundException e1) {
// e1.printStackTrace();
// }
StudentOper oper = new StudentOper();
Student student = oper.getStudent(number);

if (student != null) {// 存在信息
姓名.setText(student.getName());
专业.setText(student.getDiscipling());
年级.setText(student.getGrade());
出生.setText(student.getBorth());
if (student.getSex().equals("男")) {
男.setSelected(true);
} else {
女.setSelected(true);
}

String m = "确定要删除该学生的全部信息吗!";
int ok = JOptionPane.showConfirmDialog(this, m, "确认",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (ok == JOptionPane.YES_OPTION) {
boolean flag = oper.deleteStudent(number);
if (flag) {
String success = "数据删除成功!";
JOptionPane.showMessageDialog(this, success,
"成功啦!", JOptionPane.OK_OPTION);
} else {
String fail = "数据删除失败!";
JOptionPane.showMessageDialog(this, fail, "失败啦!",
JOptionPane.CANCEL_OPTION);
}
// try {
// outOne = new FileOutputStream(file);
// outTwo = new ObjectOutputStream(outOne);
// outOne.close();
// outTwo.close();
// 学号.setText(null);
// 姓名.setText(null);
// 专业.setText(null);
// 年级.setText(null);
// 出生.setText(null);
// } catch (FileNotFoundException e1) {
// e1.printStackTrace();
// } catch (IOException e1) {
// e1.printStackTrace();
// }

} else if (ok == JOptionPane.NO_OPTION) {
学号.setText(null);
姓名.setText(null);
专业.setText(null);
年级.setText(null);
出生.setText(null);
}

} else {
// 如果不存在该生
String m = "该学号不存在!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);
}
} else {
String m = "你必须输入学号!";
JOptionPane.showMessageDialog(this, m, "警告",
JOptionPane.WARNING_MESSAGE);
}
}
}
}




package com.hejianjiao.student.entity;

/**
* 学生实体
* */
public class Student {

private String id;
private String name;
private String discipling;// 专业
private String grade;// 年级
private String borth;// 出生
private String sex;

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 getDiscipling() {
return discipling;
}

public void setDiscipling(String discipling) {
this.discipling = discipling;
}

public String getGrade() {
return grade;
}

public void setGrade(String grade) {
this.grade = grade;
}

public String getBorth() {
return borth;
}

public void setBorth(String borth) {
this.borth = borth;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

}




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hejianjiao.student.entity.Student" table="student">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property column="name" name="name"/>
<property column="discipling" name="discipling"/>
<property column="grade" name="grade"/>
<property column="borth" name="borth"/>
<property column="sex" name="sex"/>
</class>
</hibernate-mapping>



package com.hejianjiao.database.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.hejianjiao.student.entity.Student;

/**
* 实体操作类
* */
public class StudentOper {

public Session getCurrentSession() {
return HibernateSessionFactory.getSession();
}

public void closeSession() {
HibernateSessionFactory.closeSession();
}

public boolean saveStudent(Student student) {
try {
this.getCurrentSession().beginTransaction();
this.getCurrentSession().save(student);
this.getCurrentSession().beginTransaction().commit();
return true;
} catch (HibernateException e) {
this.getCurrentSession().getTransaction().rollback();
e.printStackTrace();
return false;
} finally {
this.closeSession();
}
}

public boolean updateStudent(Student student) {
try {
this.getCurrentSession().beginTransaction();
this.getCurrentSession().update(student);
this.getCurrentSession().beginTransaction().commit();
return true;
} catch (HibernateException e) {
this.getCurrentSession().getTransaction().rollback();
e.printStackTrace();
return false;
} finally {
this.closeSession();
}
}

public boolean deleteStudent(String id) {
this.getCurrentSession().beginTransaction();
Student student = this.getStudent(id);
if (student != null) {
try {
this.getCurrentSession().delete(student);
this.getCurrentSession().beginTransaction().commit();
return true;
} catch (HibernateException e) {
this.getCurrentSession().getTransaction().rollback();
e.printStackTrace();
}
}
return false;
}

public Student getStudent(String id) {
return (Student) this.getCurrentSession().get(Student.class, id);
}

public List getStudent() {
List list = this.getCurrentSession().createQuery("from Student").list();
return list;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值