在jdbc中显示数据库中两个日期之间的数据

时间:2016-02-26 06:56:19

标签: java mysql jsp jdbc

在jdbc

中的数据库的两个日期之间显示数据

1。 DAO

 public int getdata(Date startDate,Date endDate) {
    PreparedStatement ps;
    int id = 0;

    try {

        ps = connection.prepareStatement("SELECT * FROM project.order WHERE
                        PO_Date >="+startDate+" AND PO_Date <= "+endDate+"");
        System.out.println("value of ps "+ps);

        ResultSet rs = ps.executeQuery();
        System.out.println("value of rs "+rs);
        while (rs.next()) {
            System.out.println("value of while loop "+rs.next());
            ArrayList<String> arrlist = new ArrayList<String>();

            System.out.println(rs.getInt(1));
            System.out.println(rs.getDate(2));
            System.out.println(rs.getString(4));
            System.out.println(rs.getString(5));
            System.out.println(rs.getString(6));

            System.out.println("******************");

            Iterator<String> itr = arrlist.iterator();
            while (itr.hasNext()) {
                System.out.println(itr.next());

            }
        rs.close();
    } 
        }catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return id;
  }
}

2。控制器

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    RequestDispatcher requestDispatcher = null;

    String startDate=request.getParameter("StartDate");
    String endDate=request.getParameter("EndDate");

    Date utilDate1 = null;
    Date utilDate2 = null;
    try {
        utilDate1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
        utilDate2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
    } catch (ParseException e) {
    }
    java.sql.Date sqlDate1 = new java.sql.Date(utilDate1.getTime());
    java.sql.Date sqlDate2 = new java.sql.Date(utilDate2.getTime());

    CountDTO countDTO = new CountDTO();
    countDTO.setStartDate(sqlDate1);
    countDTO.setEndDate(sqlDate2);

    CountService countService = new CountService();
    int ProductOrder = countService.getdata(countDTO);

    if (ProductOrder != 0) {
        requestDispatcher = request.getRequestDispatcher("view/sucess.jsp");
        requestDispatcher.forward(request, response);

    }
  }
}

第3。服务

 public int getdata(CountDTO countDTO) {
    int i = 0;
    Date startDate = countDTO.getStartDate();
    Date endDate = countDTO.getEndDate();

    try {
        java.sql.Date sqlStartDate = new java.sql.Date(startDate.getTime());
        java.sql.Date sqlEndDate = new java.sql.Date(endDate.getTime());
        CountDAO countDAO=new CountDAO();
        i = countDAO.getdata(sqlStartDate, sqlEndDate);
    } catch (Exception e) {
    }
    return i;
  }
}

我正在尝试上面的代码来显示两个日期StartDate和EndDate之间的数据。但我没有得到输出。

控制台

value of ps com.mysql.jdbc.JDBC4PreparedStatement@15daccd: SELECT * FROM
  project.order WHERE PO_Date >=2016-01-02 AND PO_Date <= 2016-04-25
value of rs com.mysql.jdbc.JDBC4ResultSet@7031d7

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个

false