我想将 java Timestamp 插入 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());
错误:[Microsoft][ODBC Microsoft Access Driver] 标准表达式中的数据类型不匹配。
建议的更改:
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)