SQL Server异常:索引超出范围

时间:2018-08-18 09:36:41

标签: java sql sql-server

每当我在Java Swing项目中单击表格时,我都试图将表格行加载到表单中,因此我在MouseEvent中为表格编写了以下代码:

private void tblBatchMouseClicked(java.awt.event.MouseEvent evt) {                                      
    int selectedRow = tblBatch.getSelectedRow() + 1;
    if (selectedRow > 0){
        try {
            if(rs.absolute(selectedRow)){
                String code = rs.getString(2); //this line throws an error
                String desc = rs.getString(3); 
                String major = rs.getString(4);
                txtBatchCode.setText(code);
                txtBatchDescription.setText(desc);
                comboMajor.setSelectedItem(major);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UpdateBatchDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}       

这是我加载表的代码:

private void loadTable(){
    String sql = "SELECT BATCH_ID AS 'SNO', Code, BATCH.Description, Name FROM BATCH JOIN MAJOR ON BATCH.Major_ID = MAJOR.Major_ID";
    rs = helper.executeQuery(sql);
    SwingUtil.displayResultToTable(tblBatch, rs);
}

每当我运行代码时,都会不断出现错误:

Aug 18, 2018 4:14:03 PM view.UpdateBatchDialog tblBatchMouseClicked
SEVERE: null
com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(SQLServerResultSet.java:543)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(SQLServerResultSet.java:2066)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2099)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2084)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(SQLServerResultSet.java:2427)
    at view.UpdateBatchDialog.tblBatchMouseClicked(UpdateBatchDialog.java:160)
    at view.UpdateBatchDialog.access$000(UpdateBatchDialog.java:20)
    at view.UpdateBatchDialog$1.mouseClicked(UpdateBatchDialog.java:79)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6536)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2238)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2296)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4897)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4543)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4475)
    at java.awt.Container.dispatchEventImpl(Container.java:2282)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

如果将其更改为任何其他索引,我仍然会遇到相同的错误,但索引不同。我期待着你们的任何有用的提示/替代方法/答案。

这是my project的屏幕截图,可以清晰地看到我想做什么。提前致谢。

0 个答案:

没有答案