Netbeans - Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4

 
Vista:
sin imagen de perfil
Val: 2
Ha aumentado su posición en 10 puestos en Netbeans (en relación al último mes)
Gráfica de Netbeans

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4

Publicado por Laura (1 intervención) el 30/10/2020 04:06:02
Hola a todos, quisiera recibir algo de ayuda sobre este problema que tengo al querer mandar los datos de una base a una jtable en netbeans he intentado con tutoriales y aun no me anda siempre me sale ese error, dejo el codigo del boton el cual intento enlazar con la base de datos,cualquier ayuda lo agradezco!


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
String campo = jTextField3.getText();
String where = "";
 
if(!"".equals(campo))
{
    where = "  |    WHERE fecha_prestamo  = '" + campo + "'";
 
}
 
 
try {
    DefaultTableModel modelo = new DefaultTableModel();
    jTable1.setModel(modelo);
 
    PreparedStatement ps = null;
    ResultSet rs = null;
 
    conexion conn = new conexion();
    Connection con = conn.getConnection();
 
    //Metodo de consulta con base de datos.
    String sql = "SELECT   id_libro, nombre_libro, estado_libro, id_usuario, fecha_prestamo FROM prestamosb"
+ where ;
 
    System.out.println(sql);
 
    ps = con.prepareStatement(sql);
    rs = ps.executeQuery();
 
    ResultSetMetaData rsMd = (ResultSetMetaData) rs.getMetaData();
    int cantidadColumnas = rsMd.getColumnCount();
 
    modelo.addColumn("ID");
    modelo.addColumn("Nombre");
    modelo.addColumn("Estado");
    modelo.addColumn("IDU");
    modelo.addColumn("Fecha");
 
    int[] anchos = {50, 200, 50, 50};
    for (int i = 0; i < jTable1.getColumnCount(); i++) {
        jTable1.getColumnModel().getColumn(i).setPreferredWidth(anchos[i]);
    }
 
    while (rs.next()) {
        Object[] filas = new Object[cantidadColumnas];
        for (int i = 0; i < cantidadColumnas; i++) {
            filas[i] = rs.getObject(i + 1);
        }
        modelo.addRow(filas);
    }
 
} catch (SQLException ex) {
    System.err.println(ex.toString());
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder