Select all records from a table with Groovy

Asked

Viewed 58 times

0

I need to get all the records from a table from a script in Groovy, but with the script that I created it is taking only one record of each column and out of order too. Mine script this way:

List result = new ArrayList();
def i = 1;
while (resultset.next()){
    List sousListe = new ArrayList();
    sousListe.add(resultset.getString(i));

    result.add(sousListe);
    i = i + 1;
}

return result;

Initially he took only all the records of the first column, give for that mine script was like this:

List result = new ArrayList();
while (resultset.next()){
    List sousListe = new ArrayList();
    sousListe.add(resultset.getString(1));

    result.add(sousListe);
}

return result;

So I thought if I created a variable that would be implemented in my while it would go through all the columns of my table but only the error that changed.

1 answer

0


Solved the problem, I needed to first find out the number of columns that mine ResultSet and then perform a While until the number of columns ended thus began to display all the records correctly, follows script correct:

import java.util.logging.Logger;
Logger logger= Logger.getLogger("org.bonitasoft");
List result = new ArrayList();

if (resultset.last()){
    def l = resultset.getRow();
    def i = resultset.metaData.columnCount;
    resultset.beforeFirst();
    logger.info("Numero de Linhas do ResultSet: "+l.toString());
    logger.info("Numero de Colunas do ResultSet: "+i.toString());

    while (resultset.next()){
        List sousListe = new ArrayList();
        def x = 1;
        while (x <= i){
            sousListe.add(resultset.getString(x));
            logger.info(resultset.getString(x));
            x++ ;
        }
    result.add(sousListe);

    }
}
return result;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.