一个Java的swing问题
一个JPanel上有个JTable,已经连接上数据库,如何让数据库中的表的数据添加在这个JTable上显示。要用循环把所有记录都遍历...
一个JPanel上有个JTable,已经连接上数据库,如何让数据库中的表的数据添加在这个JTable上显示。要用循环把所有记录都遍历
展开
3个回答
展开全部
private void initTable(String strSql) {
Object[][] body = null;
String[] head = null;
if (ticketType.equals(MainFrame.COLLIGATE)) {
head = new String[]{"序号", "开票日期", "操作任务", "操作单位", "开票方式","导入DMIS"};
} else if (ticketType.equals(MainFrame.SUBENTRY)) {
head = new String[]{"序号", "开票日期", "操作任务", "开票方式","导入DMIS"};
}
dTableModel = new DefaultTableModel(body, head) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
};
int index = 0;// 序号
Statement stem = null;
ResultSet rs = null;
String id;//唯一表id
String time;//日期
String czdw;//操作单位
String czrw;//操作任务或操作项目
String cardtype = ""; //开票方式
String dmistype = "";//导入DMIS系统标志
String dmisname = "";//导入DMIS系统标志显示名称
// int iNo = 1;
try {
conn = PowerOperationCardDBConnection.getPowerOperationCardConnection();
stem = conn.createStatement();
rs = stem.executeQuery(strSql);
//遍历数据记录
while (rs.next()) {
//这个rowData 是保存每一条
Object[] rowData = null;
index++;// 序号
time = rs.getString("npsj");
czrw = rs.getString("czrw");
dmistype = rs.getString("pjyj");
dmisname = (dmistype.equals("0"))?"未导过":"已导过";
if (ticketType.equals(MainFrame.COLLIGATE)) {
id = rs.getString("zhzxpzbid");
// if (iNo == 1)
// this.operatorCardID = id;
czdw = rs.getString("czdw");
cardtype = rs.getString("kpfs");
Item item = new Item(id, time);
Item dmisitem = new Item(dmistype, dmisname);
rowData = new Object[]{index, item, czrw, czdw, cardtype,dmisitem};
} else if (ticketType.equals(MainFrame.SUBENTRY)) {
id = rs.getString("fxzxpzbid");
// if (iNo == 1)
// this.operatorCardID = id;
cardtype = rs.getString("kplx");
Item item = new Item(id, time);
Item dmisitem = new Item(dmistype, dmisname);
rowData = new Object[]{index, item, czrw, cardtype,dmisitem};
}
//把数据加到表模型中
dTableModel.addRow(rowData);
// iNo++;
}
rs.close();
stem.close();
} catch (Exception e) {
// e.printStackTrace();
JOptionPane.showMessageDialog(this, e.getMessage(), SystemConstants.SYSTEM_TITLE, JOptionPane.WARNING_MESSAGE);
}
//把数据模型加到表中显示
tableTicket.setModel(dTableModel);
tableTicket.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
tableTicket.getColumnModel().getColumn(0).setMaxWidth(50);
tableTicket.getColumnModel().getColumn(1).setMinWidth(80);
tableTicket.getColumnModel().getColumn(2).setMinWidth(350);
tableTicket.getColumnModel().getColumn(3).setMinWidth(80);
if (ticketType.equals(MainFrame.COLLIGATE)) {
tableTicket.getColumnModel().getColumn(4).setMinWidth(80);
}
}
Object[][] body = null;
String[] head = null;
if (ticketType.equals(MainFrame.COLLIGATE)) {
head = new String[]{"序号", "开票日期", "操作任务", "操作单位", "开票方式","导入DMIS"};
} else if (ticketType.equals(MainFrame.SUBENTRY)) {
head = new String[]{"序号", "开票日期", "操作任务", "开票方式","导入DMIS"};
}
dTableModel = new DefaultTableModel(body, head) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
};
int index = 0;// 序号
Statement stem = null;
ResultSet rs = null;
String id;//唯一表id
String time;//日期
String czdw;//操作单位
String czrw;//操作任务或操作项目
String cardtype = ""; //开票方式
String dmistype = "";//导入DMIS系统标志
String dmisname = "";//导入DMIS系统标志显示名称
// int iNo = 1;
try {
conn = PowerOperationCardDBConnection.getPowerOperationCardConnection();
stem = conn.createStatement();
rs = stem.executeQuery(strSql);
//遍历数据记录
while (rs.next()) {
//这个rowData 是保存每一条
Object[] rowData = null;
index++;// 序号
time = rs.getString("npsj");
czrw = rs.getString("czrw");
dmistype = rs.getString("pjyj");
dmisname = (dmistype.equals("0"))?"未导过":"已导过";
if (ticketType.equals(MainFrame.COLLIGATE)) {
id = rs.getString("zhzxpzbid");
// if (iNo == 1)
// this.operatorCardID = id;
czdw = rs.getString("czdw");
cardtype = rs.getString("kpfs");
Item item = new Item(id, time);
Item dmisitem = new Item(dmistype, dmisname);
rowData = new Object[]{index, item, czrw, czdw, cardtype,dmisitem};
} else if (ticketType.equals(MainFrame.SUBENTRY)) {
id = rs.getString("fxzxpzbid");
// if (iNo == 1)
// this.operatorCardID = id;
cardtype = rs.getString("kplx");
Item item = new Item(id, time);
Item dmisitem = new Item(dmistype, dmisname);
rowData = new Object[]{index, item, czrw, cardtype,dmisitem};
}
//把数据加到表模型中
dTableModel.addRow(rowData);
// iNo++;
}
rs.close();
stem.close();
} catch (Exception e) {
// e.printStackTrace();
JOptionPane.showMessageDialog(this, e.getMessage(), SystemConstants.SYSTEM_TITLE, JOptionPane.WARNING_MESSAGE);
}
//把数据模型加到表中显示
tableTicket.setModel(dTableModel);
tableTicket.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
tableTicket.getColumnModel().getColumn(0).setMaxWidth(50);
tableTicket.getColumnModel().getColumn(1).setMinWidth(80);
tableTicket.getColumnModel().getColumn(2).setMinWidth(350);
tableTicket.getColumnModel().getColumn(3).setMinWidth(80);
if (ticketType.equals(MainFrame.COLLIGATE)) {
tableTicket.getColumnModel().getColumn(4).setMinWidth(80);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DefaultTableModel dm = new DefaultTableModel();
myTable = new JTable(dm);
myTable.setEnabled(false);
int cCount = rs.getMetaData().getColumnCount();
String[] s = new String[cCount];
for (int i = 1; i <= cCount; i++) {
s[i - 1] = rs.getMetaData().getColumnName(i);
dm.addColumn(s[i - 1]);
}
dm.addRow(s);
while (rs.next()) {
for (int i = 1; i <= cCount; i++) {
s[i - 1] = rs.getString(i);
}
dm.addRow(s);
}
this.add(myTable);
myTable.updateUI();
这是我最爱用的方法。。很爽
myTable = new JTable(dm);
myTable.setEnabled(false);
int cCount = rs.getMetaData().getColumnCount();
String[] s = new String[cCount];
for (int i = 1; i <= cCount; i++) {
s[i - 1] = rs.getMetaData().getColumnName(i);
dm.addColumn(s[i - 1]);
}
dm.addRow(s);
while (rs.next()) {
for (int i = 1; i <= cCount; i++) {
s[i - 1] = rs.getString(i);
}
dm.addRow(s);
}
this.add(myTable);
myTable.updateUI();
这是我最爱用的方法。。很爽
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
基本上就这样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询