0

我是 JDBC 新手,在执行一些查询时,我得到的结果不一致。

如果我在 sql developer(连接到 Oracle DB)中执行此查询,我会得到 4 个结果

SELECT *
FROM someTable1 some1 
JOIN someTable2 some2 on (some1.some_id= some2.other_id)
WHERE some2.some_date LIKE '01/01/01' OR some2.some_date IS NULL

然后,我从 java 中的属性文件加载相同的查询并执行查询并获得 0 个结果……有人知道为什么会这样吗?我首先怀疑属性值中的单引号,但我不知道......

在此先感谢并原谅我糟糕的英语!:)

4

1 回答 1

2

The query doesn't contain any special characters which could confuse Java, the properties loader or JDBC, so you should get exactly the same results in SQL Developer and with JDBC.

In fact, SQL Developer is written in Java, so it is using JDBC to execute the queries.

Print the query to the console before you execute it to make 100% sure the code executes the query that you have in mind.

Next, you should check the type of some_date. LIKE is only defined for string types (VARCHAR and similar), not for date/time types.

Oracle has a set of helper functions to build queries for date/time types, for example:

some_date = to_date( '01/01/2001','mm/dd/yyyy')

or

TRUNC(some_date, 'DAY') = to_date( '01/01/2001','mm/dd/yyyy')

The second query strips hours, minutes, seconds, etc. from the column and will compare only days, months and years.

Note: Always use 4-digit years to avoid all kinds of odd problems.

于 2013-01-07T15:27:37.700 回答