How to send date and time to SQL using java -
i calling stored procedure in database. 2 of parameters requires date , time in sql date format.
string x = new simpledateformat("mm-dd-yyyy").format(new date()) + " 00:00:00 am"; string y = new simpledateformat("mm-dd-yyyy").format(new date()) + " 11:59:00 pm"; date fromdate = null; date todate = null; try { fromdate = new simpledateformat("mm-dd-yyyy hh:mm:ss a").parse(x); todate = new simpledateformat("mm-dd-yyyy hh:mm:ss a").parse(y); } catch (parseexception ex) { } callablestatement proc_stmt = con.preparecall("{ call someproc(?,?) }"); proc_stmt.setdate(1, (java.sql.date) fromdate); proc_stmt.setdate(2, (java.sql.date) todate);
i believe if send date(excluding time), code works, of no use me database not generate correct results. when run above code
classcastexception:java.util.date cannot cast java.sql.date
any solution?
use java.sql.timestamp
class holds date , time sql fields, , callablestatement#settimestamp
:
proc_stmt.settimestamp(1, new java.sql.timestamp(fromdate.gettime());
Comments
Post a Comment