我只需要从jtable插入一列数据到数据库mysql,我找到了一种方法,但是当我插入的数据少于预期时,我遇到了一个问题,例如:插入2行而不是3行; Error:java.lang.arrayindexoutofboundsexception 2>=2
我该如何摆脱呢?
您可以查看我的代码...谢谢
try
{
//String product3 = "null";
int rows = jTable1.getRowCount();
int cols = jTable1.getColumnCount();
String salesql = "insert into salling(orderid,saledate,prod1,prod2,prod3,tax,subtotal,total) values(?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(salesql);
pst.setString(1, jTextFieldOrderNo.getText());
pst.setString(2, jTextFieldDate.getText());
for(int row =0; row <= rows; row++)
{
String product1 = (String)jTable1.getValueAt(0, 0);
String product2 = (String)jTable1.getValueAt(1, 0);
String product3 = (String)jTable1.getValueAt(2, 0);
// String product3 = (String)jTable1
pst.setString(3, product1);
pst.setString(4, product2);
pst.setString(5, product3);
}
pst.setString(6, jTextFieldTax.getText());
pst.setString(7, jTextFieldSuTotal.getText());
pst.setString(8, jTextFieldTotal.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Data saved...");
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}