So i try to create a JTable which connect to SQL server database by JBDC, and have a function like insert, delete, and edit the data. It work well with insert but update, delete. Can you guys show me why i got ArrayIndexOutOfBoundsException: -1
and how to fix it. Here 's my code. Book here is a class extends 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);
}
}
You get this error, because you don't select any row, so to avoid this problem you have to use :
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
}
Note
Instead of :
st.executeUpdate("Update Book set name ='" + txtName.getText() + "',type='" + txtType.getText() + "' where id =" + id);
You have to use PreparedStatement to avoid any syntax error or SQL Injection
For example :
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();
}
Another thing, your id is an int
so you can't set a String to your query like you do you have to set an int :
String id = jTable1.getValueAt(row, 0).toString();
Instead you have to use :
int id = Integer.parseInt(jTable1.getValueAt(row, 0).toString());