Java课程设计---删除学生

1、界面已经在上次修改操作的过程添加完成

2、在StudentDao中添加删除方法

	public boolean delete(int id) throws SQLException {
		DbUtil dbUtil = new DbUtil();
		//sql
		String sql = "delete from tb_student where id ='" + id + "'";
		// 在控制台打印sql语句用于检查
		System.out.println(sql);
		// 处理并返回
		return dbUtil.execute(sql);
	}

3、在StudentService中添加删除用户服务

	
	/**
	 * 根据id删除学生
	 * @param id
	 * @return
	 * @throws SQLException 
	 */
	public boolean deleteStudent(int id) throws SQLException {
		StudentDao studentDao = new StudentDao();
		return studentDao.delete(id);
	}

4、重构代码

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;

/*
 * 项目名称: 
 * 
 * 文件名称为:ShowTable.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
	// 创建服务
	private StudentService service;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ShowStudent frame = new ShowStudent();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * 初始化处理
	 * 
	 * @throws SQLException
	 */
	public void init() throws SQLException {
		// 创建服务
		service = new StudentService();
		// 查询出所有学生
		List<Student> list = service.getStudent();
		// 装载数据
		fillTable(list);
	}

	/**
	 * 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();
		// 将表格加载到滚动面板
		scrollPane.setViewportView(table);
		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.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.setBounds(346, 434, 113, 27);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("删除");
		btnNewButton_1.setBounds(516, 434, 113, 27);
		contentPane.add(btnNewButton_1);
		setVisible(true);
		
		/**
		 * 初始化
		 */
		try {
			init();
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		/******
		 * 事件处理
		 */
		// 表格单击
		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);
				}
			}
		});

		// 处理查找
		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();
				}
			}
		});

		// 处理修改
		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, "修改成功");
						init();
					} else {
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});

		// 处理删除
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取当前单击行
				int row = table.getSelectedRow();
				// 获取这行的ID,注意不是学号
				id = (int) table.getValueAt(row, 0);
				try {
					if (service.deleteStudent(id)) {
						JOptionPane.showMessageDialog(null, "删除成功");
						init();
					} else {
						JOptionPane.showMessageDialog(null, "删除失败");
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
	}

	/**
	 * 表格装载数据
	 * 
	 * @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);
	}
}

  

posted @ 2018-06-22 16:57  大象老师  阅读(726)  评论(0编辑  收藏  举报