2014.12.04从数据库中查找数据,并以表格显示

1.

<span style="font-size:18px;">package com.hechao;

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
 * 查询窗口
 * @author hechao
 *
 */
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
	private JTable empTable = null;
	private static String[] columnNames = {
		"工号", "姓名", "职位", "隶属领编号", "工资", "补贴", "隶属部门编号"};
	/**
	 * 构造器
	 */
	public MyFrame(){
		this.setTitle("表格查询");
		this.setSize(600, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		ini();
	}
	/**
	 * 刷新表格模型
	 */
	public void refreshTableModel() {
		if(empTable != null) {
			Manage manage = new Manage();
			ArrayList<Employee> list = manage.findAll();
			Object[][] data = new Object[list.size()][columnNames.length];
			for(int i = 0; i < data.length; i++) {
				Employee temp = list.get(i);
				data[i][0] = temp.getEno();
				data[i][1] = temp.getEname();
				data[i][2] = temp.getJob();
				data[i][3] = temp.getMgr();
				data[i][4] = temp.getSal();
				data[i][5] = temp.getComm();
				data[i][6] = temp.getDno();
			}
			
			empTable.setModel(new DefaultTableModel(data, columnNames) {
				@Override
				public boolean isCellEditable(int row, int column) {	// 不允许编辑单元格
					return false;
				}
			});
		}
	}
	/**
	 * 初始化窗口
	 */
	private void ini(){
		empTable = new JTable();
		empTable.getTableHeader().setReorderingAllowed(false);
		refreshTableModel();
		this.add(new JScrollPane(empTable));
	}
	
}
</span>

2.

<span style="font-size:18px;">package com.hechao;
/**
 * 员工类(POJO)
 * @author hechao
 *
 */
public class Employee {
	private int eno;  //员工编号
	private String ename;//员工姓名
	private String job;//职位
	private int mgr;//隶属领编号
	private int sal;//工资
	private int comm;//补贴
	private int dno;//部门编号
	
	public Employee(){};
	
	public int getEno() {
		return eno;
	}
	public void setEno(int eno) {
		this.eno = eno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int getMgr() {
		return mgr;
	}
	public void setMgr(int mgr) {
		this.mgr = mgr;
	}
	public int getSal() {
		return sal;
	}
	public void setSal(int sal) {
		this.sal = sal;
	}
	public int getComm() {
		return comm;
	}
	public void setComm(int comm) {
		this.comm = comm;
	}
	public int getDno() {
		return dno;
	}
	public void setDno(int dno) {
		this.dno = dno;
	}

	@Override
	public String toString() {
		return "Employee [eno=" + eno + ", ename=" + ename + ", job=" + job
				+ ", mgr=" + mgr + ", sal=" + sal + ", comm=" + comm + ", dno="
				+ dno + "]";
	}
	
	
}
</span>


3.

<span style="font-size:18px;">package com.hechao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/**
 * 管理类
 * @author hechao
 *
 */
public class Manage {
	/**
	 * 查找所有员工信息
	 * @return 返回一个装员工的数组容器
	 */
	public ArrayList<Employee> findAll(){
		ArrayList<Employee> list = new ArrayList<Employee>();
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/school",
					"root", "123456");
			stmt = con.createStatement();
			rs = stmt.executeQuery("select * from emp");
			while(rs.next()) {
				Employee employee = new Employee();
				employee.setEno(rs.getInt("eno"));
				employee.setEname(rs.getString("ename"));
				employee.setJob(rs.getString("job"));
				employee.setMgr(rs.getInt("mgr"));
				employee.setSal(rs.getInt("sal"));
				employee.setComm(rs.getInt("comm"));
				employee.setDno(rs.getInt("dno"));
				list.add(employee);
			}
			return list;
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if(rs != null) {
				try {
					rs.close();
				}
				catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(stmt != null) {
				try {
					stmt.close();
				}
				catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(con != null) {
				try {
					con.close();
				}
				catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
}
</span>


4.

<span style="font-size:18px;">package com.hechao;
/**
 * 查询
 * @author hechao
 *
 */
public class Search {
	public static void main(String[] args) {
		new MyFrame().setVisible(true);
	}	
}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值