aspose file tools*
The moose likes Servlets and the fly likes How to select columns of a table dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to select columns of a table dynamically?" Watch "How to select columns of a table dynamically?" New topic
Author

How to select columns of a table dynamically?

Stevenson Anderson
Greenhorn

Joined: Jul 09, 2011
Posts: 13
Hi !
I am new to coding.I got a requirement where the user could see the columns available in the table. And then he selects only few of the columns to see the data.
So I am planning to put a check box to every column so whatever the columns selected by the user we can identify.But the problem is how can I write a query after taking the database connection
for dynamic selection of columns?

Please suggest me the ways to do this.

Thanks in advance.

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

If performance is not a consideration, you can choose to get all the data anyway and filter what to show to the user based on their selection criteria.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

The above seems to be a valid suggestion. Else you will have to dynamically fire a query for every set of selections, which could cause a performance issue.

Learning and Learning!-- Java all the way!
Rojan punn
Greenhorn

Joined: Nov 29, 2011
Posts: 17


Selecting all coulmn values can be an overhead .What is the problem in creating the SQL dynamically eg. if you are a web developer you can create a check box with column name as name of check box and create dynamic sql in java script and pass it to you java backend?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Will that not fire a connection to the database, each time the selection of columns are changed? I believe that would be an overhead.
Rojan punn
Greenhorn

Joined: Nov 29, 2011
Posts: 17

It will create a connection But if you load the entire data the page loading will be very slow and It is ok to create a connection if you are closing it after use.
And there is no way we can get the data with out a connection.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Again I think the approach is well determined by the size of the returned dataset.The tradeoff would be better judged then.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Worrying about overhead before it's known that there's something to worry about is called premature optimization and should be avoided. Do what makes the most sense, and then, and only then, if there's a performance issues, address it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rojan punn
Greenhorn

Joined: Nov 29, 2011
Posts: 17


@Bear Bibeault .IS it a Pre mature optimization really?

Sorry if i dint ask the size of the application here..

It is well known that selecting 100 column is much slower than selecting 10 cloumns.So Is it not good to think usr will not need all columns always?

I am not questioning you ..problem here is about the design of the application...

Thinking all possible overhead (at least expected ones)is also a good way of design i believe.

Is above not true?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Bear Bibeault wrote:Worrying about overhead before it's known that there's something to worry about is called premature optimization and should be avoided. Do what makes the most sense, and then, and only then, if there's a performance issues, address it.


Very true.

It is well known that selecting 100 column is much slower than selecting 10 cloumns


We are talking about hypothetical numbers here. Do we really know that the OP is dealing with such numbers ? It is always easier to choose the simplest solution and then chisel away at it if necessary.
 
wood burning stoves
 
subject: How to select columns of a table dynamically?