日期:2014-05-20  浏览次数:20698 次

list集合问题,希望有人能解决
javase写桌面执行程序:前台界面有3个文本输入窗口让客户输入,分别是1.日期,2.天数,3.周期.当用户输入相关的信息后,后台根据(1.日期)减去(2.天数)得到一个(新的日期),然后根据这两个日期放入sql语句中进行查询统计。(3.周期)是循环次数,第2次循环是在上一次(新的日期)减1天后再开始下一次循环。循环结束后把所有循环查出的结果在JTable里打出来。希望有人能解决

------解决方案--------------------
for example
Java code
String date = date_textfield.getText();
String days = days_textfield.getText();
String period = period_textfield.getText();
SimplieDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date toDate = sdf.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(toDate);
calendar.add(Calendar.DATE, -1*Integer.valueOf(days));
Date fromDate = calendar.getTime();
List<Object[]> view = new ArrayList<Object[]>();
for (int i=0; i<Integer.valueOf(period); i++) {
    String sql = "select * from your_table where field between ? and ?";
    PreparedStatement ps = your_connection.prepareStatement(sql);
    ps.setDate(1, fromDate);
    ps.setDate(2, toDate);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        List<Object> row = new ArrayList<Object>();
        for (int i=0; i<rs.getMetaData().getColumnCount()) {
            row.add(rs.getObject(i));
        }
        view.add(row);
    }
    calendar.add(Calendar.DATE, -1);
    fromDate = calendar.getTime();
}
for (int i=0; i<view.size(); i++) {
    for (int j=0; j<view.get(i).length; j++) {
        your_jtable.setValueAt(view.get(i)[j], i, j);
    }
}