aspose file tools*
The moose likes JDBC and the fly likes A table with 100 columns problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "A table with 100 columns problem " Watch "A table with 100 columns problem " New topic
Author

A table with 100 columns problem

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Today my professor was asking how you will get values of table which has 100 columns my reply was like below code

but she said this is not feasible ... you need to simplify it , and then she said you have to make use of enumeration , now can any one explain me after getting resultset object how you can get a enumeration and then how you can access all the values from each columns ?


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Hi ,


In the Java API there is a class called ResultSetMetaData .Just refer the same and try below.

ResultSetMetaData object you will get from the ResultSet object.




Also you will get the Total number of columns and columns from the ResultSetMetaData object , so that you will not depend on the count of columns given by teacher P
This count can be used for fetching data from the REsultset or if you want to fetch with column names then can refer below code , it will also give you the data type of that column also ...



and now you can store them in the array and use later while iterating over resultset object or can try like this




- Chetan
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Chetan Dorle wrote:Hi ,


In the Java API there is a class called ResultSetMetaData .Just refer the same and try below.

ResultSetMetaData object you will get from the ResultSet object.




Also you will get the Total number of columns and columns from the ResultSetMetaData object , so that you will not depend on the count of columns given by teacher P
This count can be used for fetching data from the REsultset or if you want to fetch with column names then can refer below code , it will also give you the data type of that column also ...



and now you can store them in the array and use later while iterating over resultset object or can try like this




- Chetan

thanks for the reply ...
I know this could be possible , but My teacher was specific with a point called "enumeration" she was telling me that using "enumeration" you can do that , can you show me e.g: using enumeration ?
because as far as I know there is no method with resultset object that returns enumeration !
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

There is no method in the ResultSet interface which would use or return an Enumeration, so your teacher was most probably speaking about something similar to what Chetan suggested. Other possible interpretation could be your teacher simply wanted you to replace the hundred of statements with a loop.

I'd say that using resultset's metadata should be rather rare in Java. In most cases you should explicitly state which columns are you selecting (that is, no SELECT * FROM ...), and therefore you'll know beforehand which columns and on which indexes to process. Only if you write generic code which (for example) processes queries supplied by the user you'd need to analyze the metadata of the resultset. See also SQL column lists.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Martin Vajsar wrote:There is no method in the ResultSet interface which would use or return an Enumeration, so your teacher was most probably speaking about something similar to what Chetan suggested. Other possible interpretation could be your teacher simply wanted you to replace the hundred of statements with a loop.

I'd say that using resultset's metadata should be rather rare in Java. In most cases you should explicitly state which columns are you selecting (that is, no SELECT * FROM ...), and therefore you'll know beforehand which columns and on which indexes to process. Only if you write generic code which (for example) processes queries supplied by the user you'd need to analyze the metadata of the resultset. See also SQL column lists.


when i did use the metadata (our app could be pointed at any customer database) we used to store the results in a hashmap of column name->value so a List<Map<String,String>>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A table with 100 columns problem