简体   繁体   English

SQL Java选择本月添加的所有记录

[英]SQL java Select all the records that added in this month

I need to display to the table all the records who has is created_at this month. 我需要在表中显示本月创建的所有记录。 the format is mmm dd yyyy, Using java and sqlite 格式为mmm dd yyyy,使用java和sqlite

I have the column "Start" in my database, if the month of the date is this month, it has to appear in the table. 我的数据库中有“开始”列,如果日期的月份是本月,则它必须出现在表中。

I Tried this code below,, pls guide me. 我在下面尝试了此代码,请指导我。

i need to select all the records, within the month. 我需要在一个月内选择所有记录。

  try {
   SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
   Calendar c = Calendar.getInstance();
   c.set(Calendar.DAY_OF_MONTH, 1);
   c = Calendar.getInstance(); // reset
   String today = dateFormat.format(c.getTime());

   int month = c.get(Calendar.MONTH) + 1;

   String sql = "SELECT Firstname FROM Members_Tbl WHERE End = '" + month 
   + "'";
   pst = con.prepareStatement(sql);
   rs = pst.executeQuery();
   monthlyreports.setModel(DbUtils.resultSetToTableModel(rs));

   rs.close();
   } catch (Exception e) {
   JOptionPane.showMessageDialog(null, e);
    }

java.time java.time

I have not tested (haven't got SQLite installed), but I believe the following should work: 我尚未测试(尚未安装SQLite),但我相信以下应该可以工作:

    DateTimeFormatter monthPatternFormatter = DateTimeFormatter.ofPattern("MMM '%' uuuu", Locale.ENGLISH);
    YearMonth currentMonth = YearMonth.now(ZoneId.of("Australia/Melbourne"));
    String sql = "SELECT Firstname FROM Members_Tbl WHERE Start like ?;";
    pst = con.prepareStatement(sql);
    String monthPattern = currentMonth.format(monthPatternFormatter);
    pst.setString(1, monthPattern);
    rs = pst.executeQuery();

I am taking your word for it when you say that the format in your database is mmm dd yyyy, for example Apr 11 2019 . 当您说数据库中的格式为mmm dd yyyy时,我相信您的意思,例如Apr 11 2019 I am assuming English month abbreviations. 我假设英语月份为缩写。

I would recommend storing ISO 8601 standard date strings in your database, though. 不过,我建议将ISO 8601标准日期字符串存储在数据库中。 It goes like 2019-04-11 . 它像2019-04-11 Then your format pattern string would be uuuu-MM-'%' . 那么您的格式模式字符串将是uuuu-MM-'%' Or you might compare strings using <= and < , which would probably be more efficient. 或者,您可以使用<=<比较字符串,这可能会更有效。

The date/time classes that you were using, SimpleDateFormat and Calendar , are poorly designed and fortunately long outdated. 您使用的日期/时间类SimpleDateFormatCalendar设计很差,很幸运,它已经过时了。 Instead I am using java.time, the modern Java date and time API. 相反,我使用的是java.time,这是现代的Java日期和时间API。 It is so much nicer to work with. 与之合作真是太好了。

Links 链接

You could use (see below) the SQLite substr core function :- 您可以使用(参见下文) SQLite substr核心功能 :-

String sql = "SELECT Firstname FROM Members_Tbl WHERE substr(Start,1,2) = '" + month + "'";

BUT that would include all years. 但是将包括所有年份。

So you may want :- 所以你可能想要:-

String sql = "SELECT Firstname FROM Members_Tbl WHERE substr(Start,1,2) = '" + month + "' AND substr(Start,7,4) = '" + the_respective_year + "'";

Notes 笔记

If MM or DD are not always 2 characters (eg 1/9/2019) then the above would not work properly. 如果MM或DD并非始终为2个字符(例如1/9/2019),则上述内容将无法正常工作。

The recommended way is to store dates in YYYY-MM-DD format (or any of the time string formats from the link). 推荐的方法是以YYYY-MM-DD格式(或链接中的任何时间字符串格式)存储日期。 You can then use the SQLite DateTime functions. 然后,您可以使用SQLite DateTime函数。

Your SimpleDateFormat object must match the format stored in the table if you want to compare the months. 如果要比较月份,则SimpleDateFormat对象必须与表中存储的格式匹配。
You must compare also the year not just the month. 您还必须比较年份而不只是月份。
It is always safer to use placeholders inside the sql statement instead of concatenating the parameters: 在sql语句中使用占位符而不是串联参数总是更安全:

SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 1);
c = Calendar.getInstance(); // reset
String today = dateFormat.format(c.getTime());
String sql =
        "SELECT Firstname FROM Members_Tbl WHERE " +
        "substr(Start, 1, 3) = ? AND substr(Start, 8, 4) = ?";

pst = con.prepareStatement(sql);
pst.setString(1, today.substring(0, 3));
pst.setString(2, today.substring(7));
rs = pst.executeQuery();
monthlyreports.setModel(DbUtils.resultSetToTableModel(rs));

rs.close();

Edit 编辑
Since you changed the format of the dates to YYYY-MM-DD , 由于您将日期格式更改为YYYY-MM-DD
you can use this query: 您可以使用以下查询:

String sql = "select * from Members_Tbl WHERE strftime('%Y-%m', Start) = strftime('%Y-%m', 'now')";

String sql = "select * from Members_Tbl WHERE strftime('%m', Start) == strftime('%m', 'now')"; 字符串sql =“从Member_Tbl中选择*,在strftime('%m',Start)== strftime('%m','now')”;

This is the query I used, and after several searches the date should be YYYY-MM-DD. 这是我使用的查询,经过几次搜索后,日期应为YYYY-MM-DD。 thank you everyone for your cooperation. 谢谢大家的合作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM