mysql - java.sql.SQLException while inserting multiple date -
i'm getting error when try insert more 1 date values table, execute when try 1 date value.
the error got when using more 2 date values is:
java.sql.sqlexception: [mysql][odbc 5.1 driver][mysqld-5.5.15]you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'from) values(100,1,_binary'2011-09-17',_binary'2011-09-18')' @ line 1 @ sun.jdbc.odbc.jdbcodbc.createsqlexception(unknown source) @ sun.jdbc.odbc.jdbcodbc.standarderror(unknown source) @ sun.jdbc.odbc.jdbcodbc.sqlexecute(unknown source) @ sun.jdbc.odbc.jdbcodbcpreparedstatement.execute(unknown source) @ sun.jdbc.odbc.jdbcodbcpreparedstatement.executeupdate(unknown source) @ sampleprog.insertdate(sampleprog.java:37) @ sampleprog.main(sampleprog.java:65)
my source code :
import java.text.dateformat; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; import java.io.ioexception; import java.sql.*; public class sampleprog { public void insertdate(int ref,int id,date d1,date d2) { connection con=null; preparedstatement ps=null; string query=null; java.sql.date dd1=null,dd2=null; try { query="insert new_table (ref_no,emp_id,doapp,from) values(?,?,?,?)"; class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con=drivermanager.getconnection("jdbc:odbc:date","root","1234"); ps=con.preparestatement(query); ps.setint(1, ref); ps.setint(2,id); ps.setdate(3,new java.sql.date(d1.gettime()) ); ps.setdate(4, new java.sql.date(d2.gettime())); int i=ps.executeupdate(); } catch(exception e) { e.printstacktrace(); } } public static void main(string args[]) { date d1=null; date d2=null; int ref=100; int empid=1; string date1="2011-09-17"; string date2="2011-09-18"; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); try { d1=sdf.parse(s1); d2=sdf.parse(s2); } catch (parseexception e) { e.printstacktrace(); } sampleprog dd=new sampleprog(); dd.insertdate(ref,empid,d1,d2); } }
from
reserved keyword of mysql. must escaped order used.
insert new_table(ref_no,emp_id,doapp,`from`) values (?,?,?,?)
if have time alter table, please change name of column name avoid future problems.
Comments
Post a Comment