[英]How to convert Java TimeStamp into ms access Date?
I would like to insert a java Timestamp into an msaccess database but I am getting an error which is listed below. 我想将一个java时间戳插入一个msaccess数据库,但我收到一个错误,如下所示。 The ms-access field has been set to a DATE datatype.
ms-access字段已设置为DATE数据类型。 Any advise would be deeply appreciated.
任何建议都将深表感谢。 Thanks
谢谢
Here's my DAO class method: 这是我的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();
My DTO method: 我的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);
}
My Timestamp method: 我的时间戳方法:
public Timestamp addTimestamp(){
java.util.Date date= new java.util.Date();
return new Timestamp(date.getTime());
}
Error: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression. 错误:[Microsoft] [ODBC Microsoft Access驱动程序]条件表达式中的数据类型不匹配。
You need to convert your java.util.Data
to a java.sql.Date
or if you need the precision of the java.sql.Timestamp
change your Database type to TIMESTAMP
您需要将
java.util.Data
转换为java.sql.Date
或者如果需要java.sql.Timestamp
的精度,请将Database类型更改为TIMESTAMP
some code to create a java.sql.Date
: 一些代码来创建
java.sql.Date
:
java.util.Date today = new java.util.Date();
long t = today.getTime();
java.sql.Date dt = new java.sql.Date(t);
You can then put the java.sql.Date in your Database, did always work for me. 然后你可以把java.sql.Date放在你的数据库中,总是对我有用。
For Info: 对于信息:
java.sql.Date
corresponds to SQL DATE
it stores years, months and days while hour, minute, second and millisecond are ignored. java.sql.Date
对应于SQL DATE
它存储年,月和日,而小时,分钟,秒和毫秒则被忽略。 Additionally sql.Date
isn't tied to timezones. sql.Date
与时区sql.Date
。 java.sql.Time
corresponds to SQL TIME
andonly contains information about hour, minutes, seconds and milliseconds . java.sql.Time
对应于SQL TIME
仅包含有关小时,分钟,秒和毫秒的信息 。 java.sql.Timestamp
corresponds to SQL TIMESTAMP
which is the date to the nanosecond ( note that util.Date
only supports milliseconds! ). java.sql.Timestamp
对应于SQL TIMESTAMP
,它是纳秒的日期( 注意util.Date
只支持毫秒! )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.