0

我正在尝试在以时间戳作为列值的表中插入新行。以下是我的代码:

long millisecs = System.currentTimeMillis() ;
Timestamp ts = new java.sql.Timestamp(millisecs) ;
s.executeUpdate("INSERT INTO tblPublicHols(Date) VALUES("+ts+")");

我还尝试使用 PreparedStatment 进行如下操作:

long millisecs = System.currentTimeMillis() ;
Timestamp ts = new java.sql.Timestamp(millisecs) ;
PreparedStatement pstmt ;
pstmt = conn.prepareStatement("INSERT INTO tblBasicHoliday " +
                 "(Date, RegionID) " +
                 "VALUES (?, ?)") ;
pstmt.clearParameters() ;
pstmt.setTimestamp(1, ts);
pstmt.setInt(2, 1);
int count = 0 ;
count = pstmt.executeUpdate() ;

我仍然收到相同的错误,如下所示

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.

谁能帮我解决这个问题?谢谢。

4

2 回答 2

0

I strongly suspect that the problem is that Date is a reserved word. Try:

String sql = "INSERT INTO tblBasicHoliday ([Date], RegionID) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);

(Note that you should be closing the statement in a finally block, too.)

于 2013-05-02T16:57:26.430 回答
0

问题是这DATEMS Access 中的保留字。您应该更改列名和 SQL 语句以使用另一个不是保留字的列名以避免此错误。

于 2013-05-02T16:59:29.687 回答