I have the problem that my program returns an error, that I do not know why it occurs, when collecting data from a row of a JTable
selected and pass them to a series of JTextField
, the error produced is this:
java.lang.ArrayIndexOutOfBoundsException: 0 > = 0
I attach code where I load the data in the textField
private void btModificarActionPerformed(java.awt.event.ActionEvent evt) {
int rowIndex = tabla.getSelectedRow();
dni = String.valueOf(tabla.getValueAt(rowIndex,0));
nombre = String.valueOf(tabla.getValueAt(rowIndex,1));
ciudad = String.valueOf(tabla.getValueAt(rowIndex,2));
tfDni.setText(dni);
tfNombre.setText(nombre);
tfCiudad.setText(ciudad);
btInsertar.setEnabled(false);
btBorrar.setEnabled(false);
btListado.setEnabled(false);
}
Here I call the modification:
private void btAceptarActionPerformed(java.awt.event.ActionEvent evt) {
modificar();
}
Here already if I make the modification:
public void modificar(){
int rowIndex = tabla.getSelectedRow();
try{
String dni = String.valueOf(tabla.getValueAt(rowIndex, 0));
String nombre = String.valueOf(tabla.getValueAt(rowIndex, 1));
String ciudad = String.valueOf(tabla.getValueAt(rowIndex, 2));
String sql = "update clientes set nombre='"+nombre+"',ciudad='"+ciudad+"' where dni='"+dni+"'";
/*PARA HACER QUE EL UPDATE COJA LOS VALORES DEL CAMPO DE TEXTO, HABRÍA QUE PASARLE EL tf.getText() que corresponda*/
mysql.ejecutar(sql);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Error al Modificar");
}
}
The application's constructor is like that, I think the fault may be there.
public class Ventana extends javax.swing.JFrame {
MySQL mysql;
Connection con;
PreparedStatement pst;
Statement st;
DefaultTableModel modelo;
String dni;
String nombre;
String ciudad;
ResultSet rs;
public Ventana(MySQL mysql) throws SQLException {
/*INSTANCIAMOS MYSQL*/
this.mysql = new MySQL();
initComponents();
this.setVisible(true);
con = mysql.MySQLConnection("pedidos", "root", "1234");
modelo = new DefaultTableModel();
tabla.setModel(modelo);
}
Here is the photo so you can see everything
Here is another photo that shows the whole code
Finally, I have attached the method where I show what is in the database and I extract the metadata from the columns
when choosing the row, it returns that
returns -1, that means, in my opinion, that has not updated, and indeed it is.
Sorry for the delay in responding