[英]how to pass variable value from one method onto another as a parameter of database query in java
I have retrieved the value of logtime from database and calculated one hour ago time from the retrieved logtime.the code for retrieving logtime is 我已经从数据库中检索了logtime的值,并从检索到的logtime中计算了一个小时前的时间。
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; }
Code to calculate one hour ago time is: 计算一个小时前时间的代码是:
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; }
Now I want to use these two variables fixedDate and timestr in my another method where these variables will be used as parameters of a sqlquery as: 现在,我想在另一个方法中使用这两个变量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)); }
But no result.Please help me where I'm going wrong.I have declared these variables as global also. 但是没有结果,请帮我解决问题,我也将这些变量声明为全局变量。
This two row aren't correct 这两行不正确
clstmt.setString(2, "fixedDate");
clstmt.setString(3, "timestr");
if in sql they are Date type try this: 如果在sql中它们是Date类型,请尝试以下操作:
clstmt.setDate(2, java.sql.Date(fixedDate.getTime()));
clstmt.setDate(3, java.sql.Date.valueOf(timestr));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.