使用 JDBC 在 SQL 中的日期之间搜索? [英] Searching between dates in SQL with JDBC?

查看:21
本文介绍了使用 JDBC 在 SQL 中的日期之间搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个 Java Swing 应用程序,该应用程序从 MYOB 数据库文件中读取数据并在表中显示某些信息.我已经能够成功生成所需的 SQL 语句,但是我无法添加在日期之间进行搜索的功能(我们的数据库非常大,因此我们试图限制结果).我的一个查询示例如下(用 Java 编写):

I'm currently writing a Java Swing application that reads data in from a MYOB database file and displays certain information in a table. I've been able to successfully generate the required SQL statements but I'm having trouble adding the ability to search between dates (our database is pretty large so we are trying to limit results). An example of one of my queries is below (written in Java):

rs = stmt.executeQuery("SELECT sales.InvoiceNumber, sales.ShipToAddress, sales.Date "
                        + "FROM sales, customers "
                        + "WHERE sales.CardRecordID = customers.CardRecordID "
                        + "AND customers.Name = 'Cash Sales' "
                        + "ORDER BY sales.ShipToAddress ASC, sales.Date DESC"
                        + ";");

我有两个日期(它们实际上是 Java 中的字符串,但格式为 dd/MM/yyyy).

I have two dates (they are actually Strings in Java but are formatted as dd/MM/yyyy).

我尝试在我的 WHERE 中使用另一个 AND 子句和 BETWEEN 语句,但我从 JDBC [MYOB ODBC]获取正确操作数的文字值时出错.

I have tried using another AND clause in my WHERE with a BETWEEN statement but I get the following error from JDBC [MYOB ODBC]Error getting the literal value of right operand.

实际声明如下:

rs = stmt.executeQuery("SELECT sales.InvoiceNumber, sales.ShipToAddress, sales.Date "
                        + "FROM sales, customers "
                        + "WHERE sales.CardRecordID = customers.CardRecordID "
                        + "AND customers.Name = 'Cash Sales' "
                        + "AND sales.Date BETWEEN " + sdate + " AND " + edate + " "
                        + "ORDER BY sales.ShipToAddress ASC, sales.Date DESC"
                        + ";");

有人能帮我计算出正确的语法吗?这是缺少的最后一项功能.

Is anyone able to help me work out correct syntax for this? It's the last piece of functionality missing.

sdateedate 的当前值分别是 25/10/2013,因为它们默认为今天的日期.我已经设置了我正在测试的虚拟文件,以确保它将提供具有此日期范围的数据.同样为了澄清起见,我希望搜索日期包含在内.

Current value of sdate and edate respectively are both 25/10/2013 as they default to today's date. I have set up the dummy file I am testing to ensure it will provide data with this date range. Also for clarficiation I would like the search dates to be INCLUSIVE.

推荐答案

在日期前后使用引号:

rs = stmt.executeQuery("SELECT sales.InvoiceNumber, sales.ShipToAddress, sales.Date "
            + "FROM sales, customers "
            + "WHERE sales.CardRecordID = customers.CardRecordID "
            + "AND customers.Name = 'Cash Sales' "
            + "AND sales.Date BETWEEN '" + sdate + "' AND '" + edate + "' "
            + "ORDER BY sales.ShipToAddress ASC, sales.Date DESC"
            + ";");

或者使用准备好的语句可能更安全(例如,如果日期来自不受信任的输入):

Or it might be safer to use a prepared statement (if the dates come from untrusted inputs for example):

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date startDate = formatter.parse(sdate);
java.util.Date endDate = formatter.parse(edate);
PreparedStatement pstmt = connection.prepareStatement("SELECT sales.InvoiceNumber, sales.ShipToAddress, sales.Date "
            + "FROM sales, customers "
            + "WHERE sales.CardRecordID = customers.CardRecordID "
            + "AND customers.Name = 'Cash Sales' "
            + "AND sales.Date BETWEEN ? AND ? "
            + "ORDER BY sales.ShipToAddress ASC, sales.Date DESC");
pstmt.setDate(1, new java.sql.Date(startDate.getTime()))
pstmt.setDate(2, new java.sql.Date(endDate.getTime()))

between 运算符是包含性的,但如果您的数据库字段实际上是时间戳,则假定没有时间的日期为 00:00:00.000 时间.因此,在这种情况下,为了使您的日期包含在内,您可以将一天添加到您的结束日期.从技术上讲,它还将包括第二天(00:00:00.000 小时)的第一个瞬间,但根据您的应用程序,这可能就足够了.

The between operator is inclusive, but if your database field is actually a timestamp, then a date with no time is assumed to be at time 00:00:00.000. So, in such a case, for your dates to be inclusive, you can add one day to your end date. Technically it will also include the first instant of the next day (00:00:00.000 hour), but depending on your application it may be enough.

否则你可以在开始日期"上使用 >= ,在结束日期加一天"上使用 < :

Otherwise you could use >= on "start date" and < on "end date plus one day":

"sales.Date >= '" + sdate + "' AND sales.Date < '" + edatePlusOne + "' "

这篇关于使用 JDBC 在 SQL 中的日期之间搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆