jdbc对两个视图的一个普通查询 
视图A,视图B 
SELECT A.U_NAME,A.U_PASSWORD,A.U_KIND,B.GM_GROUP FROM A,B WHERE A.U_NAME=B.GM_MEMBER AND A.U_NAME=? 
preparestatement传参,build一切正常 
到prepare.executeQuery();这一步 
出现下面异常: 
java.sql.SQLException: ORA-01861: literal does not match format string 
上网查了很久,都说这跟oralce 日期类型字段的转型格式有关系 
但我这两个视图的字段没有任何日期类型,两个视图的构成语句因为是公司项目,不太方便公布 
里边有一些date类型的字段比较,比如: 
WHERE 
TabelA.STARTYMD IS NULL OR 
TabelA.STARTYMD <= TO_CHAR(SYSDATE, 'YYYYMMDD') 
在这里,我实验了一下,如果把TabelA的STARTYMD字段赋值成2009/02/16,则当程序走到prepare.executeQuery();这一步时肯定出现ORA-01861: literal does not match format string异常;如果指定STARTYMD字段的值为2009/02/16 11:11:11的话,也会同样抛出异常,如果指定成NULL则一切ok。 不知道各位有没有遇到过这种情况,我自己感觉可能跟oracle server对日期的默认格式设置有关,但又不知道查询视图时oracle都有什么机制。麻烦大家帮着看看给点思路。谢谢先 

解决方案 »

  1.   

    SQL Error: ORA-01861: literal does not match format string
    01861. 00000 -  "literal does not match format string"
    *Cause:    Literals in the input must be the same length as literals in
               the format string (with the exception of leading whitespace).  If the
               "FX" modifier has been toggled on, the literal must match exactly,
               with no extra whitespace.
    *Action:   Correct the format string to match the literal.
    尝试把执行的查询sql打印出来,放在sqlplus上运行看看结果是否正确另将 TO_CHAR(SYSDATE, 'YYYYMMDD')  换成 TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24miss') 试一下还有一种可能的猜测,是不是字符集的关系?
      

  2.   

    日期类型与字符串类型比较是不行的,应该这样吧
    .................
    WHERE 
    TabelA.STARTYMD IS NULL OR 
    TO_CHAR(TabelA.STARTYMD, 'YYYYMMDD') <= TO_CHAR(SYSDATE, 'YYYYMMDD') 
      

  3.   

    ORA-01861 literal does not match format stringCause: Literals in the input must be the same length as literals in the format string (with the exception of leading white space). If the "FX" modifier has been toggled on, the literal must match exactly, with no extra white space.Action: Correct the format string to match the literal.
      

  4.   

    谢谢提供方案
    执行的sql就是下面的这个
    SELECT A.U_NAME,A.U_PASSWORD,A.U_KIND,B.GM_GROUP FROM A,B WHERE A.U_NAME=B.GM_MEMBER AND A.U_NAME=? 
    A和B是两个视图,视图自身的检索语句里有日期的转换,对视图的检索并没有涉及到日期类型的字段
    这个语句在sqlplus上是没有问题的
      

  5.   

    谢谢大家的回复
    实在抱歉,我提问题的疏漏
    TabelA.STARTYMD是Date类型的字段 
      

  6.   

    To:dawugui
    我的视图自身的检索很正常,想要的数据也都能取得
    可为什么一到检索视图的时候,就会出现异常呢?
    现在视图自身的检索用到了很多的日期比较
    基本都是TabelA.STARTYMD <= TO_CHAR(SYSDATE, 'YYYYMMDD')这样的比较
    STARTYMD是Date类型的字段,我尝试过把TO_CHAR(SYSDATE, 'YYYYMMDD')中的format更改成各种形式
    但仍然抛出异常,TabelA.STARTYMD字段的值也跟上面的format一致修改了很多形式,都没有效果。仍然异常。
      

  7.   

    TabelA.STARTYMD是日期类型字段,不能和字符直接进行比较
    TabelA.STARTYMD IS NULL OR
    TabelA.STARTYMD <= TO_CHAR(SYSDATE, 'YYYYMMDD')TabelA.STARTYMD IS NULL OR
    TabelA.STARTYMD <= trunc(SYSDATE)