Solved

Help for Fetching resultset data

Posted on 2004-10-19
10
272 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

713 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