2017-04-22 25 views
2

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

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); 
    } 

} 
+0

在哪行?你得到这个错误 –

+0

在btnUpdateActionPerformed和btnDeleteActionPerformed我得到了ArrayIndexOutOfBoundsException,即使它仍然删除或更新到数据库 –

+0

好'-1'这发生在你没有选择一行时,然后选择行返回'例如,如果'row = -1',那么'jTable1.getValueAt(-1,1)'然后你得到错误。为了避免它,你可以检查'if(row!= - 1){//做一些事情} else {// plz select a row}' –

回答

3

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

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所以你可以一个String不能设置为您查询喜欢你你有没有设定一个int:

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

相反,你必须使用:

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

我试图修复你说的,但它仍然有异常,有没有我尝试通过displayTable()函数删除或编辑数据后更新jtable –

+0

@TúAnhDư'ArrayIndexOutOfBoundsException'不见了?,什么新的异常不是? –

+0

它仍然ArrayIndexOutOfBoundsException:-1,我认为这不是getselectedRow()事情导致异常,因为数据库后更新,但它来自displayTable() –