Java课程设计---修改学生基本信息
1、修改窗体
2、在StudentDao中增加修改学生信息的方法
/** * 修改的方法 * * @param student * @return * @throws SQLException */ public boolean update(Student student) throws SQLException { DbUtil dbUtil = new DbUtil(); String sql = "update tb_student set name='" + student.getName() + "',sno='" + student.getSno() + "',sex='" + student.getSex() + "',classname='" + student.getClassName() + "' where id ='" + student.getId() + "'"; // 在控制台打印sql语句用于检查 System.out.println(sql); // 处理并返回 return dbUtil.execute(sql); }
3、在StudentService中增加修改服务
/** * 修改学生信息服务 * * @param student * @return * @throws SQLException */ public boolean editStudent(Student student) throws SQLException { StudentDao studentDao = new StudentDao(); return studentDao.update(student); }
4、给窗体中的表格添加点击事件
表格单击完整代码
table.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int row = table.getSelectedRow(); textField_1.setText(table.getValueAt(row, 1) + ""); textField_2.setText(table.getValueAt(row, 2) + ""); String sex = table.getValueAt(row, 3) + ""; if ("男".equals(sex)) { boy.setSelected(true); } else if ("女".equals(sex)) { girl.setSelected(true); } } });
5、提交并处理修改过的内容
btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //获取学号 String sno = textField_1.getText().trim(); //获取姓名 String name = textField_2.getText().trim(); //获取性别 String sex = ""; if (boy.isSelected()) { sex = "男"; } else if (girl.isSelected()) { sex = "女"; } //获取班级 String className = comboBox.getSelectedItem().toString(); //说明id已经在前面保存了; //构建窗体 Student student = new Student(id, sno, name, sex, className); try { if (service.editStudent(student)) { JOptionPane.showMessageDialog(null, "修改成功"); } else { JOptionPane.showMessageDialog(null, "修改失败"); } } catch (SQLException e1) { e1.printStackTrace(); } } });
6、截图
7、完整代码
package com.student.view; import java.awt.EventQueue; import java.sql.SQLException; import java.util.List; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import com.student.model.Student; import com.student.service.StudentService; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JRadioButton; import javax.swing.JComboBox; import javax.swing.DefaultComboBoxModel; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; /* * 项目名称: * * 文件名称为:ShowStudent.java * 文件创建人:daxiang * * @author daxiang * @version * @time 2018年6月22日 上午8:03:24 * @copyright daxiang */ public class ShowStudent extends JFrame { private static final long serialVersionUID = 1L; private JPanel contentPane; private JTable table; private JTextField textField; private JLabel lblNewLabel; private JLabel lblNewLabel_1; private JLabel lblNewLabel_2; private JLabel lblNewLabel_3; private JTextField textField_1; private JTextField textField_2; private JRadioButton boy; private JRadioButton girl; private int id;// 保存修改时的ID /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ShowTable frame = new ShowTable(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ShowStudent() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 860, 618); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); // 设置无布局 contentPane.setLayout(null); // 创建滚动面板 JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(37, 89, 739, 263); contentPane.add(scrollPane); // 创建表格 table = new JTable(); table.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int row = table.getSelectedRow(); id = (int) table.getValueAt(row, 0); textField_1.setText(table.getValueAt(row, 1) + ""); textField_2.setText(table.getValueAt(row, 2) + ""); String sex = table.getValueAt(row, 3) + ""; if ("男".equals(sex)) { boy.setSelected(true); } else if ("女".equals(sex)) { girl.setSelected(true); } } }); // 将表格加载到滚动面板 scrollPane.setViewportView(table); try { // 创建服务 StudentService service = new StudentService(); // 查询出所有学生 List<Student> list = service.getStudent(); // 装载数据 fillTable(list); JLabel label = new JLabel("请输入学生姓名:"); label.setBounds(93, 43, 123, 18); contentPane.add(label); textField = new JTextField(); textField.setBounds(242, 40, 169, 24); contentPane.add(textField); textField.setColumns(10); JButton button = new JButton("查 找"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { // 获取查询数据 // textField.getText().trim()获取文本框数据并且去掉空格 List<Student> list1 = service.getStudent(textField .getText().trim()); // 装载到表格 fillTable(list1); } catch (SQLException e1) { e1.printStackTrace(); } } }); button.setBounds(477, 39, 113, 27); contentPane.add(button); lblNewLabel = new JLabel("学 号"); lblNewLabel.setBounds(86, 383, 52, 18); contentPane.add(lblNewLabel); lblNewLabel_1 = new JLabel("姓名"); lblNewLabel_1.setBounds(306, 383, 43, 18); contentPane.add(lblNewLabel_1); lblNewLabel_2 = new JLabel("班 级"); lblNewLabel_2.setBounds(86, 438, 52, 18); contentPane.add(lblNewLabel_2); lblNewLabel_3 = new JLabel("性别"); lblNewLabel_3.setBounds(516, 383, 43, 18); contentPane.add(lblNewLabel_3); textField_1 = new JTextField(); textField_1.setBounds(152, 380, 140, 24); contentPane.add(textField_1); textField_1.setColumns(10); textField_2 = new JTextField(); textField_2.setBounds(363, 380, 123, 24); contentPane.add(textField_2); textField_2.setColumns(10); ButtonGroup buttonGroup = new ButtonGroup(); boy = new JRadioButton("男"); boy.setBounds(571, 379, 52, 27); contentPane.add(boy); girl = new JRadioButton("女"); girl.setBounds(639, 379, 52, 27); contentPane.add(girl); buttonGroup.add(girl); buttonGroup.add(boy); JComboBox<String> comboBox = new JComboBox<String>(); comboBox.setModel(new DefaultComboBoxModel<String>(new String[] { "计科1班", "计科2班", "计科3班", "计科4班" })); comboBox.setBounds(152, 435, 140, 24); contentPane.add(comboBox); JButton btnNewButton = new JButton("修 改"); // 处理修改 btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 获取学号 String sno = textField_1.getText().trim(); // 获取姓名 String name = textField_2.getText().trim(); // 获取性别 String sex = ""; if (boy.isSelected()) { sex = "男"; } else if (girl.isSelected()) { sex = "女"; } // 获取班级 String className = comboBox.getSelectedItem().toString(); // 说明id已经在前面保存了; // 构建窗体 Student student = new Student(id, sno, name, sex, className); try { if (service.editStudent(student)) { JOptionPane.showMessageDialog(null, "修改成功"); } else { JOptionPane.showMessageDialog(null, "修改失败"); } } catch (SQLException e1) { e1.printStackTrace(); } } }); btnNewButton.setBounds(346, 434, 113, 27); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("删除"); btnNewButton_1.setBounds(516, 434, 113, 27); contentPane.add(btnNewButton_1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
setVisible(true); } /** * 表格装载数据 * * @param list */ public void fillTable(List<Student> list) { // 将默认的加载数据去掉,重新定义个表头 String[] head = new String[] { "序号", "学号", "姓名", "性别", "班级" }; // 创建一个二维数组,5表示列数 Object[][] data = new Object[list.size()][5]; // 遍历 for (int i = 0; i < list.size(); i++) { data[i][0] = list.get(i).getId(); data[i][1] = list.get(i).getSno(); data[i][2] = list.get(i).getName(); data[i][3] = list.get(i).getSex(); data[i][4] = list.get(i).getClassName(); } // 将数据和表头封装 DefaultTableModel datamoModel = new DefaultTableModel(data, head); // 将封装好的数据加载 table.setModel(datamoModel); } }