您的位置:首页 > 数据库

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

2014-12-04 23:20 288 查看

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: