无效的日期时间格式:将日期/时间插入Java的Access中 [英] invalid datetime format: insert date/time into Access from Java
问题描述
我想在Access中插入一个datetime值,但出现此错误:
I want to insert a datetime value to Access, but I get this error:
net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.4数据异常:无效的日期时间格式
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.4 data exception: invalid datetime format
这是代码:
private void txtsubmitActionPerformed(java.awt.event.ActionEvent evt) {
SimpleDateFormat A = new SimpleDateFormat("dd/MM/yyyy");
Peminjaman P= new Peminjaman(setIDTrans(),txtid.getText(),A.format(txttglpinjam.getDate()),A.format(txttglkembali.getDate()),txtplat.getText(),txtnama.getText(),txtdriver.getText());
c.InsertPeminjamanD(P);
推荐答案
错误消息表明您的InsertPeminjamanD
方法最终试图做类似的事情
The error message indicates that your InsertPeminjamanD
method is ultimately trying to do something like
INSERT INTO Table1 (DateTimeField) VALUES ('31/05/2016')
并且由于以下两个原因而无效:
and that won't work for two reasons:
-
UCanAccess希望日期文字被括在井号(
#
)中,并且
UCanAccess通常期望xx/yy/zzzz
日期文字是MM/dd/yyyy
,而不是dd/MM/yyyy
.
UCanAccess normally expects xx/yy/zzzz
date literals to be MM/dd/yyyy
, not dd/MM/yyyy
.
因此,如果将上面的查询重新排列为...,那么它将正常工作.
So, the above query will work if it is rearranged as ...
INSERT INTO Table1 (DateTimeField) VALUES (#05/31/2016#)
...尽管使用PreparedStatement并为其传递正确的日期值确实会更好:
... although it really would be better to use a PreparedStatement and pass it a proper date value:
String sql = "INSERT INTO Table1 (DateTimeField) VALUES (?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDate(1, java.sql.Date.valueOf("2016-05-31"));
ps.executeUpdate();
这篇关于无效的日期时间格式:将日期/时间插入Java的Access中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!