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 に答える