views:

323

answers:

1

I have this method to insert data using jdbc that will insert the value according to the java type. Something like this:

Object o = map.get( key );

if( o == null ) {
    // setNull( index );
} else  if( o instanceof String ) {
    // setString( index, (String) o );
} else if( o instanceof Timestamp ) {
    // setTimestampt( index, ( Timestamp ) o );
} else if( o instanceof Integer ) {
    // setInt( index, (Integer) o );
}
    index++;

It has a problem though ( beside it is all commented :P )

If the value of o is null, I'm requiered to use "setNull(int, int)" method, but I have to specify the SQL type:

...Note: You must specify the parameter's SQL type.

But... I don't know the type, so I'm considering use always VARCHAR ( just because )

setNull( index,Types.VARCHAR); .

What would happen if I set null in prepared statement with varchar always?

I'm using:

Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

With Oracle JDB driver:

11.1.0.7.0-Production ( ojdbc6.jar )

What would be an alternative?

+2  A: 

Could you not interrogate the database and get the column type via the ResultSetMetaData interface?

Rob
It's a bit complicated. I'm fooling around with an extra extra dynamism ( sort of ) so I don't really have the exact position of the column. But instead I'm setting the position based on the column name ( like EMPL_ID may be the 20th column but I'm setting it like the 1st on my pstmt..... )
OscarRyz
Wait, wait... it is starting to make sense.... mmmhhh .... let me chew it for a while... What if I do ( select * from table where 1 = 0 ; get the ResultSetMetadata and get the type using the columnname ... ( o no! I see another Map<String,String> in my future... ) ...
OscarRyz
Can't you get as input the value and the type instead of just the value as you are getting now?
OpenSource
@OpenSource... I might, it's just the source is not a SQL table
OscarRyz
Love the Map<String,String>s, Oscar. Love them like a brother.
Rob