简体   繁体   中英

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

 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:

 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM