我的问题是:如何实现一个“添加行”按钮,该按钮创建一个 JTable 的新行(从 SQLite 数据库获取数据),最后一行 ID 的自动增量++。id 列已经自动递增了数据,现在我只知道创建一个所有字段为空白的新行(没有最新行的增量++)。请问,有人可以帮忙吗?
private void cmd_newrowActionPerformed(java.awt.event.ActionEvent evt) {
((DefaultTableModel) PrimaryTable.getModel()).addRow(new Vector());
}
我想要的是:
==================
id | data |
==================
1 | some_data |
2 | some_data |
3 | some_data |
==================
After I press addRow button:
==================
id | data |
==================
1 | some_data |
2 | some_data |
3 | some_data |
4 | blank | /// blank row data with an autoincrement of the row id
==================
更新:
好的,现在我知道如何将新行插入数据库,但我不知道如何更新 JXTable。因此,它可以工作的波纹管代码,以及在我重新启动应用程序后更新的新空白行。有人可以帮助我吗?
private void cmd_newrowActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "Insert into RDSSPrimaryTable (sample_no,mineral_name,chemical_formula,main_peaks,other_peaks,reference,link,comments,spectrum) values (?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
// no pst.setString for id - it takes automatically
pst.setString(1, txt_sample_pt.getText());
pst.setString(2, txt_mineral_pt.getText());
pst.setString(3, txt_chemical_pt.getText());
pst.setString(4, txt_main_pt.getText());
pst.setString(5, txt_other_pt.getText());
pst.setString(6, txt_reference_pt.getText());
pst.setString(7, txt_link_pt.getText());
pst.setString(8, txt_comments_pt.getText());
pst.setBytes(9, person_image);
JOptionPane.showMessageDialog(null, "Inserted");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
((DefaultTableModel) PrimaryTable.getModel()).fireTableDataChanged(); // I tried this method even if the DefaultTableModel fire table automatically.
PrimaryTable.repaint(); //tried also repaint, revalidate - nothing happens
}