如何正确使用setDate?

发布于 2024-12-04 11:48:53 字数 961 浏览 1 评论 0原文

我有一个包含“日期”列的表格,并以格式键入日期:“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 技术交流群。

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

发布评论

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

评论(1

萧瑟寒风 2024-12-11 11:48:53

据我所知,这段代码不会编译开始:

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()));

您正在尝试使用 date 但您从未初始化过该变量。这是一个稍微干净的版本:

Calendar cal = Calendar.getInstance();
cal.set(2011, 9, 1);
Date startDate = cal.getTime();
TVProgramDAO tvDAO = new TVProgramDAO();
tvDAO.findUnusedTvPrograms(new java.sql.Date(startDate.getTime()));

请注意,这将使用系统的默认时区,这可能不是一个好主意。就我个人而言,我建议使用 Joda Time 代替......

This code won't compile to start with as far as I can see:

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()));

You're trying to use date but you've never initialized the variable. Here's a slightly cleaner version:

Calendar cal = Calendar.getInstance();
cal.set(2011, 9, 1);
Date startDate = cal.getTime();
TVProgramDAO tvDAO = new TVProgramDAO();
tvDAO.findUnusedTvPrograms(new java.sql.Date(startDate.getTime()));

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...

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