如何编写指令来比较SQL中的“Date”和“Time”等字段,并将“Date”和“Time”作为参数进行比较?

时间:2011-08-31 08:26:10

标签: mysql

我有以下Java代码来访问DB2以查找是否存在记录:

public static boolean isMailStored(Connection connection, Date giornogiorno,Time oraora) {  
    try {
        System.out.println("oraora: "+oraora);
        String CHECK_MAIL = "select count(*) from x.MAILDAIMAP where MNROMAIL = ";
        CHECK_MAIL = CHECK_MAIL + oraora;           
        System.out.println("Statement: "+CHECK_MAIL);
        ResultSet rs=st.executeQuery(CHECK_MAIL);

System.out.println显示:

oraora: 10:27:20
Statement: select count(*) from x.MAILDAIMAP where MORAMAIL = 10:27:20
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0104N  an unespected token ":27" was detected after "where MORAMAIL = 10".  The tokens provided may include: "CONCAT".  SQLSTATE=42601

at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(Unknown Source)Error...

at COM.ibm.db2.jdbc.app.DB2Statement.execute2(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2Statement.executeQuery(Unknown Source)
at parthoOriginale.Email.isMailStored(Unknown Source)
at parthoOriginale.ReadMail.processMessages(Unknown Source)
at parthoOriginale.ReadMail.main(Unknown Source)

MORAMAIL在DB2中定义为Time。为什么会出现此错误?如何编写指令以将DateTime等字段与DateTime作为参数进行比较?

1 个答案:

答案 0 :(得分:0)

将Date和Time字段视为实际上只是格式有约束的String / Varchar字段。查询它们时,将比较值视为字符串,即将它们放在引号中。

CHECK_MAIL = CHECK_MAIL + "\"" + oraora + "\"";