7

我目前正在编写一个 Java Swing 应用程序,它从 MYOB 数据库文件中读取数据并在表中显示某些信息。我已经能够成功生成所需的 SQL 语句,但在添加日期之间的搜索功能时遇到了麻烦(我们的数据库非常大,所以我们试图限制结果)。下面是我的一个查询示例(用 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)。

我尝试在with语句中使用另一个AND子句,但我从 JDBC 收到以下错误WHEREBETWEEN[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"
                        + ";");

有人能帮我找出正确的语法吗?这是缺少的最后一个功能。

sdate编辑:和的当前值edate分别为 25/10/2013,因为它们默认为今天的日期。我已经设置了我正在测试的虚拟文件,以确保它将提供具有此日期范围的数据。另外为了澄清,我希望搜索日期包含在内。

4

3 回答 3

11

在日期周围使用引号:

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"
            + ";");

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

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 小时),但根据您的应用程序,这可能就足够了。

否则,您可以>=在“开始日期”和<“结束日期加一天”上使用:

"sales.Date >= '" + sdate + "' AND sales.Date < '" + edatePlusOne + "' "
于 2013-10-25T07:48:05.587 回答
0

看起来您需要以这种格式表示日期:

select * from table where datefield between '2006-10-01' 和 '2006-11-30'

也看这里,别忘了谷歌是你的朋友!:)

于 2013-10-25T07:52:11.480 回答
-1
Class.forName("com.mysql.jdbc.Driver");

connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospitall","root","");

PreparedStatement ps=connection.prepareStatement("select * from patient where patientid='"+txtpatientid.getText()+"'");

Statement stm=connection.createStatement();

ResultSet rs=ps.executeQuery();

if(rs.next()){

String patientid=txtpatientid.getText(); 

String str="select * from patient where patientid='"+patientid+"'";

ResultSet result=stm.executeQuery(str);
于 2015-06-24T06:27:21.627 回答