我正在使用jooq为Oracle构建查询。除日期外,一切正常:
public static void main(String[] args) throws SQLException {
java.sql.Timestamp now = new java.sql.Timestamp(new Date().getTime());
Connection con = DriverManager.getConnection(... , ... , ...);
final Factory create = new OracleFactory(con);
Statement s = con.createStatement();
s.execute("create table test_table ( test_column DATE )");
s.execute("insert into test_table values (to_date('20111111', 'yyyymmdd'))");
// -- using to_date
ResultSet rs = s.executeQuery("select count(1) from test_table where test_column<to_date('20121212', 'yyyymmdd')");
rs.next();
System.out.println(""+rs.getInt(1));
rs.close();
// -- using a preparedstatement with java.sql.timestamp
PreparedStatement ps = con.prepareStatement("select count(1) from test_table where test_column<?");
ps.setTimestamp(1,now);
rs = ps.executeQuery();
rs.next();
System.out.println(""+rs.getInt(1));
rs.close();
// -- using jooq with java.sql.timestamp
final org.jooq.Table<org.jooq.Record> table = create.tableByName("TEST_TABLE");
final org.jooq.SelectSelectStep sss = create.select(create.count());
final org.jooq.SelectJoinStep sjs = sss.from(table);
final org.jooq.SelectConditionStep scs = sjs.where(create.fieldByName("TEST_COLUMN").lessThan(now));
System.out.println(scs.toString());
rs = s.executeQuery(scs.toString());
rs.next();
System.out.println(""+rs.getInt(1));
rs.close();
s.close();
}
提供以下输出:
1
1
select count(*) from "TEST_TABLE" where "TEST_COLUMN" < '2012-12-12 19:42:34.957'
Exception in thread "main" java.sql.SQLDataException: ORA-01861: literal does not match format string
我原本以为JOOQ会在lessThan(Object)中检查Object的类型 确定它是否能够提出合理的转换,但显然是这样 在这种情况下只做一个Object.toString()。我还记得我在MySQL中通过JOOQ从未遇到日期查询问题(虽然这是一段时间了)。我做错了什么?
答案 0 :(得分:1)
我怀疑这个问题是由于create.fieldByName()
不知道列的类型(因此,对象),并在比较谓词的右侧强制未知类型。这应该在jOOQ中修复。我已为此注册了#2007:
https://github.com/jOOQ/jOOQ/issues/2007
同时,尝试在您的字段上明确设置类型:
create.fieldByName(Timestamp.class, "TEST_COLUMN").lessThan(now)