Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Oracle jdbc + null value for primitive type

 
Ryan Crum
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to insert 'null' into a numeric column in Oracle (the column does allow null fields) however doing this in a pain free manor has proven difficult so far. I have tried the following:
PreparedStatement ps ....
// The setObject(...) doesnt work, "Invalid column type" error is returned
ps.setObject( idx2col, null );
The following code works:
ps.setNull( idx2col, Types.INTEGER );
The problem with the above code is that it requires that I know the type of the field the object will be going into. I do not know this information because the function that executes this PreparedStatement is very general. Sometimes the column that will be set to null is a number, sometimes its a string or date...
Does anyone have suggestions as to how a null can be inserted into the column without knowing the type (Using Oracle + their thin jdbc driver)?
Thanks.
 
Stanley Tan
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I just went through a similar problem. It's really annoying to know that it works for everyone else. Anyway, I've got the same setup as you (Oracle, thin driver). What I did what to use a Statement instead of a PreparedStatement and then just append the query on.
 
Amit Da
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a preparedStatement one can easily know the jdbc type of any parameter.(refer to jdk1.4 javadoc of PreparedStatement). prepStmt.getParameterMetaData returns ParameterMetaData which has getParameterType(int param) giving jdbc type. Now pstmt.setNull(idx, jdbcType) can be called with ease
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"amitda",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic