0

我有一个方法,它通过日期对象返回 ResultSet 查询的值。问题是,在我的输出中,它只返回特定列中的最后一个值。我该怎么做?

 public Date getTime(){
      Date date = null;
      DateFormat format;

      try {

              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection(url, "root", "");

              Statement stmt = con.createStatement();

              ResultSet result = stmt.executeQuery("SELECT * FROM error_log WHERE service_source = 'Billbox' ");

              while (result.next()) {  //retrieve data
                  String ds = result.getString("error_date");
                  format = new SimpleDateFormat("M/d/yyyy H:m:s a");

                  date = (Date)format.parse(ds);  

              }
              con.close();

          } catch (Exception ex) {
              Logger.getLogger(LogDB.class.getName()).log( 
                            Level.SEVERE, null, ex);
          }
      return date;
  }

然后在我的主要方法中:

  public static void main(String args[]){        
      TestA ea = new TestA();
      Date ss = ea.getTime();
      System.out.println(ss);
  } 

但这只会返回我查询中的最后一个值。我怎样才能打印出其他(老年人)连同它?

4

3 回答 3

2

您需要从查询结果中填写日期列表。尝试这个 :

  public List<Date> getTime(){
      List<Date> dates = new ArrayList<Date>();
      DateFormat format;

      try {

          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection(url, "root", "");

          Statement stmt = con.createStatement();

          ResultSet result = stmt.executeQuery("SELECT * FROM error_log WHERE service_source = 'Billbox' ");

          while (result.next()) {  //retrieve data
              String ds = result.getString("error_date");
              format = new SimpleDateFormat("M/d/yyyy H:m:s a");

              dates.add((Date)format.parse(ds));  

          }
          con.close();

      } catch (Exception ex) {
          Logger.getLogger(LogDB.class.getName()).log( 
                        Level.SEVERE, null, ex);
      }
      return dates;
  }
于 2013-07-11T14:26:45.380 回答
1

由于 while 循环,您指定了最后一个值的日期。将您的方法更改为

public List<Date> getTime(){

你可以试试

List<Date> dates = new ArrayList<Date>();

然后在你的while循环中;

while (result.next()) {  //retrieve data
                  String ds = result.getString("error_date");
                  format = new SimpleDateFormat("M/d/yyyy H:m:s a");

                  date = (Date)format.parse(ds);  
                  dates.put(date);


              }

return dates;
于 2013-07-11T14:26:46.003 回答
0

只是:

        List<Date> lDateDb = new ArrayList<Date>();
        while (result.next()) {  //retrieve data
              String ds = result.getString("error_date");
              format = new SimpleDateFormat("M/d/yyyy H:m:s a");

              date = (Date)format.parse(ds);  
              lDateDb.add(date);
          }
        return lDateDb;

将返回类型更改getTimeList<Date>并将主要更改为:

public static void main(String args[]){        
  TestA ea = new TestA();
  List<Date> lAllDate = ea.getTime();
  for (Date lDate : lAllDate)
  System.out.println(lDate );
} 
于 2013-07-11T14:26:37.430 回答