为什么我的 SQL 查询失败?

发布于 2024-10-27 08:45:18 字数 583 浏览 4 评论 0原文

我有这个表:

sportman
{
    code int primary key,
    date Date.
}

包含

code 10,          30,         50.
date 1990-02-15,  1999-02-15, 2010-02-15.

我在 NetBeans 中编写此查询的值:

 resultSet = statement.executeQuery("select code from sportman "
                                    + "where date =  1990-02-15";

但结果集为空。问题是什么?我该如何解决?

 while(resultSet.next())
                      {
                             x = resultSet.getString("code");
                      }

I have this table:

sportman
{
    code int primary key,
    date Date.
}

containing values

code 10,          30,         50.
date 1990-02-15,  1999-02-15, 2010-02-15.

I wrote this query in NetBeans:

 resultSet = statement.executeQuery("select code from sportman "
                                    + "where date =  1990-02-15";

but the resultset is empty. What is the problem and how can I solve it?

 while(resultSet.next())
                      {
                             x = resultSet.getString("code");
                      }

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

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

发布评论

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

评论(4

终陌 2024-11-03 08:45:18

1990-02-151973,而不是 1990 年 2 月 15 日。

在这些情况下,一种有用的调试方法是打印之前的整个 SQL 语句执行它。

一个好的方法是使用 PreparedStatement< /代码>

PreparedStatement stmt = con.prepareStatement("select code from sportman where start = ?");
stmt.setDate(1, java.sql.Date.valueOf("1990-02-15");
resultSet = stmt.executeQuery();

1990-02-15 is 1973 and not February 15th 1990.

A useful debugging approach in those situations is to print the entire SQL statement prior to executing it.

A good approach would be to use a PreparedStatement:

PreparedStatement stmt = con.prepareStatement("select code from sportman where start = ?");
stmt.setDate(1, java.sql.Date.valueOf("1990-02-15");
resultSet = stmt.executeQuery();
心欲静而疯不止 2024-11-03 08:45:18

您确定这是您的实际代码吗?您应该会因 executeQuery() 调用缺少括号而收到编译错误。此外,1990-02-15 不是 String。该错误是您所说的“但结果集为空”的意思吗?

Are you sure that's your actual code? You should be getting a compilation error from the missing parenthesis to your executeQuery() call. Also, 1990-02-15 is not a String. Is that error what you meant by "but resultset is empty"?

蓝天 2024-11-03 08:45:18

您没有告诉我们您正在使用哪个 DBMS,但如果您使用的是 Oracle,您可能需要截掉属于 Oracle DATE 列的时间部分:

PreparedStatement stmt = con.prepareStatement("select code from sportman where trunc(start) = ?");
stmt.setDate(1, java.sql.Date.valueOf("1990-02-15");
resultSet = stmt.executeQuery();

You are not telling us which DBMS you are using, but if you are using Oracle, that you probably need to cut off the time part that is part of an Oracle DATE column:

PreparedStatement stmt = con.prepareStatement("select code from sportman where trunc(start) = ?");
stmt.setDate(1, java.sql.Date.valueOf("1990-02-15");
resultSet = stmt.executeQuery();
萌逼全场 2024-11-03 08:45:18
resultSet = statement.executeQuery("select code from sportman "
                                + " where date ='1990-02-15'");

这有效。

resultSet = statement.executeQuery("select code from sportman "
                                + " where date ='1990-02-15'");

This works.

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