Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing straight JDBC? JPA?
what have you tried already?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case it should be moved to our Spring forum.
 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic