File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A table with 100 columns problem

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1107
Eclipse IDE Oracle VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic