如何将日期(不带时间)插入到MySQL数据库表中? 我尝试了以下代码,但是出现以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'Mar 05 00:00:00 GMT-08:00 2014,1,1)' at line 1
注意:在MySQL表中,我选择了日期数据类型作为此列的数据类型。
String Date = "\\d\\d\\d\\d\\D[0-1][0-9]\\D[0-3][0-9]";
while (DateMatcher.find()) {
String date = DateMatcher.group().trim();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse(date);
db.insert_date(myDate,2,1);
}
我遇到的问题只涉及日期部分 插入日期的查询语句:
// insert DATE
public void insert_date(Date date_str, int sentence_id ,int document_id ){
// Statements allow to issue SQL queries to the database
try {
statement = connect.createStatement();
System.out.println( "insert into Date(date_Str,Sen_id,doc_id) " +
" values(" + date_str + "," + sentence_id + "," + document_id + ")"
);
statement.executeUpdate(
"insert into test.Date(date,Sen_id,doc_id)" +
" values(" + date_str + "," + sentence_id + "," + document_id + ")"
);
}
catch(Exception e ){System.out.println(e);};
// Result set get the result of the SQL query
}