The Beauty of Java Hashtable (Old, 2010)

Sometimes newbies can’t quite grasp how to use Java to get data from a database in an easy way.
A simple hashtable goes a long way in web development. for instance:

String sql = “select name,age,dob from employee”;

//notice the use of String key and Object value, since data from the database comes in many forms:Decimal,varchar,boolean
Hashtable<String,Object><string,object><string,object><string,object> employees = new Hashtable<string,object><string,object><string,object><String,Object><string,object>();

int count 0;

ResultSet rs = statement.executeQuery();

This simple routine will give the novice developer a new weapon to his her arsenal.
Here we have a generic way store database data in an array.
If the data was all varchar, which is Java String, we could have created a hashtable with a signature of <String,String><string,string>. However, <String,Object>String,Object> gives the flexibility we need.

Don’t forget to increment that counter!

Author: Michael Clayton


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: