利用jdbc做的一个简单系统(接上一篇)

116 篇文章 22 订阅

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

package cn;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTxt;

public class TeacherFrame extends JFrame {
	/** 姓名文本框 */
	private LovoTxt nameTxt = new LovoTxt("姓名", 50, 50, this);
	/** 学历文本框 */
	private LovoTxt eduTxt = new LovoTxt("学历", 50, 100, this);
	/** 职称文本框 */
	private LovoTxt jobTxt = new LovoTxt("职称", 50, 150, this);
	/** 值文本框 */
	private JTextField jtxt = new JTextField();
	private LovoTable table = new LovoTable(this, new String[] { "姓名", "学历",
			"职称" }, new String[] { "name", "edu", "job" }, "id");
	/** 老师数据查询对象 */
	private TeacherDao dao = new TeacherDao();

	public TeacherFrame() {
		this.setLayout(null);
		table.setSizeAndLocation(300, 50, 220, 150);
		JPanel jp = new JPanel();
		jp.setLayout(null);
		Border b = BorderFactory.createLineBorder(Color.blue);
		Border border = BorderFactory.createTitledBorder(b, "查找");
		jp.setBorder(border);
		jp.setBounds(380, 220, 200, 100);
		this.add(jp);
		jtxt.setBounds(110, 30, 80, 20);
		jp.add(jtxt);

		LovoButton addButton = new LovoButton("添加", 50, 220, this);
		LovoButton delButton = new LovoButton("删除", 150, 220, this);
		LovoButton updateButton = new LovoButton("修改", 250, 220, this);
		LovoButton findButton = new LovoButton("查找", 100, 60, jp);
		load();
		findButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				find();
			}
		});

		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				add();
			}
		});
		delButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				del();
			}
		});
		updateButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				update();
			}
		});
		this.setSize(600, 400);
		this.setVisible(true);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}

	/**
	 * 将数据库数据加载到表格中
	 */
	public void load() {
		// 查询数据库得到全部数据
		List<Teacher> list = dao.findAll();
		table.updateLovoTable(list);
	}

	/**
	 * 将文本框数据封装插入数据库
	 */
	public void add() {
		// 创建老师对象,并封装文本框数据
		Teacher t = new Teacher();
		t.setName(nameTxt.getText());
		t.setEdu(eduTxt.getText());
		t.setJob(jobTxt.getText());
		// 完成数据库的添加
		dao.add(t);
		List<Teacher> list = dao.findAll();
		// 重新查询所有记录,更新表格
		table.updateLovoTable(list);
	}

	/**
	 * 删除选中行的数据
	 */
	public void del() {
		// 得到选中行的id
		int id = table.getKey();
		if (id == -1) {
			JOptionPane.showMessageDialog(null, "请选中行");
			return;
		}
		// 删除记录
		dao.del(id);
		List<Teacher> list = dao.findAll();
		table.updateLovoTable(list);
	}

	/**
	 * 修改数据
	 */
	public void update() {
		int id = table.getKey();
		if (id == -1) {
			JOptionPane.showMessageDialog(null, "请选中行");
			return;
		}
		dao.update(id, eduTxt.getText(), jobTxt.getText());
		List<Teacher> list = dao.findAll();
		table.updateLovoTable(list);
	}

	/**
	 * 按姓名模糊查询
	 */
	public void find() {
		// 得到文本框的内容
		String value = jtxt.getText();
		List<Teacher> list = null;
		list = dao.findByName(value);
		table.updateLovoTable(list);
	}

	public static void main(String[] args) {
		TeacherFrame m = new TeacherFrame();
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值