gpt4 book ai didi

java - 结果集不迭代指定列的值

转载 作者:行者123 更新时间:2023-12-01 23:43:16 24 4
gpt4 key购买 nike

我有一个方法,它通过日期对象返回 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);
}

但这仅返回查询中的最后一个值。我怎样才能打印出其他(较旧的)连同它?

最佳答案

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

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

关于java - 结果集不迭代指定列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17595984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com