2013-07-11 54 views
0

我有一個方法,它通過日期對象返回ResultSet查詢的值。這個障礙是,在我的輸出中,它只返回特定列中的最後一個值。我如何去做這件事?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); 
    } 

但這僅返回我的查詢中的最後一個值。我怎麼能打印出其他(老年人)一起呢?

+1

你好,如果上述任何一個答案解決了你的問題,請接受一個! – Simon

+0

是選擇一個,然後點擊左上角的鉤子。 –

回答

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; 
    } 
+0

請參閱我對[這個答案]的評論(http://stackoverflow.com/a/17596064/1065197) –

+0

編輯完成後,你應該返回一個'List '而不是'ArrayList '否則代碼不會編譯。 –

+0

是的,我很快寫了這個答案,但你是對的! – Simon

0

剛:中getTimeList<Date>

 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; 

變化返回類型,改變主要以:

public static void main(String args[]){   
    TestA ea = new TestA(); 
    List<Date> lAllDate = ea.getTime(); 
    for (Date lDate : lAllDate) 
    System.out.println(lDate); 
} 
+0

感謝您的輸入。它適用於所有答案。謝謝 –

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; 
+0

使用'List dates = new ArrayList ();'會更好。參考[「編程接口」是什麼意思?](http://stackoverflow.com/q/383947/1065197) –

+0

你是對的,我編輯我的答案。 –