Friday, May 8, 2009

Misinterpretation (or the reason why FAQ sounds like "fuck you")

I was trying to get the size of a JDBC result set without the usage of some gay code like "while(rs.next()) { //increment an index}" or worse, creating another statement with the query "select count(*) .... " to retreive only the number of rows from the targeted table.
Even though at first you might want to try resultSet.getFetchSize() , it will not get the "fetch size" specified in the nomenclature, i.e. the number of rows you seek. A closer peek at the jdbc documentation states that getFetchSize() "Retrieves the number of result set rows that is the default fetch size for result sets generated from this Statement object" .
The solution for this ? there is no predefined method in the Statement metadata to return the result set size without parsing it... you may need to choose one of the (gay) methods specified above or find some other.

Saturday, May 2, 2009

parse me again blues

Having the DB entity "Message" with ID and text, i was returning with the entity manager a collection of the form Set<Message> . After obtaining this set i needed to line these messages in a forum thread i was building. The problem isn't that i couldn't get the messages but the message list was not sorted by id (which i recon is by default because that's how result sets go), so everytime i refreshed my forum page, i got those messsages in a different order.
My solution? Altough redneck-ish, i've manually sorted the Set<Message> collection before returning it :p