mysql - Using Timestamp in java sql prepared statement -
i trying execute select query using prepared statement in java. in clause im checking condition on timestamp type column shown below.
string selectsql = "select * db.keycontacts createddatetime>?"; preparedstatement preparedstatement = connect.preparestatement(selectsql); preparedstatement.settimestamp(1, convertstrtotimestamp(lastsynctimestamp)); resultset = preparedstatement.executequery(selectsql );
//function convert timestampstring java.sql.timestamp
private java.sql.timestamp convertstrtotimestamp(string datetimestr){ java.sql.timestamp timestampdate = null; try { dateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");//2015-05-11 18:26:55 java.util.date dateobj = (java.util.date)formatter.parse(datetimestr); timestampdate = new timestamp(dateobj.gettime()); } catch (parseexception e) { // todo auto-generated catch block e.printstacktrace(); } return timestampdate; }
when query executed, getting following exception.
com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near '?' @ line 1
so im going wrong here?
thanks in advance.
remove parameter from
resultset = preparedstatement.executequery(selectsql );
and change to
resultset = preparedstatement.executequery( );
the query passed in preparedstatement.executequery(selectsql );
takes priority on query passed in connect.preparestatement(selectsql);
simple string ("select * db.keycontacts createddatetime>?"
) in dint set parameter there syntax error ?
and can statement prepared @ preparedstatement preparedstatement = connect.preparestatement(selectsql);
since executequery()
inherited statement execute query without preparing it.
Comments
Post a Comment