Possible Duplicate:
How to convert Java TimeStamp into ms access Date?如何將Timestamp(date.getTime())轉換爲ms訪問日期?
我想將Java時間戳到MSACCESS數據庫,但我得到它下面列出的錯誤。 ms-access字段已被設置爲DATE數據類型。任何意見將深表謝意。由於
這裏是我的DAO類方法:
public void addSale(String saleDetails, String saleTotal, Timestamp saleTimestamp)
throws ClassNotFoundException, SQLException {
Statement myStatement = getConnection();
String sql = "INSERT INTO Sale (SaleDetails, SaleTotal, SaleTimestamp)"
+ " VALUES ('"+saleDetails+"','"+saleTotal+"','"+saleTimestamp+"')";
myStatement.executeUpdate(sql);
closeConnection();
我的DTO方法:
public void storeSale(String saleDetails, String saleTotal, Timestamp saleTimestamp){
DAO dao = DAO.getDAO();
try {
dao.addSale(saleDetails, saleTotal, saleTimestamp);
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(Sale.class.getName()).log(Level.SEVERE, null, ex);
}
我的時間戳方法:
public Timestamp addTimestamp(){
java.util.Date today = new java.util.Date();
long time = today.getTime();
java.sql.Date date = new java.sql.Date(time);
return new Timestamp(date.getTime());
錯誤: [微軟] [ODBC Microsoft Access中驅動程序]標準表達式中的數據類型不匹配。
變化Recommemded:
public void addSale(String saleDetails, String saleTotal)
throws ClassNotFoundException, SQLException {
Statement myStatement = getConnection();
String sql = "INSERT INTO Sale (SaleDetails, SaleTotal, SaleTimestamp)"
+ " VALUES ('"+saleDetails+"','"+saleTotal+"',Now())";
myStatement.executeUpdate(sql);
closeConnection();
新的錯誤:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in string in query expression ''Now())'.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)
是的,只有兩個小時前,「有一個公認的答案「,原始線程@Adesh有什麼問題? –
等等,還有一個小時就是這個強迫症,還是在發佈三次後有一個神祕的祕密迴應? –
其實每個問題都略有不同......但你是對的。我不應該很快接受答案。 – Adesh