为什么我的 SQL 查询失败?
我有这个表:
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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1990-02-15
是1973
,而不是 1990 年 2 月 15 日。在这些情况下,一种有用的调试方法是打印之前的整个 SQL 语句执行它。
一个好的方法是使用
PreparedStatement< /代码>
:
1990-02-15
is1973
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
:您确定这是您的实际代码吗?您应该会因
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 aString
. Is that error what you meant by "but resultset is empty"?您没有告诉我们您正在使用哪个 DBMS,但如果您使用的是 Oracle,您可能需要截掉属于 Oracle
DATE
列的时间部分: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:这有效。
This works.