如何正确使用setDate?
我有一个包含“日期”列的表格,并以格式键入日期:“2011-09-06” 我想执行查询:
select * from tvprograms where date=?
我尝试这样做:
Date startDate;
java.util.Date date;
Calendar cal = Calendar.getInstance();
cal.set(2011, 9, 1);
startDate = cal.getTime();
TVProgramDAO tvDAO = new TVProgramDAO();
tvDAO.findUnusedTvPrograms(new java.sql.Date(date.getTime()));
findUnusedTvPrograms 看起来:
public List<Integer> findUnusedTvPrograms(Date date) {
List<Integer> results = new ArrayList<Integer>();
if (obtainConnection()) {
PreparedStatement stmt = null;
ResultSet rs = null;
...
stmt = con.prepareStatement(findOldTvProgQuery.toString());
stmt.setDate(1, date);
rs = stmt.executeQuery();
while (rs.next()) {
results.add(rs.getInt(1));
...
return results;
}
但它不起作用。
I have table with column "date" and type Date in format: "2011-09-06"
and i want to execute query:
select * from tvprograms where date=?
And I try this:
Date startDate;
java.util.Date date;
Calendar cal = Calendar.getInstance();
cal.set(2011, 9, 1);
startDate = cal.getTime();
TVProgramDAO tvDAO = new TVProgramDAO();
tvDAO.findUnusedTvPrograms(new java.sql.Date(date.getTime()));
findUnusedTvPrograms looks:
public List<Integer> findUnusedTvPrograms(Date date) {
List<Integer> results = new ArrayList<Integer>();
if (obtainConnection()) {
PreparedStatement stmt = null;
ResultSet rs = null;
...
stmt = con.prepareStatement(findOldTvProgQuery.toString());
stmt.setDate(1, date);
rs = stmt.executeQuery();
while (rs.next()) {
results.add(rs.getInt(1));
...
return results;
}
But it doesnt work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,这段代码不会编译开始:
您正在尝试使用
date
但您从未初始化过该变量。这是一个稍微干净的版本:请注意,这将使用系统的默认时区,这可能不是一个好主意。就我个人而言,我建议使用 Joda Time 代替......
This code won't compile to start with as far as I can see:
You're trying to use
date
but you've never initialized the variable. Here's a slightly cleaner version:Note that that will use the default time zone of the system, which may not be a good idea. Personally I would recommend using Joda Time instead...