Java 为JTable中的每一行添加到数据库
我想为Jtable中的每一行添加到数据库中。e、 g.它在表中添加第一个产品,但我希望它添加每个产品Java 为JTable中的每一行添加到数据库,java,sql,database,jtable,Java,Sql,Database,Jtable,我想为Jtable中的每一行添加到数据库中。e、 g.它在表中添加第一个产品,但我希望它添加每个产品 private void addToInvoiceLine(){ try { String sql = "Insert Into invoiceLine (invoiceID,SKU,quantity) values (?,?,?)"; pst = conn.prepareStatement(sql); int row = resultsTa
private void addToInvoiceLine(){
try {
String sql = "Insert Into invoiceLine (invoiceID,SKU,quantity) values (?,?,?)";
pst = conn.prepareStatement(sql);
int row = resultsTable.getSelectedRow();
String sku = (orderTable.getModel().getValueAt(row, 0).toString());
String quantity = (orderTable.getModel().getValueAt(row, 2).toString());
pst.setString(1, invoiceNo.getText());
pst.setString(2, sku);
pst.setString(3, quantity);
pst.execute();
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
您需要在表中的每一行上创建一个循环,如下所示:
private void addToInvoiceLine(){
try {
String sql = "INSERT INTO invoiceLine (invoiceID,SKU,quantity) VALUES (?,?,?)";
pst = conn.prepareStatement(sql);
for(int row=0; row<orderTable.getModel().getRowCount(); row++){
String sku = orderTable.getModel().getValueAt(row, 0).toString();
String quantity = orderTable.getModel().getValueAt(row, 2).toString();
pst.setString(1, invoiceNo.getText());
pst.setString(2, sku);
pst.setString(3, quantity);
pst.execute();
}
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
private void addToInvoiceLine(){
试一试{
字符串sql=“插入发票行(发票ID、SKU、数量)值(?,?)”;
pst=conn.prepareStatement(sql);
for(int row=0;row是否要将Jtable
中的所有行复制到数据库表中?是,哪个产品已添加到Jtable中?使用循环有什么问题?for(int row=0;row
我没有脑袋,所以通过我不存在的脑袋说出来?但是,…但是,…但是,…我没有脑袋!试试{…}catch(HeadlessException e){joptionPane.showMessageDialog(…);}
catch-22