Solved

Help for Fetching resultset data

Posted on 2004-10-19
10
269 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now