【问题标题】:java JDBC ArrayIndexOutOfBoundsException when delete, updatejava JDBC ArrayIndexOutOfBoundsException 时删除,更新
【发布时间】:2017-09-19 07:31:16
【问题描述】:

所以我尝试创建一个 JTable,它通过 JBDC 连接到 SQL server 数据库,并具有插入、删除和编辑数据等功能。它适用于插入但更新、删除。你们能告诉我为什么我得到ArrayIndexOutOfBoundsException: -1 以及如何解决它。这是我的代码。这里的书是一个扩展JFRAME的类

public Book() {
    initComponents();

    model.addColumn("ID");
    model.addColumn("Name");
    model.addColumn("Type");

    jTable1.setModel(model);
    displayTable();
    jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            int row = jTable1.getSelectedRow();

            txtName.setText(jTable1.getValueAt(row, 1).toString());
            txtType.setText(jTable1.getValueAt(row, 2).toString());

        }
    });

}

public void displayTable() {
    try {
        model.setRowCount(0);
        ConnectToSQL sql = new ConnectToSQL();
        connection = sql.getConnection();
        st = connection.createStatement();
        ResultSet result = st.executeQuery("SELECT * FROM BOOK");
        while (result.next()) {
            model.addRow(new Object[]{result.getInt("id"), result.getString("name"), result.getString("type")});
        }

    } catch (SQLException ex) {
        Logger.getLogger(Book.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    int row = jTable1.getSelectedRow();
    String id = jTable1.getValueAt(row, 0).toString();
    try {
        // TODO add your handling code here:
        st.executeUpdate("Update Book set name ='" + txtName.getText() + "',type='" + txtType.getText() + "' where id =" + id);
        displayTable();

    } catch (SQLException ex) {
        Logger.getLogger(Book.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    int row = jTable1.getSelectedRow();
    String id = jTable1.getValueAt(row, 0).toString();
    try {
        // TODO add your handling code here:
        st.executeUpdate("Delete from book where id =" + id);
        displayTable();

    } catch (SQLException ex) {
        Logger.getLogger(Book.class.getName()).log(Level.SEVERE, null, ex);
    }

}

【问题讨论】:

  • 在哪一行?你得到这个错误
  • 在 btnUpdateActionPerformed 和 btnDeleteActionPerformed 我得到了 ArrayIndexOutOfBoundsException,即使它仍然删除或更新到数据库
  • 好吧-1 当你没有选择一行时会发生这种情况。然后选择的行返回-1。例如如果row=-1 然后jTable1.getValueAt(-1, 1) 然后你得到错误。为避免这种情况,您可以查看if(row!=-1){//do something}else{//plz select a row}

标签: java sql-server swing jdbc indexoutofboundsexception


【解决方案1】:

你得到这个错误,因为你没有选择任何行,所以为了避免这个问题你必须使用:

if(jTable1.getSelectedRow() != -1){
   int row = jTable1.getSelectedRow();
   String id = jTable1.getValueAt(row, 0).toString();
   //rest of your code here
}else{
   //show an error for example, no row is selected
}

注意

而不是:

st.executeUpdate("Update Book set name ='" + txtName.getText() + "',type='" + txtType.getText() + "' where id =" + id);

你必须使用 PreparedStatement 来避免任何语法错误或 SQL 注入

例如:

String query = "Update Book set name = ?, type=? where id =?";

try (PreparedStatement update = connection.prepareStatement(query)) {

    update.setString(1, txtName.getText());
    update.setString(2, txtType.getText());
    update.setInt(3, id);

    update.executeUpdate();
}

另外,你的 id 是 int,所以你不能像你必须设置一个 int 那样为你的查询设置一个字符串:

String id = jTable1.getValueAt(row, 0).toString();

相反,您必须使用:

int id = Integer.parseInt(jTable1.getValueAt(row, 0).toString());

【讨论】:

  • 我试图像你说的那样修复,但它仍然出现异常,当我通过 displayTable() 函数删除或编辑数据后尝试更新 jtable 时,它​​会出现异常
  • @TúAnhDư ArrayIndexOutOfBoundsException 消失与否?新的例外是什么?
  • 它仍然是 ArrayIndexOutOfBoundsException: -1,我认为这不是 getselectedRow() 导致异常的原因,因为数据库在此之后更新,但它来自 displayTable()
  • @TúAnhDư 好的,你能调试你的程序并检查你的异常到底发生在哪里吗?
  • 哇,它有效,我将 addListSelectionListener 更改为 jTableMouseClick 并且它工作得非常完美,但我不知道为什么这不起作用......无论如何,谢谢你花时间帮助我跨度>
猜你喜欢
  • 1970-01-01
  • 2015-09-21
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
相关资源
最近更新 更多