JDBC 中查询 Oracle 数据库的日期范围(Java 中)

发布于 2024-10-20 19:07:23 字数 343 浏览 1 评论 0 原文

String sql="select id from period where '"+systemDate+"' between startDate and endDate";

我需要执行这个查询,并且我使用的是 Oracle 10g。我想获取这些日期之间的 id 。我面临的问题是在 Oracle 中,我的日期格式是“dd-mon-yy”,而 systemDate 的格式是“yyyy-mm-dd”。那么有没有办法将Java日期转换为dd-mon-format或任何其他方式来执行上述查询?

String sql="select id from period where '"+systemDate+"' between startDate and endDate";

I need to execute this query and I am using Oracle 10g. I want to get the id which falls between these dates. Problem I am facing is in Oracle my date format is "dd-mon-yy" and systemDate is in format "yyyy-mm-dd". So is there any way to convert Java date to dd-mon-format or any other way to execute the above query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看轻我的陪伴 2024-10-27 19:07:23

您应该将 java.util.Date 转换为 java.sql.Date 并使用PreparedStatement,如下所示:

java.util.Date jStartDate = ...; //your "java" date object
java.sql.Date startDate = new java.sql.Date(jStartDate.getTime());

java.util.Date jEndDate = ...;
java.sql.Date endDate = new java.sql.Date(jEndDate.getTime());

PreparedStatement p = connection.prepareStatement("select id from period where systemDate between ? and ?");
p.setDate(1, startDate);
p.setDate(2, endDate);
ResultSet rs = p.executeQuery();

You should convert your java.util.Date to a java.sql.Date and use a PreparedStatement as shown below:

java.util.Date jStartDate = ...; //your "java" date object
java.sql.Date startDate = new java.sql.Date(jStartDate.getTime());

java.util.Date jEndDate = ...;
java.sql.Date endDate = new java.sql.Date(jEndDate.getTime());

PreparedStatement p = connection.prepareStatement("select id from period where systemDate between ? and ?");
p.setDate(1, startDate);
p.setDate(2, endDate);
ResultSet rs = p.executeQuery();
奶茶白久 2024-10-27 19:07:23

Java 有日期格式化类。 DateFormat 是这些的抽象父类,SimpleDateFormat 这里 可能是你想要的,

SimpleDateFormat oracleStyle = new SimpleDateFormat("dd-MM-yy");
String dString = oracleStyle.format(systemDate);
String sql="select id from period where '"+systemDate+"' between startDate and endDate";

我还没有测试过。格式调用可能需要新的字符串缓冲区作为参数,并且我的格式字符串可能会关闭,但这就是想法。阅读文档

Java has date formatting classes. DateFormat is the abstract parent class for these, SimpleDateFormat here is probably the one you want

SimpleDateFormat oracleStyle = new SimpleDateFormat("dd-MM-yy");
String dString = oracleStyle.format(systemDate);
String sql="select id from period where '"+systemDate+"' between startDate and endDate";

I havent tested this. The format call might need new string buffers as arguments, and my format string could be off, but this is the idea. Read the documentation

海拔太高太耀眼 2024-10-27 19:07:23

更新:其他答案现已过时。现代解决方案使用 JSR 310 中定义的 java.time。它们取代了存在严重缺陷的遗留日期时间类,例如 DateCalendarSimpleDateFormat

使用参数编写 SQL。使用 ? 作为运行时传入的值的占位符。

使用文本块以便于阅读。

切勿在日期时间工作中使用 SQL 命令BETWEEN。时间跨度最好使用半开放方法来定义,其中开始包含,而结束排除。相反,BETWEEN 是完全封闭的,以包含两个端点。例如,一个月从该月的第一天开始,一直持续到(但不包括)下个月的第一天。

String sql = """
        SELECT id_ 
        FROM event_ 
        WHERE NOT ( date_ < ? ) 
        AND date_ < ? 
        ;
        """ ;

你说:

oracle 我的日期格式是“dd-mon-yy”

不,不正确。 DATE 列不是文本。所以它没有“格式”。

你说:

系统日期的格式为 yyyy-mm-dd。

在 Java 中,使用日期时间对象来保存日期时间值。日期时间对象不是文本,因此它们没有“格式”。

如果将一些文本作为输入日期,则解析为 java.time.LocalDate 对象。您指定的格式符合 ISO 8601 标准。默认情况下,java.time 类中使用该标准来生成/解析文本。因此无需指定格式模式。

LocalDate start = LocalDate.parse( "2024-01-01" ) ;
LocalDate end = start.plusMonths( 1 ) ; // Cover month of January of 2024.

将这些对象作为要替换 SQL 语句中 ? 占位符的值传递。

myPreparedStatement.setObject( 1 , start ) ;
myPreparedStatement.setObject( 2 , end ) ;

Update: Other Answers are now outdated. The modern solution uses the java.time classes defined in JSR 310. These supplanted the terribly flawed legacy date-time classes such as Date, Calendar, and SimpleDateFormat.

Write your SQL using parameters. Use ? as a placeholder for the value to be passed in at runtime.

Use text blocks for easier reading.

Never use the SQL command BETWEEN in date-time work. Spans of time are best defined using the Half-Open approach, where the beginning is inclusive while the ending is exclusive. In contrast, BETWEEN is fully closed, to include both endpoints. For example, a month starts on the first of the month and runs up to, but does not include, the first of the following month.

String sql = """
        SELECT id_ 
        FROM event_ 
        WHERE NOT ( date_ < ? ) 
        AND date_ < ? 
        ;
        """ ;

You said:

oracle my date format is "dd-mon-yy"

No, incorrect. A DATE column in Oracle database is not text. So it has no “format”.

You said:

systemDate is in format yyyy-mm-dd.

In Java use date-time objects to hold date-time values. Date-time objects are not text, so they have no “format”.

If handed some text as your input date, parse as a java.time.LocalDate object. Your specified format complies with ISO 8601 standard. That standard is used by default in the java.time classes for generating/parsing text. So no need to specify a formatting pattern.

LocalDate start = LocalDate.parse( "2024-01-01" ) ;
LocalDate end = start.plusMonths( 1 ) ; // Cover month of January of 2024.

Pass these objects as the values to be substituted for the ? placeholders in our SQL statement.

myPreparedStatement.setObject( 1 , start ) ;
myPreparedStatement.setObject( 2 , end ) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文