This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes How to determine datatypes of the parameters runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to determine datatypes of the parameters runtime" Watch "How to determine datatypes of the parameters runtime" New topic

How to determine datatypes of the parameters runtime

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hello All,

I have a very simple basic question on determining the datatypes of the parameters set to a prepared statement at a run time. I searched JDBC forum but could not find any answer.

I'm writing one common Database utility class file which creates a connection, executes dynamic queries and set the results back.

my method is as below

from the above code, cleary I'm sending a string like
"insert into table ? values(?,?,?)". I want this utility class file to be used across my different projects, so apparently I do have any info about what columns are there in the table. I want my calling class to be completely unaware that it is using JDBC. So it just sends a string format of the query with place holders. But I know, it should also send the parameters as method arguements. But how would my method determine the type of the parameters. I would put my resultset into collection and send it back to calling class to deal with.I do not want to have any overloaded methods also in my class because of huge permutations and combinations.

Any ideas?

Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1138

I know that with Oracle database and JDBC driver, most times you can use method "setObject()".
If one of the parameters is null, then you need to know the database type.
And "setObject()" doesn't work with some database types like CLOB and BLOB.

As far as I know, INSERT statements don't return result sets, but for queries, you should be able to use the "getObject()" method on most occasions. Again, this may not be suitable for CLOB and BLOB columns and you may also have a problem with NULL column values.

Good Luck,
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi Avi,

Yes INSERT would not result into any resultsets. I just mentioned as a an example. My intention is to replace query with any simple to complex SELECT statements. Probably my question is very vague and too ambituous. I'm also looking into other options like RowSet interface of JDBC 2.0 API. Did anyone see the benefits of RowSet?
I agree. Here's the link:
subject: How to determine datatypes of the parameters runtime
It's not a secret anymore!