For creating google suggestion kind of functionality ( on key press retrive a list of results) :
1) query the SQL table and fetching the records(using JDBC) every time based on key press.
2) fetch the records made to objects , put in a list and serialized already. on key press deserialize that list, manipulate the list(check for startswith()) and showing the results.
Querying the database on ever character change does sound like it might need some sort of caching mechanism, especially if you need some sort of expiry mechanism and more control over what is cached. Of course, as with any potential performance problem try it first, see if you actually have a problem. Serialization is not likely to be a good choice, because its limited and the extra persistance step is probably going to be slower than going to the DB every time. I'd also suspect this may need to be asynchronous; it's likely to drive users up the wall if they have to wait for the applciation to respond before entering another character.
Have a google for "java caches" there are a whole bunch of them out there that might suit your needs.
Bauke Scholtz wrote:Querying the DB everytime is in fact faster and less memory hogging.
True iff your searching mechanism is optimized.
For systems that don't have a search tool and allow queries directly against the database using things like wildcard values, searches could be incredibly slow. Granted, you wouldn't want to cache/store every wild card either.