如何根据今天的日期从表中取回数据10天

时间:2019-06-03 13:12:58

标签: java mysql sql date days

我有表格在哪里搜索框。我想在此搜索框中输入今天的日期并单击“提交”按钮后,我从数据库中获得10天的记录。我在表中有以下几列(表名= issuedevices)。 Device_ID,Employee_id,Employee_Name,Employee_Ext,Issue_Date

当我使用以下查询并在issue_date列中输入日期时,获取该日期的记录。  pst = con.prepareStatement(“从issueDevices中选择*,其中Issue_Date ='” + str +“');

当我编写查询时,就像什么也没发生 pst = con.prepareStatement(“从Issuedevices中选择*,其中Issue_Date ='” + str +“'-INTERVAL'10'DAY”);

try 
          {
                   String str = tf5.getText();
                   Connection con=DB.getConnection();
                   PreparedStatement st = con.prepareStatement("select * from issuedevices where Issue_Date=?");
                 //  PreparedStatement st = con.prepareStatement("SELECT * FROM issuedevices WHERE Issue_Date > sysdate - INTERVAL '10' DAY;");

                   st.setString(1, str);
                   ResultSet rs=st.executeQuery();

             //       Vector v = new Vector();
               if (rs.next()) 
                    {
                         frame1 = new JFrame("Database Search Result");
                         frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                         frame1.setLayout(new BorderLayout());
                        //TableModel tm = new TableModel();
                         DefaultTableModel model = new DefaultTableModel();
                         model.setColumnIdentifiers(columnNames);
                         table = new JTable();
                         table.setModel(model);
                         table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                         table.setFillsViewportHeight(true);
                           JScrollPane scroll = new JScrollPane(table);
                           scroll.setHorizontalScrollBarPolicy(
                                   JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                           scroll.setVerticalScrollBarPolicy(
                                   JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                 //  from = (String) c1.getSelectedItem();
                             String id = "";
                             String Device_ID = "";
                             String Employee_id = "";
                             String Employee_Name = "";
                             String Employee_Ext = "";
                             String Issue_Date= "";

                try {
                      //  pst = con.prepareStatement("select * from issuedevices where Issue_Date='" + str + "'");
                       pst = con.prepareStatement("select * from issuedevices where Issue_Date BETWEEN '" + str + "' - INTERVAL '11' DAY  AND '" + str + "'");
                       // pst = con.prepareStatement("select * from issuedevices where Issue_Date = '" + str + "'- INTERVAL '10' DAY");

                        ResultSet rs1 = pst.executeQuery();

                        int i = 0;
                        if (rs1.next())
                        {
                            id = rs1.getString("id");
                            Device_ID = rs1.getString("Device_ID");
                            Employee_id = rs1.getString("Employee_id");
                            Employee_Name = rs1.getString("Employee_Name");
                            Employee_Ext=rs1.getString("Employee_Ext");
                            Issue_Date=rs1.getString("Issue_Date");

                            model.addRow(new Object[]{id, Device_ID, Employee_id,Employee_Name,Employee_Ext,Issue_Date});
                            i++;
                        }
                        if (i < 1) 
                        {
                            JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        if (i == 1) 
                        {
                            System.out.println(i + " Record Found");
                        }
                        else 
                        {
                            System.out.println(i + " Records Found");
                        }
                } 
                        catch (Exception ex)
                        {
                            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        frame1.add(scroll);
                        frame1.setVisible(true);
                        frame1.setSize(400, 300);
                    }

                    st.close();
                    rs.close();
          } 
          catch (Exception e) 
          {
              JOptionPane.showMessageDialog(null, "Name not Found");
          }

2 个答案:

答案 0 :(得分:2)

java.time和JDBC 4.2

    pst = con.prepareStatement("select * from issuedevices where Issue_Date BETWEEN ? AND ?;");
    LocalDate today = LocalDate.now(ZoneId.of("Europe/Isle_of_Man"));
    pst.setObject(1, today.minusDays(10));
    pst.setObject(2, today);

不要手工构建查询字符串。使用参数。并且不要将日期参数作为字符串传递,而应作为LocalDate对象传递。从代码中可以看到,LocalDate有一种减去10天(或任何其他天数)的方法。

链接: Oracle tutorial: Date Time解释了如何使用java.time。

答案 1 :(得分:0)

您将查询参数绑定为字符串:

st.setString(1, str);

考虑将其绑定为日期:

st.setDate(1, new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").parse(str));

当然要使用您想要的date format

您还可以在数据库中进行转换,而完整保留Java代码,但修改查询:

select * from issuedevices where Issue_Date=STR_TO_DATE(?, "%M %d %Y");

另外考虑将日期与范围进行比较(不要使用“ =”,请使用“>”)