博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java课程设计---修改学生基本信息
阅读量:5021 次
发布时间:2019-06-12

本文共 8562 字,大约阅读时间需要 28 分钟。

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
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
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
comboBox = new JComboBox
(); comboBox.setModel(new DefaultComboBoxModel
(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
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); }}

  

转载于:https://www.cnblogs.com/daxiang2008/p/9213940.html

你可能感兴趣的文章
Jmeter中的XPath Assertion
查看>>
入门-什么是webshell?
查看>>
monkey 命令
查看>>
[BZOJ] 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
查看>>
junit测试框架
查看>>
Unity3D之MonoBehaviour的可重写函数整理
查看>>
通过定时监听input框来实现onkeyup事件-
查看>>
C#写webservice服务引用
查看>>
.NET重构(二):ArrayList,List,IList的联系和区别
查看>>
微信小程序 转发
查看>>
PAT乙级练习1001
查看>>
hydra 密码破解工具详解
查看>>
关于ssh报错:Agent admitted failure to sign using the key.
查看>>
NoSQL漫谈 NoSql = Not Only Sql != No Sql
查看>>
BZOJ - 2741 分块维护最大连续异或和
查看>>
Mac 下配置Lua环境
查看>>
OC,nil,NULL,Nil,kCFNull
查看>>
C语言运算符优先级
查看>>
k8s系列----索引
查看>>
团队任务五
查看>>