简体   繁体   English

在Oracle SQL Developer中运行的SQL查询但是当我从我的JAVA程序运行查询时,它给出了Sql异常

[英]Sql Query running in oracle SQL Developer But when i run the query from my JAVA program it is giving Sql exception

I am fetching some data using the following query 我使用以下查询获取一些数据

SELECT    
t.value_date,t.settlement_date,    
  (CASE WHEN NVL( t.settlement_date, '01-01-9999')='01-01-9999'
    THEN t.value_date
    ELSE t.settlement_date
  END) AS modified_value_date,    
  t.instrument_pk,    
  bct.base_ccy_pk,    
  i.contract_size,    
  lc.min_trading_unit AS min_trading_unit_lc,    
  bc.min_trading_unit AS min_trading_unit_bc    

FROM transaction t,    
  bc_transaction bct,    
  instrument i,    
  instrument lc,    
  instrument bc

WHERE t.status         = 'NORMAL'    
AND t.fund_pk          = 99436    
AND t.instrument_pk    = 235342    
AND t.instrument_pk    = i.instrument_pk    
AND t.local_ccy_pk     = lc.instrument_pk    
AND bct.transaction_pk = t.transaction_pk    
AND bct.base_ccy_pk    = bc.instrument_pk    
AND t.value_date      >= to_date('20121123000000','yyyymmddhh24miss')    
AND t.transaction_type = 'SECURITY_TRADE'    
ORDER BY modified_value_date,    
  t.processing_order,    
  t.txn_reference_no;

Problem is when im running the above query in Oracle SQL Developer it's giving the proper result. 问题是当我在Oracle SQL Developer中运行上述查询时,它会给出正确的结果。 But when i am trying to run the query from my java program using PreparedStatement it is producing an exception like java.sql.SQLException: ORA-01843: not a valid month 但是,当我尝试使用PreparedStatement从我的java程序运行查询时,它产生一个异常,如java.sql.SQLException:ORA-01843:不是一个有效的月份

My problem got solved when i replace the date in CASE bolck with 当我用CASE bolck替换日期时,我的问题得到了解决

(CASE WHEN NVL(t.settlement_date,to_date('99990101000000','yyyymmddhh24miss'))=to_date('99990101000000','yyyymmddhh24miss') 
    THEN t.value_date 
    ELSE t.settlement_date 
  END) AS modified_value_date

Now My questions is 现在我的问题是

  1. why there is such kind of discrepancy ?If the problem is with the to_date() then it should have given the same exception in SQL developer also. 为什么会出现这种差异 ?如果问题出在to_date()那么它应该在SQL开发人员中也给出了相同的异常。
  2. The problem does not end here I saw when I m debugging my program from eclipse again the same query is running perfectly without to_date() in NVL block the I checked and found that my JDK is 1.5 but the JRE was 1.6.Then I changed my JRE to 1.5 and debug the same java program and found that it is producing the same exception ,for the same query without to_date() in NVL block, at the time of debugging. 问题不会在这里结束我看到当我再次从eclipse调试我的程序时,相同的查询运行完全没有在NVL块中的to_date()我检查并发现我的JDK是1.5但是JRE是1.6。然后我改变了我的JRE为1.5并调试相同的java程序,并发现它在调试时对于NVL块中没有to_date()的相同查询产生相同的异常。

    Now I m completely messed up why such kind of discrepancy between JRE 5 and JRE 6 ? 现在我完全搞砸了为什么JRE 5和JRE 6之间存在这种差异

CAN ANYONE HELP ME? 谁能帮我?

It is not about JRE version but about your session settings. 它不是关于JRE版本,而是关于您的会话设置。 You have specified string and left Oracle session to implicitely convert to date using NLS setting (likely NLS_DATE_FORMAT). 您已指定字符串并离开Oracle会话以使用NLS设置(可能是NLS_DATE_FORMAT)隐式转换为日期。
Do yourself a favor - never leave hardcoded value to depend on external setting. 帮自己一个忙 - 永远不要让硬编码的价值取决于外部环境。
You can use DATE'9999-01-01' instead. 您可以使用DATE'9999-01-01'代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM