Solved

Help for Fetching resultset data

Posted on 2004-10-19
10
274 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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

696 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