I always wondered about working with Java and databases. I use the database abstraction layer and get my results as follows:
Hashtable h = connection.selectRow("select * from table where id = 5");
However, when returning a series of lines in the same format, it returns this:
ArrayList<Hashtable> a = connection.selectAll("select * from table where id > 5");
Now that I like generics, I would like to make full use of it using
ArrayList<Hashtable<String,String>> a = connection.selectAll("select * from table where id > 5");
But this does not compile:
Cannot convert from ArrayList<Hashtable> to ArrayList<Hashtable<String,String>>.
However, crashed like this, it works:
ArrayList<Hashtable> a = connection.selectAll("select * from table where id > 5"); Hashtable<String,String> h = a.get(0);
This only triggers a security warning like:
Type safety: The expression of type Hashtable needs unchecked conversion to conform to Hashtable<String,String>.
It seems that one step above will do the same as these two lines here, but in one Java refuses to do the conversion. I suspect that some kind of internal mechanism is responsible for this behavior, but has not yet found a reason.
Would anyone like to clarify?
Edit: just clarify. When in my example the error message reports
Cannot convert from ArrayList<Hashtable> to ArrayList<Hashtable<String,String>>.
when in the lines below, Java seems very capable of the same thing that it just told me that it cannot do when I use
Hashtable<String,String> h = a.get(0);
It seems to me that Java lies with me. Not consciously, of course, we love each other. But there must be a reason. And this is what I am trying to find out.