views:

41

answers:

1

I am working in Java, inserting data from HashMaps of certain types into a SQL database. It has produced some code like this:

for ( String key : strings.keySet() ) {
    result_set.updateString(key, strings.get(key));
}

for ( String key : booleans.keySet() ) {
    result_set.updateBoolean(key, booleans.get(key));
}

for ( String key : dates.keySet() ) {
    result_set.updateDate(key, dates.get(key));
}

I am used to Ruby, where code like this would take up one line and I can't believe I have to do it like this in Java. I must be wrong. Is there a better way? I assume using Generics? I tried using result_set.updateObject(key, object) but it gave me "SQLException: Unable to convert between java.util.Date and VARCHAR."

+1  A: 

Take a look at MyBatis, an SQL Mapper that handles the mapping between POJOs (including Maps and Lists) and SQL. It's saved us a ton of work relative to what it would take to do everything ourselves in raw JDBC.

Jim Ferrans
I'll definitely take a look when I get back to work tomorrow. Thanks!
AndrewKS