Search code examples
javasqltreeresultset

Need to convert a Result Set to some data structure


I have a java.sql.ResultSet like this:

=============================
| colA | ColB | ColC | ColD |
=============================
|   1  |   1  |   1  |   1  |
|   1  |   1  |   1  |   2  |
|   1  |   1  |   2  |   3  |
|   1  |   1  |   2  |   4  |
|   1  |   2  |   3  |   5  |
|   1  |   2  |   4  |   6  |
|   1  |   2  |   4  |   7  |
|   1  |   2  |   4  |   8  |
|   1  |   3  |   5  |   9  |
|   1  |   3  |   6  |  10  |
=============================

The first column will always have a single value. The last column will always be unique.

I need an implementation where I want to retrieve all the items in ColD that correspond to a specific item in ColB or ColC or ColA.

for instance, if I request all items in ColD for value 2 in ColB, I should get [5,6,7,8](as a list or array).

I was wondering if a tree implementation would be a right way to go about this.

What would be the best way to accomplish is? Thanks in advance.


Solution

  • Select colD from tableName where colB='2';
    

    Isn't the above query sufficient ?

    Update

    If you need this to be done in java then :

    ResultSet rs=getAllDataInResultSet();//this method will return from stored procedure
    List<int> resultList=new ArrayList<>();
    while(rs.next()){
          int  colB=rs.getInt(2);
          if(colB==2){
           resultList.add(rs.getInt(4));
          }
    }
    logger.debug(resultList); //logging the final Result list
    

    Output :

    [5,6,7,8]