我有一个jTable。它有6列。我需要在第1列输入名称,一旦按下回车键,必须从数据库填充其他5列。有人可以帮忙吗?
private void jTable2KeyPressed(java.awt.event.KeyEvent evt) {
{
int key = evt.getKeyCode();
if (key == KeyEvent.VK_ENTER)
{
// Object s=jTable2.getModel().getValueAt(1, 1);
//System.out.println("Value" +s);
Object Name= jTable2.getModel().getValueAt(1, 1);;
try {
openconn();
PreparedStatement ps;
System.out.println("Val" +Name);
ps = conn.prepareStatement("select * from inventory.addproducts where name='"+Name+"'");
rs= ps.executeQuery();
if(rs.next())
{
String rate=rs.getString(2);
jTable2.getModel().setValueAt(rate, 2, 3);
}
else
{
JOptionPane.showInputDialog("Wrong input");
}
conn.close();
}
catch(Exception ex)
{
}
}}
}
答案 0 :(得分:0)
希望这样做有意义,使用DefaultTableModel填充你的JTabel,一个简单的例子,还没有尝试过:
private DefaultTableModel model=new DefaultTableModel();
private JTable table=new JTable(model);
private JButton submit =new JButton("Submit");
public void populate(){
submit.addActionListener(e->{model.addRow(new Object[]{textField.getText(),database.getSurname(),database.getAge()});})
}