SQL语句语法错误

发布于 2025-01-05 10:15:15 字数 1315 浏览 1 评论 0原文

使用 SQL 语句检索两个时间戳之间的记录时遇到问题。这只是给了我一个 NullPointerException,我认为这只是一个语法错误,我一直在四处搜索,但找不到任何东西。这是语句:

private static final String strGetRecordByDate =
    "SELECT * FROM APP.MYTABLE " +
    "WHERE MYDATE >= ?, AND MYDATE <= ? " +
    "VALUES (?, ?)";

创建语句:

stmtGetRecordByDate = dbConnection.prepareStatement(strGetRecordByDate);

stmtGetRecordByDate.clearParameters() 上显示空指针异常:

public Vector getRecordByDate(Date datefrom, Date dateto){
    Vector<String> records = new Vector<String>();
    ResultSet results = null;
    java.sql.Date sqlDatefrom = new java.sql.Date(datefrom.getTime());
    java.sql.Date sqlDateto = new java.sql.Date(dateto.getTime());
    try {           
        stmtGetRecordByDate.clearParameters();
        stmtGetRecordByDate.setDate(1, sqlDatefrom);
        stmtGetRecordByDate.setDate(2, sqlDateto);
        results = stmtGetRecordByDate.executeQuery();
        while (results.next()) {
            int id = results.getInt(1);
            String entry = results.getString(2);
            records.add(entry);
        }
    } catch(SQLException sqle) {
        sqle.printStackTrace();
    }
    return records;
}

其余的查询和语句工作正常,所以我知道数据库本身很好,只是这个有问题。

任何建议都会很棒。 谢谢!

Having trouble with an sql statement to retrieve a record between two timestamps. This is just giving me a NullPointerException and I think it's just a syntax error, II've been searching around and I couldn't find anything. This is the statement:

private static final String strGetRecordByDate =
    "SELECT * FROM APP.MYTABLE " +
    "WHERE MYDATE >= ?, AND MYDATE <= ? " +
    "VALUES (?, ?)";

creating the statement:

stmtGetRecordByDate = dbConnection.prepareStatement(strGetRecordByDate);

The nullpointer exception shows on stmtGetRecordByDate.clearParameters():

public Vector getRecordByDate(Date datefrom, Date dateto){
    Vector<String> records = new Vector<String>();
    ResultSet results = null;
    java.sql.Date sqlDatefrom = new java.sql.Date(datefrom.getTime());
    java.sql.Date sqlDateto = new java.sql.Date(dateto.getTime());
    try {           
        stmtGetRecordByDate.clearParameters();
        stmtGetRecordByDate.setDate(1, sqlDatefrom);
        stmtGetRecordByDate.setDate(2, sqlDateto);
        results = stmtGetRecordByDate.executeQuery();
        while (results.next()) {
            int id = results.getInt(1);
            String entry = results.getString(2);
            records.add(entry);
        }
    } catch(SQLException sqle) {
        sqle.printStackTrace();
    }
    return records;
}

The Rest of the queries and statements work fine so I know that the database itself is fine there is just something wrong with this one.

Any suggestions would be great.
Thanks!

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

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

发布评论

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

评论(1

我不咬妳我踢妳 2025-01-12 10:15:15

您不能在该上下文中使用 VALUES 并且 WHERE 子句中存在杂散逗号;你想要这个:

private static final String strGetRecordByDate =
        "SELECT * FROM APP.MYTABLE " +
        "WHERE MYDATE >= ? AND MYDATE <= ?";

你也可以使用BETWEEN

private static final String strGetRecordByDate =
        "SELECT * FROM APP.MYTABLE " +
        "WHERE MYDATE BETWEEN ? AND ?";

但这只是一个风格问题。

我不确定 Java 方面的情况,但由于 SQL 无效,您可能会在 stmtGetRecordByDate 中得到 null

You can't use a VALUES in that context and you have a stray comma in your WHERE clause; you want this:

private static final String strGetRecordByDate =
        "SELECT * FROM APP.MYTABLE " +
        "WHERE MYDATE >= ? AND MYDATE <= ?";

You could also use BETWEEN:

private static final String strGetRecordByDate =
        "SELECT * FROM APP.MYTABLE " +
        "WHERE MYDATE BETWEEN ? AND ?";

but that's just a matter of style.

I'm not certain about the Java side of things but you're probably getting a null in stmtGetRecordByDate because of your invalid SQL.

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