Sunday, July 26, 2009

Rewriting java files responsible for database connection

I am rewritting the java files responsible for database connection because I find many codes I write are very much repeating, in part especially for database inquires.

Metadata.java
I originally wrote the searchSql function repeatedly, since the query criteria are different, however, the result set are generally same. So I separated out the different part into several method:
  1. search(String keywords); - searches for metadata with title or description like keyword.
  2. browse(); - searches all metadata exist in the metadata table.
  3. browse(String category); - searches all metadata with matching category
Users.java
Originially I had copied quiet a few methods from metadata.java to users.java because I hadn't figure out how to call methods from other classes. Now, I will call a constructor class in the UserServlet.java file:
Metadata md = MetadataServlet.geMetadataClass();
then I'll be able to call the delete method from metadata.java which delete selected game from the database. In addition, I'm able to search for all games submitted by this user from metadata.java by calling md.searchUserID(output, username)

No comments:

Powered By Blogger