简体   繁体   English

如何在Java中将变量值从一种方法传递到另一种方法作为数据库查询的参数

[英]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.

相关问题 如何将字符串变量 java 从一种方法传递到另一种方法 - How pass string variable java from one method to another method 我如何通过 ArrayList<string> 在 Java 中从一种方法到另一种方法。 变量未初始化错误</string> - How do I pass an ArrayList<String> from one method to another in Java. Variable not initialised error 如何在Java中将字符串从一个方法传递到另一个方法 - How to pass a string from one method to another method in Java 如何在 Java 中将参数从一个传递到另一个 class? - How to pass parameter from one to another class in Java? 如何在Java中将参数从一个类传递到另一个类? - How to pass in the parameter from one class to another in Java? 如何将值从一种方法传递到另一种方法 - How to pass the value from one method to another method 如何将变量从一个参数化方法传递到另一个? - How to pass variable from one parameterized method to another? android将变量从一种方法传递到另一种方法 - android pass variable from one method to another 如何在Java中将变量从一类传递到另一类? - How Do You Pass a Variable from One Class to Another in Java? 如何将变量值从一个JFrame传递到Netbeans中的另一个JFrame - How to pass a variable value from one JFrame to Another JFrame in Netbeans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM