-1

I have a problem with a button that should be modifying data on my database.

This is the error I'm getting:

"Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 11 >= 11"

I gonna link here the code for the button and some extra things in case anyone ask...

I've checked and yes, everything is okay with the database columns.

Also, the jTable has 11 columns.

colActual = jTableNombre.getSelectedRow();


private void jButtonGuardarNombreActionPerformed(java.awt.event.ActionEvent evt) {                                                     

Connection conn = null;
Statement stmt = null;
        String nombre = (String) jTableNombre.getModel().getValueAt(colActual,2);
        String director=(String) jTableNombre.getModel().getValueAt(colActual,3);
        String año = (String) jTableNombre.getModel().getValueAt(colActual,4);
        String generos = (String) jTableNombre.getModel().getValueAt(colActual,5);
        String actores = (String) jTableNombre.getModel().getValueAt(colActual,6);
        String pais = (String) jTableNombre.getModel().getValueAt(colActual,7);
        String idioma = (String) jTableNombre.getModel().getValueAt(colActual,8);
        String doblaje = (String) jTableNombre.getModel().getValueAt(colActual,9);
        String subtitulos = (String) jTableNombre.getModel().getValueAt(colActual,10);
        String ubicacion = (String) jTableNombre.getModel().getValueAt(colActual,11);

    try {   
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            String sql= "UPDATE MOVIES" + 
                        "SET NOMBRE = '"+nombre+"'"+
                        "SET DIRECTOR = '"+director+"'"+
                        "SET AÑO = '"+año+"'"+
                        "SET GENEROS = '"+generos+"'"+
                        "SET ACTORES = '"+actores+"'"+
                        "SET PAIS = '"+pais+"'"+
                        "SET IDIOMA = '"+idioma+"'"+
                        "SET DOBLAJE = '"+doblaje+"'"+
                        "SET SUBTITULOS = '"+subtitulos+"'"+
                        "SET UBICACION = '"+ubicacion+"'"+
                        " WHERE ID = '"+id+"'";
            stmt.executeUpdate(sql);                           

    }
    catch(Exception e) {
        System.out.println(e);
    }
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283

1 Answers1

2

If the table only has 11 columns, then 11 is not a valid index. Indices start at 0, so valid column indices for your table would be 0 through 10.

Note also that the parameters for TableModel.getValueAt are (rowIndex, columnIndex). Is your variable named colActual really doing what you intend to do?

jTableNombre.getModel().getValueAt(colActual,2)
Riaz
  • 864
  • 6
  • 15
  • Thanks! it solved the "ArrayIndexOutOfBoundsException" error! dindt know about the indices starting at 0 instead of 1 on jTables, im very gratefull! – Fabrizio Bruzzese Jan 31 '16 at 20:24
  • Im getting a error now about SQL, i allready took the extra SET from each line and added the "," and the space to fit the syntax... I think the error is in the last line it says: cant compare int with char. But both id and ID are int on Database and java code. Any idea? – Fabrizio Bruzzese Jan 31 '16 at 20:32
  • Please post the SQL question as a separate question and we'll take a look. By the way, this isn't just for `JTable`s. In Java all index operations start at 0, at least with any official Java API. 3rd party libraries are free to do what they like, but the convention is to use zero-based indices. – Riaz Jan 31 '16 at 22:53
  • Thank you, ill do a separated question. – Fabrizio Bruzzese Feb 01 '16 at 01:10