sql server - Conversion failed when converting date or time from character string in java -


i have created 2 methods in java ,one of returns database server time , other returns 1 hour ago time of database server.now have use these 2 date time sql query.the code retrieving database server time is:-

     public string database_time() throws sqlexception        {          con = getconnection();         string sql = "select getdate()";         clstmt = con.preparecall(sql);           clstmt.execute();         rs = clstmt.getresultset();         while(rs.next()) {         timestr= rs.getstring(1);         }          system.out.println("database time is" +timestr);          return timestr;        }  

another method retrieving 1 hour ago time

  public string previostime() throws parseexception, sqlexception     {         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);       fixeddate = calendar.gettime();       stringdate = simpledateformat.format(fixeddate );      system.out.println("previous date is"+(stringdate));      system.out.println("current date is"+timestr);      return stringdate;      } 

but when use stringdate , timestr in sql query ,then got error com.microsoft.sqlserver.jdbc.sqlserverexception: conversion failed when converting date and/or time character string.

code sql query is:- string sql = "select * vs1_bag logtime between 'stringdate'and 'timestr' ";

edit:- method through retrieve time,and use in application is:-

public string [] getcharttime() throws sqlexception, parseexception     {        list<string> tstr = new arraylist<string>();     database_time();     string atime[] = null;     previostime();     getconnection();     try         {              con = getconnection();               stmt = con.createstatement();   string sql = "select * vs1_bag logtime between 'stringdate'and 'timestr' ";            stmt.executequery(sql);            rs = stmt.getresultset();          while(rs.next()) {             // value of column, , add list             tstr.add(rs.getstring(1).substring(11,16));            }          }         catch( exception e )         {             system.out.println("\nexception in  bean in getdbtable(string code):"+e);         }                     {             closeconnection();          }           // return list here, let's convert array      atime=  tstr.toarray(new string[tstr.size()]);       return atime;       } 

how resolve it.

shouldn't this? :

string sql = "select * vs1_bag logtime between '"+stringdate+"' , '"+timestr+"' "; 

Comments

Popular posts from this blog

javascript - Wordpress slider, not displayed 100% width -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -