• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic