我已檢索從數據庫logTime Description該條的值和計算出的一小時前的時間從所檢索的logtime.the代碼用於檢索logTime Description該條數據庫查詢的Java中的參數是如何從一個方法傳遞的變量值到另一個作爲
public String database_Time() {
try {
con = getConnection();
String sql = "select max(logtime) from vacuum_analog_1mins";
clstmt = con.prepareCall(sql);
clstmt.execute();
rs = clstmt.getResultSet();
while (rs.next()) {
timestr = rs.getString(1);
}
} catch (Exception e) {
System.out.println("\nException in Bean in getDbTable(String code):" + e);
} finally {
closeConnection();
}
return timestr;
}
代碼來計算一小時前時間:
public Date previostime() throws ParseException
{
database_Time();
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date = simpleDateFormat.parse(timestr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
hours--;
calendar.set(Calendar.HOUR_OF_DAY, hours);
Date fixedDate = calendar.getTime();
System.out.println("previous date is" + (fixedDate));
System.out.println("current date is" + timestr);
return fixedDate;
}
現在我想用這兩個變量fixedDate和TIMESTR在我的另一種方法,其中這些變量將被用作sqlquery的參數爲:
List <String> timeStr = new ArrayList <String>();
database_Time();
String atime[] = null;
database_Time();
previostime();
getConnection();
try {
con = getConnection();
String sql = "exec vcs_gauge @gauge_name=?,@first_rec_time=?,@last_rec_time=?";
clstmt = con.prepareCall(sql);
clstmt.setString(1, "vs1_bag");
clstmt.setString(2, "fixedDate");
clstmt.setString(3, "timestr");
clstmt.execute();
rs = clstmt.getResultSet();
while (rs.next()) {
// Just get the value of the column, and add it to the list
timeStr.add(rs.getString(1).substring(11, 16));
}
但沒有結果請幫助我在哪裏出錯。我已將這些變量聲明爲全球。
fixedDate和TIMESTR被設置爲變量的類? – Mailkov 2014-11-24 12:14:56
@Mailkov它們被聲明爲變量public Date fixedDate; \t public String timestr; – 2014-11-24 12:16:58
因爲我看到你在你的方法中聲明瞭fixedDate previoustime() – Mailkov 2014-11-24 12:18:28