SQL语句语法错误
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在该上下文中使用
VALUES
并且 WHERE 子句中存在杂散逗号;你想要这个:你也可以使用
BETWEEN
:但这只是一个风格问题。
我不确定 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:You could also use
BETWEEN
: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
instmtGetRecordByDate
because of your invalid SQL.