?
Solved

Help for Fetching resultset data

Posted on 2004-10-19
10
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

777 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