学生信息管理系统总共有6个类,这6个类的作用已在24.1.3小节中做过介绍,此处不再赘述,以下是这6个类的源代码,读者也能在本书提供的源代码文件夹中直接下载它们。
Student.java
public class Student {
private String sid;//学号
private String name;//姓名
private String sex;//性别
private String classId;//班级
private String phone;//家长手机号
private String info;//备注信息
//构造方法
public Student() {
}
public Student(String sid, String name, String sex, String classId, String phone, String info) {
this.sid = sid;
this.name = name;
this.sex = sex;
this.classId = classId;
this.phone = phone;
this.info = info;
}
//get()和set()方法
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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 getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
DBUtil.java
import java.sql.*;
public class DBUtil {
static String driverName = "com.mysql.cj.jdbc.Driver";//驱动类
static String url = "jdbc:mysql://localhost:3306/student";//数据库url
static String user = "root";//用户名
static String password = "123456";//密码
static Connection con = null;//连接
//静态块-加载驱动
static {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//建立连接
public static Connection getConnection() throws SQLException {
con = DriverManager.getConnection(url, user, password);
return con;
}
//释放资源
public static void close(ResultSet rs, Statement stm, Connection con) throws SQLException {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (con != null) {
con.close();
}
}
}
StudentDao.java
import java.sql.*;
import java.util.ArrayList;
public class StudentDao {
// 查询,查询结果以学号正序排列
public static ArrayList<Student> query(String colName, String keywords, int page)
throws Exception {
ArrayList<Student> list = new ArrayList<Student>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
String sql;
if (null == keywords || "".equals(keywords.trim())) {
sql = "select * from student order by sid";
} else {
sql = "select * from student where " + colName + " like '%"
+ keywords + "%' order by sid";
}
pst = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = pst.executeQuery();
rs.last();
int rowCount = rs.getRow();
if (rowCount > 0) {
rs.absolute((page - 1) * 10 + 1);
int i = 0;
while (i < 10 && !rs.isAfterLast()) {
Student student = makeObject(rs);
list.add(student);
i++;
rs.next();
}
}
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(rs, pst, con);
}
return list;
}
// 计算满足条件的学生数量有多少
public static int getStudentNum(String colName, String keywords)
throws Exception {
int num = 0;
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
String sql;
sql = "select count(*) from student";
if (null != keywords && !"".equals(keywords)) {// 如果关键字不为空则再加条件
sql = sql + " where " + colName +
" like '%" + keywords + "%' ";
}
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
num = rs.getInt(1);
}
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(rs, pst, con);
}
return num;
}
// 按学号查询单个学生信息
public static Student queryBySid(String sid) throws Exception {
Student student = null;
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
pst = con.prepareStatement("select * from student where sid=?");
pst.setString(1, sid);
rs = pst.executeQuery();
if (rs.next()) {// 如果查到数据则把数据包装成一个对象
student = makeObject(rs);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, pst, con);
}
return student;
}
// 插入学生信息
public static boolean insert(Student student) throws Exception {
boolean flag = true;
Connection con = null;
PreparedStatement pst = null;
if (isStudentExists(student.getSid())) {
return false;
}
try {
con = DBUtil.getConnection();
String sql = "insert into student(sid,name,class_id,sex,phone,info) " +
"values (?,?,?,?,?,?)";
pst = con.prepareStatement(sql);
pst.setString(1, student.getSid());
pst.setString(2, student.getName());
pst.setString(3, student.getClassId());
pst.setString(4, student.getSex());
pst.setString(5, student.getPhone());
pst.setString(6, student.getInfo());
pst.executeUpdate();
} catch (SQLException e) {
flag = false;
throw e;
} finally {
DBUtil.close(null, pst, con);
}
return flag;
}
// 判断学号是否重复
public static boolean isStudentExists(String sid) {
boolean isExists = false;
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
String sql = "select * from student where sid=?";
pst = con.prepareStatement(sql);
pst.setString(1, sid);
rs = pst.executeQuery();
if (rs.next()) {
isExists = true;
} else {
isExists = false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DBUtil.close(rs, pst, con);
} catch (Exception e) {
e.printStackTrace();
}
}
return isExists;
}
// 修改学生信息(学号不能修改)
public static boolean update(Student student) throws Exception {
boolean isSuccess = false;
Connection con = null;
PreparedStatement pst = null;
try {
con = DBUtil.getConnection();
String sql = "update student set name=?,class_id=?, sex=?," +
"phone=?,info=? where sid=?";
pst = con.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setString(2, student.getClassId());
pst.setString(3, student.getSex());
pst.setString(4, student.getPhone());
pst.setString(5, student.getInfo());
pst.setString(6, student.getSid());
pst.executeUpdate();
isSuccess = true;
} catch (SQLException e) {
isSuccess = false;
throw e;
} finally {
DBUtil.close(null, pst, con);
}
return isSuccess;
}
// 删除一条学生信息
public static boolean deleteBySid(String sid) throws Exception {
boolean flag = true;
Connection con = null;
PreparedStatement pst = null;
try {
con = DBUtil.getConnection();
pst = con.prepareStatement("delete from student where sid=?");
pst.setString(1, sid);
pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
flag = false;
throw e;
} finally {
DBUtil.close(null, pst, con);
}
return flag;
}
//把一条数据包装为Student对象
private static Student makeObject(ResultSet rs) throws Exception {
// 控制游标不出范围
Student student = new Student();
String sid = rs.getString("sid");
String name = rs.getString("name");
String classId = rs.getString("class_id");
String sex = rs.getString("sex");
String phone = rs.getString("phone");
String info = rs.getString("info");
student.setClassId(classId);
student.setSid(sid);
student.setName(name);
student.setSex(sex);
student.setPhone(phone);
student.setInfo(info);
return student;
}
}
MainFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
public class MainFrame extends JFrame {
JTable table;//数据表格
JScrollPane scrollPane;//表格所在的滚动面板
Object[] columns;//表格列名称
DefaultTableModel data;//表格的数据模型
JComboBox<String> jbcKeywords;// 查询关键字组合框
JButton btnQuery;//查询按钮
JButton btnAdd;//新增按钮
JButton btnDelete;//删除按钮
JButton btnUpdate;//更新按钮
JButton btnDetail;//信息详情按钮
JButton btnFirst;// 首页按钮
JButton btnPreview;// 上一页按钮
JButton btnNext;// 下一页按钮
JButton btnLast;// 末页按钮
JTextField txtKeywords;// 查询关键字文本框
JLabel lblPage;// 页码标签
int curPage = 1, pageCount = 1;// 当前页和总页数
public MainFrame() {
init();
}
private void init() {
Container container = this.getContentPane();
container.setLayout(null);
// 创建组件对象
jbcKeywords = new JComboBox<String>();
txtKeywords = new JTextField();
columns = new Object[]{"学号", "姓名", "班级", "性别", "家长手机号"};
data = new DefaultTableModel(columns, 0) {
//设置表格单元格不可编辑
public boolean isCellEditable(int row, int column) {
return false;
}
};
table = new JTable(data);
scrollPane = new JScrollPane(table);
btnQuery = new JButton("查询");
btnAdd = new JButton("新增");
btnDelete = new JButton("删除");
btnUpdate = new JButton("修改");
btnDetail = new JButton("详情");
btnFirst = new JButton("首页");
btnPreview = new JButton("上页");
btnNext = new JButton("下页");
btnLast = new JButton("末页");
lblPage = new JLabel("", JLabel.CENTER);// 文字居中显示的标签
// 设置各组件属性
jbcKeywords.setSize(100, 30);
jbcKeywords.setLocation(125, 20);
jbcKeywords.setFont(new Font("微软雅黑", Font.PLAIN, 20));
jbcKeywords.addItem("姓名");
jbcKeywords.addItem("性别");
jbcKeywords.addItem("班级");
txtKeywords.setSize(150, 30);
txtKeywords.setLocation(235, 20);
txtKeywords.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnQuery.setSize(80, 30);
btnQuery.setLocation(395, 20);
btnQuery.setFont(new Font("微软雅黑", Font.PLAIN, 20));
table.setRowHeight(28);
table.setFont(new Font("微软雅黑", Font.PLAIN, 15));
//设置表格只能被选中一行(单选模式)
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getTableHeader().setFont(new Font("微软雅黑", Font.PLAIN, 18));
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(1).setPreferredWidth(100);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(50);
table.getColumnModel().getColumn(4).setPreferredWidth(150);
scrollPane.setSize(500, 311);
scrollPane.setLocation(50, 60);
btnAdd.setSize(90, 30);
btnAdd.setLocation(100, 375);
btnAdd.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnDelete.setSize(90, 30);
btnDelete.setLocation(200, 375);
btnDelete.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnUpdate.setSize(90, 30);
btnUpdate.setLocation(300, 375);
btnUpdate.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnDetail.setSize(90, 30);
btnDetail.setLocation(400, 375);
btnDetail.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnFirst.setSize(90, 30);
btnFirst.setLocation(50, 420);
btnFirst.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnPreview.setSize(90, 30);
btnPreview.setLocation(150, 420);
btnPreview.setFont(new Font("微软雅黑", Font.PLAIN, 20));
lblPage.setSize(100, 30);
lblPage.setLocation(250, 420);
lblPage.setHorizontalTextPosition(JLabel.CENTER);
lblPage.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnNext.setSize(90, 30);
btnNext.setLocation(360, 420);
btnNext.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnLast.setSize(90, 30);
btnLast.setLocation(460, 420);
btnLast.setFont(new Font("微软雅黑", Font.PLAIN, 20));
// 添加监听器
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 如果curPage等于0,在查询之前必须重新赋值为1
if (curPage == 0) {
curPage = 1;
}
showInfo(curPage);
}
});
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DetailDialog dialog = new DetailDialog(DetailDialog.ADD, "");
if (dialog.isUpdate == true) {// 如果数据有更新
// 查询最新数据,并去除查询条件及设置查询第1页的数据
curPage = 1;
txtKeywords.setText("");
showInfo(curPage);
}
}
});
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
try {
if (row >= 0) {
int r = JOptionPane.showConfirmDialog(MainFrame.this,
"确定要删除该学生信息?");
if (r == JOptionPane.OK_OPTION) {//如果单击确定按钮
String sid = (String) table.getValueAt(row, 0);//获得学号
StudentDao.deleteBySid(sid);
String colName = "";
String keywords = txtKeywords.getText();
//如果有查询关键字则转换出相应列名称
if (keywords.trim().length() != 0) {
colName = getColName();
}
//计算删除一条数据后总共剩余多少条数据
int num = StudentDao.getStudentNum(colName, keywords);
if (num == 0) {//如果删掉之后再无数据
data.removeRow(row);//
curPage = 0;
pageCount = 0;
lblPage.setText(curPage + "/" + pageCount);
} else if (data.getRowCount() == 1) {//本页仅一条数据
curPage--;//当前页减1(即回到上一页)
showInfo(curPage);//查询上页数据
} else {
showInfo(curPage);//查询本页数据
}
JOptionPane.showMessageDialog(MainFrame.this,
"删除成功!");
}
} else {
JOptionPane.showMessageDialog(MainFrame.this,
"请选择要删除的学生!");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(MainFrame.this,
"因系统错误删除失败!");
ex.printStackTrace();
}
}
});
btnUpdate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row >= 0) {
String sid = (String) table.getValueAt(row, 0);//获得学号
DetailDialog dialog = new DetailDialog(DetailDialog.UPDATE, sid);
if (dialog.isUpdate == true) {// 如果数据有更新
Student student = dialog.getStudent();//获取更新后的对象
//把更新后对象的各项信息刷新到表格的各单元格中
table.setValueAt(student.getName(), row, 1);
table.setValueAt(student.getClassId(), row, 2);
table.setValueAt(student.getSex(), row, 3);
table.setValueAt(student.getPhone(), row, 4);
}
} else {
JOptionPane.showMessageDialog(MainFrame.this,
"请选择要修改的学生!");
}
}
});
btnDetail.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row >= 0) {
String sid = (String) table.getValueAt(row, 0);//获得学号
DetailDialog dialog = new DetailDialog(DetailDialog.DETAIL, sid);
} else {
JOptionPane.showMessageDialog(MainFrame.this,
"请选择要查看的学生!");
}
}
});
btnFirst.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (curPage == 0) {
JOptionPane.showMessageDialog(MainFrame.this,
"没有数据,不能翻页!");
} else if (curPage == 1) {//已经在第一页的情况下
JOptionPane.showMessageDialog(MainFrame.this,
"已经是第一页!");
} else {
curPage = 1;//设置当前页为第一页
showInfo(curPage);//查询
}
}
});
btnPreview.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (curPage == 0) {
JOptionPane.showMessageDialog(MainFrame.this,
"没有数据,不能翻页!");
} else if (curPage == 1) {//已经在第一页的情况下
JOptionPane.showMessageDialog(MainFrame.this,
"已经是第一页!");
} else {
curPage--;//设置当前页为上一页
showInfo(curPage);//查询
}
}
});
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (curPage == 0) {
JOptionPane.showMessageDialog(MainFrame.this,
"没有数据,不能翻页!");
} else if (curPage == pageCount) {//已经在最后一页的情况下
JOptionPane.showMessageDialog(MainFrame.this,
"已经是最后一页!");
} else {
curPage++;
showInfo(curPage);
}
}
});
btnLast.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (curPage == 0) {
JOptionPane.showMessageDialog(MainFrame.this,
"没有数据,不能翻页!");
} else if (curPage == pageCount) {//已经在最后一页的情况下
JOptionPane.showMessageDialog(MainFrame.this,
"已经是最后一页!");
} else {
curPage = pageCount;//设置当前页为最后一页
showInfo(curPage);//查询
}
}
});
container.add(jbcKeywords);
container.add(btnQuery);
container.add(txtKeywords);
container.add(scrollPane);
container.add(btnAdd);
container.add(btnDelete);
container.add(btnUpdate);
container.add(btnDetail);
container.add(btnFirst);
container.add(btnPreview);
container.add(lblPage);
container.add(btnNext);
container.add(btnLast);
showInfo(1);// 进行默认查询
}
//查询第page页数据
private void showInfo(int page) {
String colName = "";
String keywords = txtKeywords.getText();
//如果有查询关键字则转换出相应列名称
if (keywords.trim().length() != 0) {
colName = getColName();
}
try {
ArrayList<Student> list = StudentDao.query(colName, keywords, page);
int num = StudentDao.getStudentNum(colName, keywords);
//计算总页数
if (num % 10 == 0) {
pageCount = num / 10;
} else {
pageCount = num / 10 + 1;
}
// 如果总页数为0,当前页也赋值为0
if (pageCount == 0) {
curPage = 0;
}
setData(list);// 把查询到的数据装入表格
lblPage.setText(curPage + "/" + pageCount);
} catch (Exception e) {
e.printStackTrace();
}
}
// 把数据装入表格模型
private void setData(ArrayList<Student> list) {
int rowCount = data.getRowCount();// 获得原模型中行的数量
for (int i = rowCount - 1; i >= 0; i--) {
data.removeRow(i);
}
for (Student student : list) {
String sid = student.getSid();
String name = student.getName();
String sex = student.getSex();
String classId = student.getClassId();
String phone = student.getPhone();
String info = student.getInfo();
data.addRow(new Object[]{sid, name, classId, sex, phone});
}
}
String getColName() {
String colName = "";
String selectedItem = jbcKeywords.getSelectedItem().toString();
switch (selectedItem) {
case "姓名":
colName = "name";
break;
case "性别":
colName = "sex";
break;
case "班级":
colName = "class_id";
break;
}
return colName;
}
}
DetailDialog.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DetailDialog extends JDialog {
//界面所显示的对象
Student student = new Student();
//定义打开模式
public static final int ADD = 1;//新增模式
public static final int UPDATE = 2;//修改模式
public static final int DETAIL = 3;//详情模式
//当前窗体对象的模式
private int mode;
//关闭窗口时是否单击了取消按钮
boolean isUpdate = false;//是否更新了数据库
//定义组件
JLabel lblSid;//学号标签
JTextField txtSid;//学号文本框
JLabel lblName;//姓名标签
JTextField txtName;//姓名文本框
JLabel lblSex;//性别标签
JRadioButton rbtMale;//单选按钮男
JRadioButton rbtFemale;//单选按钮女
ButtonGroup group;//按钮组
JLabel lblClassId;//班级标签
JTextField txtClassId;//班级文本框
JLabel lblPhone;//家长手机标签
JTextField txtPhone;//家长手机文本框
JLabel lblInfo;//备注信息标签
JTextArea txaInfo;//备注信息文本区
JScrollPane scrollPane;//备注文本区的滚动面板
JButton btnOK;//确定按钮
JButton btnCancel;//取消按钮
public DetailDialog(int mode, String sid) {
init(mode, sid);
}
private void init(int mode, String sid) {
this.mode = mode;
String title = "";
switch (mode) {
case ADD:
title = "新增学生信息";
break;
case UPDATE:
title = "修改学生信息";
break;
case DETAIL:
title = "学生信息详情";
break;
}
Container container = this.getContentPane();
container.setLayout(null);
lblSid = new JLabel("学号");
txtSid = new JTextField();
lblName = new JLabel("姓名");
txtName = new JTextField();
lblSex = new JLabel("性别");
rbtMale = new JRadioButton("男");
rbtFemale = new JRadioButton("女");
group = new ButtonGroup();
group.add(rbtMale);
group.add(rbtFemale);
lblClassId = new JLabel("班级");
txtClassId = new JTextField();
lblPhone = new JLabel("手机");
txtPhone = new JTextField();
lblInfo = new JLabel("备注信息(不超过50字)");
txaInfo = new JTextArea();
scrollPane = new JScrollPane(txaInfo);
btnOK = new JButton("确定");
btnCancel = new JButton("取消");
//设置组件属性
lblSid.setSize(50, 25);
lblSid.setLocation(50, 20);
lblSid.setFont(new Font("微软雅黑", Font.PLAIN, 18));
txtSid.setSize(250, 25);
txtSid.setLocation(100, 20);
txtSid.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblName.setSize(50, 25);
lblName.setLocation(50, 50);
lblName.setFont(new Font("微软雅黑", Font.PLAIN, 18));
txtName.setSize(250, 25);
txtName.setLocation(100, 50);
txtName.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblSex.setSize(50, 25);
lblSex.setLocation(50, 80);
lblSex.setFont(new Font("微软雅黑", Font.PLAIN, 18));
rbtMale.setSize(50, 25);
rbtMale.setLocation(100, 80);
rbtMale.setFont(new Font("微软雅黑", Font.PLAIN, 18));
rbtFemale.setSize(50, 25);
rbtFemale.setLocation(150, 80);
rbtFemale.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblClassId.setSize(50, 25);
lblClassId.setLocation(50, 110);
lblClassId.setFont(new Font("微软雅黑", Font.PLAIN, 18));
txtClassId.setSize(250, 25);
txtClassId.setLocation(100, 110);
txtClassId.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblPhone.setSize(50, 25);
lblPhone.setLocation(50, 140);
lblPhone.setFont(new Font("微软雅黑", Font.PLAIN, 18));
txtPhone.setSize(250, 25);
txtPhone.setLocation(100, 140);
txtPhone.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblInfo.setSize(250, 25);
lblInfo.setLocation(50, 170);
lblInfo.setFont(new Font("微软雅黑", Font.PLAIN, 18));
txaInfo.setFont(new Font("微软雅黑", Font.PLAIN, 15));
scrollPane.setSize(300, 100);
scrollPane.setLocation(50, 200);
scrollPane.setFont(new Font("微软雅黑", Font.PLAIN, 18));
if (mode != ADD) {//如果不是新增模式,则把信息详情反写入相应文本框
try {
student = StudentDao.queryBySid(sid);
this.txtSid.setText(student.getSid());//反写学号
this.txtSid.setEditable(false);//学号只读
this.txtName.setText(student.getName());//反写姓名
this.txtClassId.setText(student.getClassId());//反写班级
this.txtPhone.setText(student.getPhone());//反写手机号
this.txaInfo.setText(student.getInfo());//反写备注
if ("男".equals(student.getSex())) {//反选性别
this.rbtMale.setSelected(true);
} else {
this.rbtFemale.setSelected(true);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(DetailDialog.this,
"因系统错误获取信息失败!");
e.printStackTrace();
}
}
//如果是详情模式则设置组件为不可编辑状态
if (mode == DETAIL) {
this.txtName.setEditable(false);
this.txtClassId.setEditable(false);
this.txtPhone.setEditable(false);
this.txaInfo.setEnabled(false);
this.rbtMale.setEnabled(false);
this.rbtFemale.setEnabled(false);
}
btnOK.setSize(80, 25);
btnOK.setLocation(100, 315);
btnOK.setFont(new Font("微软雅黑", Font.PLAIN, 18));
btnOK.addActionListener(new ActionListener() {
//新增或修改成功
public void operateSuccess(String message) {
JOptionPane.showMessageDialog(DetailDialog.this, message);
isUpdate = true;//表示数据有更新
DetailDialog.this.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
Student student = checkData();
if (mode == ADD) {//如果是新增
if (student != null) {
try {
StudentDao.insert(student);
operateSuccess("新增成功!");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(DetailDialog.this,
"因系统错误新增信息失败!");
}
}
} else if (mode == UPDATE) {//修改模式
try {
if (student != null) {
int r = JOptionPane.showConfirmDialog(DetailDialog.this,
"确定要修改该学生信息?");
if (r == JOptionPane.OK_OPTION) {
StudentDao.update(student);
DetailDialog.this.student = student;//更新对象
operateSuccess("修改成功!");
}
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(DetailDialog.this,
"因系统错误修改信息失败!");
}
} else if (mode == UPDATE) {
DetailDialog.this.dispose();
}
}
});
btnCancel.setSize(80, 25);
btnCancel.setLocation(220, 315);
btnCancel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DetailDialog.this.dispose();
}
});
container.add(lblSid);
container.add(txtSid);
container.add(lblName);
container.add(txtName);
container.add(lblSex);
container.add(rbtMale);
container.add(rbtFemale);
container.add(lblClassId);
container.add(txtClassId);
container.add(lblPhone);
container.add(txtPhone);
container.add(lblInfo);
container.add(scrollPane);
container.add(btnOK);
container.add(btnCancel);
//显示窗体
this.setTitle(title);
this.setModal(true);
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/*检查数据的合理性
如果数据合理则返回组装好的Student对象,否则返回null
*/
private Student checkData() {
String sid = txtSid.getText();
String name = txtName.getText();
String sex = "";
String classId = txtClassId.getText();
String phone = txtPhone.getText();
String info = txaInfo.getText();
if (sid.length() == 0) {//如果学号为空
JOptionPane.showMessageDialog(DetailDialog.this,
"学号不能为空!");
return null;
}
if (sid.length() >= 10) {//如果学号长度超过10
JOptionPane.showMessageDialog(DetailDialog.this,
"学号长度不能超过10!");
return null;
}
if (mode == ADD) {//如果是新增学生信息,则检查学号重复性
if (StudentDao.isStudentExists(sid.trim())) {
JOptionPane.showMessageDialog(DetailDialog.this,
"学号出现重复,不能新增!");
return null;
}
}
if (name.length() == 0) {//如果姓名为空
JOptionPane.showMessageDialog(DetailDialog.this,
"姓名不能为空!");
return null;
}
if (name.length() >= 10) {//如果姓名长度超过10
JOptionPane.showMessageDialog(DetailDialog.this,
"姓名长度不能超过10!");
return null;
}
//如果没有选择性别
if (rbtMale.isSelected() == false && rbtFemale.isSelected() == false) {
JOptionPane.showMessageDialog(DetailDialog.this,
"请选择性别!");
return null;
} else {//选择了性别的情况下把所选性别赋值给sex
if (rbtMale.isSelected()) {
sex = rbtMale.getText();
} else {
sex = rbtFemale.getText();
}
}
if (classId.length() == 0) {//如果班级为空
JOptionPane.showMessageDialog(DetailDialog.this,
"班级不能为空!");
return null;
}
if (classId.length() >= 10) {//如果班级长度超过10
JOptionPane.showMessageDialog(DetailDialog.this,
"班级长度不能超过10!");
return null;
}
if (phone.length() != 11) {//如果手机号不是11位
JOptionPane.showMessageDialog(DetailDialog.this,
"手机号必须是11位!");
return null;
}
if (info.length() >= 50) {//如果备注信息长度超过50
JOptionPane.showMessageDialog(DetailDialog.this,
"备注信息不能超过50个字!");
return null;
}
Student student = new Student();
//把用户填写的数据组装到Student对象中
student.setSid(sid);
student.setName(name);
student.setSex(sex);
student.setClassId(classId);
student.setPhone(phone);
student.setInfo(info);
return student;
}
public Student getStudent() {
return this.student;
}
}
Main.java
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setSize(600, 500);
frame.setLocationRelativeTo(null);
frame.setTitle("学生信息管理系统");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
文字版教程还配有更详细的视频讲解,小伙伴们可以点击这里观看。