Solved

Help for Fetching resultset data

Posted on 2004-10-19
10
271 Views
Last Modified: 2012-06-27
I am in truble to process resultset data by using the below code.The problem is If I process a column first, I will not get process the same column again. for eg. I did process  stData=rs.getString(4); under the while loop the same column will be processed again under the for loop. then I will get exception and the message is "data not found".If I comment the line "stData=rs.getString(4); " , it will work fine. Coluld you guys pls help me to solve this issue.I am using netbeans3.6 IDE. if I use the statement
Statement st= Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);, no hope.


   //Statement st= Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
   
Statement st= Con.createStatement();
    String StrSql="select * from tblTickets";
    ResultSet rs= st.executeQuery(StrSql);
    ResultSetMetaData TmpMetaData= rs.getMetaData();
    int NumColumn= TmpMetaData.getColumnCount();
    String StResultData,stData;
   
        while(rs.next()){
// I will process few recordset data here
            stData=rs.getString(4);
           
// loop through columns
            for (int i=3;i<NumColumn;i++ ){
            StResultData= rs.getString(i);  
                       
            System.out.print("Data: "+StResultData);
           
            } // for loop
        }
    }catch(SQLException e){
        System.out.println("Errorr: "+e.getMessage());
    }

0
Comment
Question by:tstName
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12345243
Why not **first** get all the data in a list

List data = new ArrayList();
for (int i=0; i<NumColumn; i++) {
  data.add( rs.getString(i);
}

and afterwards process them

If you don't like that, you'll have to avoid getting the same column twice
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12345269
You could also place the column data in a map with as key the column.
Then in the loop first you look if it is in the map, if it is you get it out of the map
if it isn't you get it out of the ResultSet:

        Map theData = new HashMap();

        while(rs.next()){
            stData=rs.getString(4);
            // 1) Process it
            // 2) Add it to the map for further use
            theMap.put( new Integer(4), stData );     // <<<< put it in the map
           
            // loop through columns
            for (int i=3;i<NumColumn;i++ ){
                 if ( theMap.containsKey(new Integer(i)) )      // First check the map
                   StResultData = (String)theMap.get( new Integer(i) );  // found
                 else
                   StResultData = rs.getString(i);       // not found in the map
                 System.out.print("Data: "+StResultData);
            } // for loop
        }
0
 

Author Comment

by:tstName
ID: 12345281
I did use hashtable to solve this issue.But I dont like that, i like to use resultset processing rather than taking data into a hashtable and process it.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 37

Expert Comment

by:zzynx
ID: 12345339
>> But I dont like that
Any reason?
>>i like to use resultset processing
Then you'll have to live with the limitations of it ;°)
Or change your design so that the need for processing twice the same column disappears
0
 

Expert Comment

by:absarahmad
ID: 12345347
hello

try to use string version of getString() ,i think this will be the problem

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12345366
Your loop is wrong btw (unless you're intending to omit the last column)
0
 

Expert Comment

by:sujata7576
ID: 12346560
hi,

Assuming that u know beforehand which all columns u r  using for processing before using them in for loop , u can neglect those colums in ur for loop ie

 while(rs.next()){
            stData=rs.getString(4);
   for (int i=3;i<NumColumn;i++ ){
  //since u already have value of column 4 in stData.    
  if(i != 4)  
         StResultData= rs.getString(i);  
                       
            System.out.print("Data: "+StResultData);
           
            } // for loop
        }

though the above solution has its limitations such as u shoud know the column numbers u r checking and u have to use if loop in your for loop to check if u r checking the same column again

Hope this will help.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12346634
>>though the above solution has its limitations
You can remember the checked column in a Set

Set done = new HashSet();
while(rs.next()) {
   stData=rs.getString(4);
   done.add( new Integer(4) );  // <<<<<< mark as done

   for (int i=3;i<NumColumn;i++ ) {
     if ( !done.contains(new Integer(i)) ) {  // <<<<< check if not already done
         StResultData= rs.getString(i);  
         System.out.print("Data: "+StResultData);
     }

   } // for loop
}
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question