如何在沒有時間的情況下將日期插入到MySQL數據庫表中? 我嘗試了這些代碼,但我得到以下異常:如何在Java中將日期插入到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
}
爲什麼不使用MySQL日期數據類型(http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html)並將該列配置爲默認值? –
在出錯的地方可以看到,我只是想知道爲什麼「程序員」在提出問題之前沒有閱讀錯誤信息。注意你如何構建MySQL查詢。特別是報價。特別是你沒有匹配的報價。 –
檢查您的查詢語法..有什麼地方出錯..張貼您的查詢代碼 – Lakshmi