我想在msaccess数据库中插入Java时间戳,但出现以下错误。 ms-access字段已设置为日期数据类型。任何建议将不胜感激。谢谢

这是我的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 date= new java.util.Date();
    return new Timestamp(date.getTime());
    }


错误:

java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]查询表达式“ 2012-11-10 09:30:57.174)中的字符串语法错误”。
    在sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    在sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    在sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)
    在sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
    在sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)

最佳答案

您的查询中存在语法错误。尝试将单引号放在saletimestamp的末尾

String sql = "INSERT INTO Sale (SaleDetails, SaleTotal, SaleTimestamp)"
                + " VALUES ('"+saleDetails+"','"+saleTotal+"','"+saleTimestamp+"')";

09-12 16:54