Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

oracle query

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oracle allows to pass DEFAULT as column value in the query To quote from oracle website

For e.g.

If the departments table had been created with a default value of 121 for the manager_id column, then you could issue the same statement as follows:



I want to write a single query using Prepared statement in JDBC which works for both default and non default case.



For non default mangers I will use the

For default manager what should I pass. Any idea ?


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34681
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, you can't. Why not just use two queries in JDBC?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Actually the queries are auto generated by a tool.
 
Lukas Eder
Ranch Hand
Posts: 48
5
Java Oracle Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot bind the DEFAULT SQL keyword to a JDBC bind variable. You could, however, solve this transparently using a trigger as such:



The above assumes that manager_id is a nullable column. If the column is NOT NULL, then you might be more lucky replacing the original INSERT through an INSTEAD OF trigger
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but triggers are not allowed in the application.
 
Lukas Eder
Ranch Hand
Posts: 48
5
Java Oracle Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep bhatt wrote:Thanks but triggers are not allowed in the application.


Then you cannot do it with a single statement. You'll have to resort to two statements. One with a bind value, and another with the DEFAULT keyword.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility would be to supply the default values directly from the Java side. Oracle database can be queried for default values of columns, see eg. here.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic