If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Having problem with iterating throug the resultSet

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-09, 13:41
swati malla swati malla is offline
Registered User
 
Join Date: Jul 2009
Posts: 26
Having problem with iterating throug the resultSet

Hello Everyone,
I have a question on DB2 with java. Select * from mySchemaName.myTableName this is the query i am using to retrieve all the rows of the table.Now i don have any idea of the table .I dont know how many columns are there and how to retrieve the column values.


By using the following two methods ResultSetMetaData.getColumnName and ResultSetMetaData.getcolumnClassName() i am getting the column name and column data type in java.
Is there any way to get the values of the resultSet in a generic way.I mean ,to get each and every value in the result.I wanted to use ResultSet.getString() or ResultSet.getBoolean() based on the column type but before to that i have to check each and every columns data type and then use the respective methods.Is there any generic way with out checking the column data type we can retrieve the values.


I would really appreciate if some one can reply me.

Thanks in advance.
swati
Reply With Quote
  #2 (permalink)  
Old 07-15-09, 14:06
rdutton rdutton is offline
Registered User
 
Join Date: Dec 2008
Posts: 76
I have done what you are doing, and in my experience there is no magic process that will eliminate the need to know how many columns you are dealing with and what data type each column is.

Maybe someone else has a different experience, but I doubt it. There is third party code and objects already written that will do most of the effort for you. Try looking in developerWorks on IBM's website.
__________________
RD
Reply With Quote
  #3 (permalink)  
Old 07-15-09, 15:58
swati malla swati malla is offline
Registered User
 
Join Date: Jul 2009
Posts: 26
Thanks a lot for your response.
I am getting each and every columns data type and checking with all java to sql data types and then getting the value.the code may some thing look like
ResultSetMetaData.getcolumncount()
for(int i=1;i<ResultSetMetaData.getcolumncount();i++){
String columntype=ResultSetMetaData..getColumnClassName(i );
String columnName=ResultSetMetaData.getColumnName(i);

if(columntype.equlas(java.lang.String){
String value=(String)ResultSet.getString(columnName);//like this i am checking with all java to sql data types.

}


}
Is there any good approach apart from the above code...

I would really appreciate if some one can answer this question....


Thanks,
swati
Reply With Quote
  #4 (permalink)  
Old 07-15-09, 16:41
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by swati malla
Is there any way to get the values of the resultSet in a generic way.I mean ,to get each and every value in the result.
What's wrong with ResultSet.getObject() ?
Reply With Quote
  #5 (permalink)  
Old 07-16-09, 16:50
swati malla swati malla is offline
Registered User
 
Join Date: Jul 2009
Posts: 26
if we go with ResultSet.getObject().Again we have to convert it into to some data types to retrieve the value right?
the code looks some what
Object ob=ResultSet.getObject()
if(columntype.equlas(java.lang.String){
String value=(String)ob

}

I guess thsi is the way but no effective way am i correct i might be wrong though...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On