当我尝试向表中插入多个日期值时出现错误,但当我尝试使用一个日期值时它成功执行。
使用超过 2 个日期值时出现的错误是:
java.sql.SQLException: [MySQL][ODBC 5.1 Driver][mysqld-5.5.15]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'From) values(100,1,_binary'2011-09-17',_binary'2011-09-18')' at line 1
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
at SampleProg.InsertDate(SampleProg.java:37)
at SampleProg.main(SampleProg.java:65)
我的源代码是:
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 into 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);
}
}