2014-11-24 26 views
0

我已檢索從數據庫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)); 
 

 
    }

但沒有結果請幫助我在哪裏出錯。我已將這些變量聲明爲全球。

+0

fixedDate和TIMESTR被設置爲變量的類? – Mailkov 2014-11-24 12:14:56

+0

@Mailkov它們被聲明爲變量public Date fixedDate; \t public String timestr; – 2014-11-24 12:16:58

+0

因爲我看到你在你的方法中聲明瞭fixedDate previoustime() – Mailkov 2014-11-24 12:18:28

回答

0

這兩個行是不正確的

clstmt.setString(2, "fixedDate"); 
clstmt.setString(3, "timestr"); 

如果在SQL他們是Date類型試試這個:

clstmt.setDate(2, java.sql.Date(fixedDate.getTime())); 
clstmt.setDate(3, java.sql.Date.valueOf(timestr)); 
+0

沒有變化,沒有什麼顯示。 – 2014-11-24 12:26:33

+0

但您更改Date fixedDate = calendar.getTime(); in fixedDate = calendar.getTime();在你以前的()方法? – Mailkov 2014-11-24 12:27:36

+0

我已經這樣做了,看看我的代碼 – 2014-11-24 12:29:01

相關問題