• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to populate all items in JTable when corresponding JTextfield is empty  RSS feed

 
Gourav Goyal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am designing a advanced search option in a java project with sqlite in NetBeans. there are 5 different JTextfields and 5 columns, I want to populate JTable with corresponding matching criteria. If a JTextfield is empty then it should select all items of that column.

query i was using is:


but when i leave a JTextfield empty it shows no data in JTable. only method i know is to use if else conditions but that generates 5!= 120 conditions!!!
 
Piet Souris
Rancher
Posts: 1783
55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Gourav,

indeed, if one of the fields is empty, then you do a query that looks for
an empty field in your database. And if there is no such field , then
indeed, you get an empty result.

Easiest that I can think of is to use a StringBuilder first.
Append the 'Select (...)', and then go through all textfields like:

et cetera. Then issue 'pst=conn.prepareStatement(sb.toString);'

Greetz,
Piet
 
Gourav Goyal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Gourav,

indeed, if one of the fields is empty, then you do a query that looks for
an empty field in your database. And if there is no such field , then
indeed, you get an empty result.

Easiest that I can think of is to use a StringBuilder first.
Append the 'Select (...)', and then go through all textfields like:

et cetera. Then issue 'pst=conn.prepareStatement(sb.toString);'

Greetz,
Piet


oh man thanks!! you literally saved me.
one more question..how can i append "; in last to complete the query in the StringBuilder? any special format?
 
Piet Souris
Rancher
Posts: 1783
55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Gourav,

Thanks for your reaction! That's the biggest reward a replier
could possibly get.

After having handled your last textfield, then do:

So, even when all textfields are empty, you should get

"select * from <file>;"

Greetz,
Piet
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!