Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Oracle jdbc + null value for primitive type

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This one time, at bandcamp, I had relations with a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic