我有一个链接到mysql表的jtable
。
该表格有两列' Item_no
'和' Item_description
'
我想要的是:用户完成在item_no
中输入jtable
后,相邻的单元格(项目描述)将填入数据库中的相应说明
答案 0 :(得分:2)
覆盖setValueAt
的{{1}}。设置项目编号列值后,从数据库加载相应的值并相应地更新模型。
有关详细信息,请参阅How to use tables
答案 1 :(得分:0)
解决!!! AllocationsTable.getCellEditor(0,0).addCellEditorListener(new CellEditorListener(){
@Override
public void editingStopped(ChangeEvent ce) {
PreparedStatement ps;
String sql = "Select * from items where item_no= ?";
try
{
for (i=0;i<model.getRowCount();i++)
{
ps = conn.prepareStatement(sql);
ps.setString(1,model.getValueAt(i, 0).toString());
ResultSet rst;
rst = ps.executeQuery();
rst.last();
model.setValueAt(rst.getString("Description"), i, 1);
model.fireTableCellUpdated(i, 1);
}
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage());
}