Java課程設計---修改學生基本信息

一、修改窗體

 

二、在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);
	}

  

三、在StudentService中增長修改服務

	/**
	 * 修改學生信息服務
	 * 
	 * @param student
	 * @return
	 * @throws SQLException
	 */
	public boolean editStudent(Student student) throws SQLException {
		StudentDao studentDao = new StudentDao();
		return studentDao.update(student);
	}

四、給窗體中的表格添加點擊事件

表格單擊完整代碼

		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);
				}
			}
		});

  

五、提交併處理修改過的內容

			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();
					}
				}
			});

六、截圖

七、完整代碼

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);
	}
}
相關文章
相關標籤/搜索