我正在尝试传入一个具有DiscountID和Name列的JTable对象。我基本上试图得到JTable A(一个DiscountID)中选择的第一列的一行,以及与JTable B
中所选记录相关的这一组数据,即rs.next中的JTable5
()循环。问题是我收到以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:730)
at java.util.Vector.elementAt(Vector.java:473)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at javax.swing.JTable.getValueAt(JTable.java:2717)
at airvia.Model.setFlexiBands(Model.java:688)
指的是这一行:
stmt.setObject(1, table.getValueAt(row, 0));
以下是相关方法的代码:
public static void setFlexiBands(JTable table) {
try {
DiscountGUIView dgv = new DiscountGUIView();
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abpp034?user=abpp034&password=120001772");
stmt = con.prepareStatement("SELECT UpperBound, PercentageRate FROM FlexiBand WHERE DiscountID = ?");
int row = table.getSelectedRow();
stmt.setObject(1, table.getValueAt(row, 0));
rs = stmt.executeQuery();
try {
int i = 0;
while (rs.next()) {
dgv.getjTable5().setValueAt(rs.getInt("UpperBound"), i, 0);
dgv.getjTable5().setValueAt(rs.getInt("PercentageRate"), i, 1);
i++;
}
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException se) {
System.out.println(se.getErrorCode());
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException se) {
System.out.println(se.getErrorCode());
}
}
if (con != null) {
try {
con.close();
} catch (SQLException se) {
System.out.println(se.getErrorCode());
}
}
}
} catch (SQLException ex) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}
}
我对如何解决这个问题感到困惑,因为我已经使用变量int row = table.getSelectedRow();
在其他地方调用了一个JTable对象并将其传递给stmt.setObject(1, table.getValueAt(row, 0));
并且我没有任何异常从中。错误发生在我运行程序时,因此我无法看到带有2个JTable的GUI,因此选择任何行。