2014-11-24 31 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

相关问题