File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes i want to put column name as key and row as value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "i want to put column name as key and row as value." Watch "i want to put column name as key and row as value." New topic
Author

i want to put column name as key and row as value.

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
from database we retrieve data after that i want to put column name as key and row as value
so key are same for all row(like-select * from emp) means i want all data put in a map.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

Are you doing straight JDBC? JPA?
what have you tried already?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You need to first read table's metadata in order to obtain column names that you would use as a key for your map. As I understood your requirement, for the given key (column name) value would be list of strings (or whatever type) containing values stored in that column.

Though I have no idea why you would want to do something like that... Database contains structured data (or at least it should), and storing data in maps by column name doesn't sound so reasonable to me.


The quieter you are, the more you are able to hear.
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
Kemal Sokolovic wrote:

Though I have no idea why you would want to do something like that... Database contains structured data (or at least it should), and storing data in maps by column name doesn't sound so reasonable to me.


actually this is from spring where List l=queryForList("select Query") is apredefined method which return List and internally convert each row in a map and he takes column name as a key and a row as a value after that he puts all map object in a ArrayList and arraylist return
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
In which case it should be moved to our Spring forum.
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
Campbell Ritchie wrote:In which case it should be moved to our Spring forum.


but i want to implement like spring querForList().so qestion belongs to collection topic(i am not asking how method working).
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Ritesh raushan wrote:
Campbell Ritchie wrote:In which case it should be moved to our Spring forum.


but i want to implement like spring querForList().so qestion belongs to collection topic(i am not asking how method working).


What are you asking? We are confused? Are you having a problem running JdbcTemple.queryForList() Then it belongs in the Spring Forum here.

But I don't think that is what you are asking. I think you want to do something similar to what queryForList is doing.

But if you are using Spring, I would use jdbcTemplate.queryForList() rather than re-inventing the wheel and go from 1 line of code to 30 or more.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Kemal Sokolovic wrote:Though I have no idea why you would want to do something like that... Database contains structured data (or at least it should), and storing data in maps by column name doesn't sound so reasonable to me.


Actually for some use cases it is the most reasonable and better way.

Let me give you an exact instance.

I had a Charting UI component to make a Pie chart. To determine the pie slices names and size the component wanted a Map, where the key is the pie slice name and the value was the size. Why go to the database, get a ResultSet, have to convert it to a Domain object to pass up to the UI layer, then write code to convert the domain object into a Map. Why not query for that Map at the DAO layer with one line of code, and just pass the Map up to the client.

Mark
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Well, that was a good example, now I have an idea why something like that should be done. Thanks for clarification!

I guess I've lost the big picture because the original post didn't contain more details about possible applications of that approach (which is why I like OP to provide details like that), and I thought only of the basic case - read data from each column, and store it into Map to manipulate it outside the database. Hence, it didn't make sense to me because it would be difficult and more complex to keep database structure.

Edit: Little off topic, but I just saw that with this one I have published 0.5kPosts!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Kemal Sokolovic wrote:Well, that was a good example, now I have an idea why something like that should be done. Thanks for clarification!

I guess I've lost the big picture because the original post didn't contain more details about possible applications of that approach (which is why I like OP to provide details like that), and I thought only of the basic case - read data from each column, and store it into Map to manipulate it outside the database. Hence, it didn't make sense to me because it would be difficult and more complex to keep database structure.

Edit: Little off topic, but I just saw that with this one I have published 0.5kPosts!


Exactly, there are some, few use cases where it really does make sense. But most of the time it doesn't

Yes, there are some details missing from the original posters posts.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i want to put column name as key and row as value.