I'm writing an application using jdbc connection to access and want display a sql query using the number of rows found to declare an array of JTextFields. Not only does nothing display but also getting an Error: java.lang.ArrayIndexOutOfBoundsException: 00.

Can anyone help please.

private void selItActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // get number of rows in db
      int rowCount = getRow();
      // dispay number of rows in query
      JOptionPane.showMessageDialog(null, "Number of rows to view\n\r" + rowCount);
      //set panel visibility, position, etc.
        JFrame sell = new JFrame("Sell an Item");
        sell.setVisible(true);
        sell.setBounds(0,0,525,550);
        sell.setLocationRelativeTo(null);
            // connect to database
            String data = "jdbc:odbc:SNbase";
       try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(
                data, "", "");
            Statement st = conn.createStatement();
            // set sql query
            ResultSet rec = st.executeQuery(
                    "SELECT * " +
                    "FROM stock_items");
            // loop for all rows
            while(rec.next()) {
                // use number of rows to put data into array
                for (int i=0; i<rowCount; i++){
                    // place query result into String
                     String text = rec.getString(2) + "\t" + rec.getString(3) + "\t"
                + rec.getString(4) + "\t" + rec.getString(5) + "\t" + rec.getString(6) + "\r\n";
               // set array "i"
               JTextField jft[] = new JTextField[i];
               // place String into array "i"
               jft[i]=new JTextField(text);
               // put array onto sell frame
                 sell.add(jft[i]);
                }
                
            }
            // colse connection
            st.close();
        } catch (SQLException s) {
            System.out.println("SQL Error: " + s.toString() + " "
                + s.getErrorCode() + " " + s.getSQLState());
        } catch (Exception e) {
            System.out.println("Error: " + e.toString()
                + e.getMessage());
        }

    }

Your code is messed up

Use the below Code inside your For Loop

JTextField jt = new JTextField();
jt.setSize(new Dimension(30,20));
jt.setText(text);
int val =(i+1)*10;
jt.setLocation(val, val);
sell.add(jt);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.