Problem when filling JTable with Query?

0

Hello Colleagues, I am programming in java in MVC pattern and I run into this loop inside my code. I'm using arraylist

//Arraylist para llenar una jtable con arreglos

 public ArrayList<String[]> Select(){

            try{
                 Statement s = getConexion().createStatement();
                 rs = s.executeQuery("Select idclie,status from cliente");
                 //System.out.println("El select si se hace");

                matResultado=new ArrayList<String[]>();
                String Fila[] = new String[2];



                //System.out.println("Array List se llena");

                while (rs.next()){
                    System.out.println("Entro al while");
                    Fila=new String[2];
                    for (int x=0;x<2;x++){       

                        //System.out.println("Entro al for del while");
                    //System.out.print(rs.getObject(x+1).toString());
                        Fila[x]=rs.getObject(x+1).toString();

                        //System.out.println("A una llave del final");                          
                    }
                  //  System.out.println("");
                    matResultado.add(Fila);
                }


                System.out.println("Salgo chido frel while");
                if (matResultado.isEmpty()) {
                    System.out.println("mat resutado es nulo");
                    return null;
                }
                System.out.println("math resultado vale"+matResultado.size());
                return matResultado;
            }catch(Exception ex){
                System.out.println(ex.getMessage());
            }

        return null;
    }

It works but when there are more than 10 registers, the program dies with e "Too many connections" and I realized that it makes a loop when making the query, because depending on the number of registers it does 1 while ex: I have 40 registers , 40 'System.out.println ("math resultado vale" + matResultado.size ());' that means that it fills my arrangement 40 times

That's how I send it to call to fill the table

if(pressed.getSource()==V1.btnBuscar){
    if(V1.cbxTipo.getSelectedIndex()==0){

        V1.modelo.setRowCount(0);


        try{
        for (int i=0;i<M1.Select().size() ; i++) {
            V1.modelo.addRow(M1.Select().get(i));
        }

        }

        catch (Exception excp){
            System.out.println(excp.getMessage());
            excp.printStackTrace();
        }
    
asked by Rickzize 24.05.2017 в 16:20
source

1 answer

0

As I said in comment, your problem is in the multiple calls you make.

Changing this you would call it only once.

if(pressed.getSource()==V1.btnBuscar){
    if(V1.cbxTipo.getSelectedIndex()==0){

       V1.modelo.setRowCount(0);


        try{
            ArrayList<String[] valoresBBDD = M1.Select();
            for (int i=0;i<valoresBBDD .size() ; i++) {
                V1.modelo.addRow(valoresBBDD.get(i));
            }

        }catch (Exception excp){
            System.out.println(excp.getMessage());
            excp.printStackTrace();
        }
   }
}
    
answered by 24.05.2017 / 17:17
source