wood burning stoves 2.0*
The moose likes JDBC and the fly likes MSAccess and JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MSAccess and JDBC" Watch "MSAccess and JDBC" New topic
Author

MSAccess and JDBC

M Devastated
Greenhorn

Joined: May 20, 2003
Posts: 2
I'm using MSAccess and java and I'm trying to do something like this:
ps = connection.prepareStatement("UPDATE table SET int_1 = ?, int_2 = ?, string_1 = ?, date_1 = ? WHERE id = ?");
ps.setInt(1, 1);
ps.setInt(2, 2);
ps.setString(3, "aString");
Calendar rightNow1 = Calendar.getInstance();
rightNow1.set(rightNow1.MILLISECOND,0);
java.util.Date tempDate1 = rightNow1.getTime();
Timestamp sysDate1 = new Timestamp(tempDate1.getTime());
ps.setTimestamp(4, sysDate1);
and I get:
Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement
please help...
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Where do set the 5th parameter in your PreparedStatement. I only see you setting 4?
is there more code? At which line is the exception being thrown?
Jamie
M Devastated
Greenhorn

Joined: May 20, 2003
Posts: 2
OK, I forgot to post the complete code...here it is. The fifth parameter is the criteria of the database search.
Though the problem lies with the date-setting, cause if I remove the 4th param from preparedstatement and remove ps.setTimestamp() from the code, everything works fine.
Here is the complete code:
ps = connection.prepareStatement("UPDATE table SET int_1 = ?, int_2 = ?, string_1 = ?, date_1 = ? WHERE id = ?");
ps.setInt(1, 1);
ps.setInt(2, 2);
ps.setString(3, "aString");
Calendar rightNow1 = Calendar.getInstance();
rightNow1.set(rightNow1.MILLISECOND,0);
java.util.Date tempDate1 = rightNow1.getTime();
Timestamp sysDate1 = new Timestamp(tempDate1.getTime());
ps.setTimestamp(4, sysDate1);
ps.setInt(5, 5);
SJ Adnams
Ranch Hand

Joined: Sep 28, 2001
Posts: 925
I have no experience with MS Access, however "Timestamp" is a different beast to "Date". Are you sure Access supports Timestamp (this was new for Oracle 9i), whats the table definition?
AFIAK Timestamp has got timezone information & other stuff in it?
Simon
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Hi M,
On my Windows XP machine with J2SE SDK 1.4.1_02 and Micro$oft Access 2002, I have the following table definition:
Table: Table1
Columns:
  • id AutoNumber
  • name Text
  • updated Dat/Time


  • The following code updates a row in this table.

    Hope it helps you.
    Good Luck,
    Avi.
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    M Devastated,
    Welcome to JavaRanch! We ain't got many rules 'round these parts, but we do got one. Please change your display name to conform to the JavaRanch naming policy.
    Thanks pardner. Hope to see you 'round the Ranch!


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Stefan Wagner
    Ranch Hand

    Joined: Jun 02, 2003
    Posts: 1923

    Perhaps you only mixed up java.util.Date (not suitable for jdbc) and java.sql.Date (that 's the right one)?


    http://home.arcor.de/hirnstrom/bewerbung
    Stephen Rozum
    Greenhorn

    Joined: Sep 22, 2004
    Posts: 1
    Hello All,

    While doing a google search I came across this posting in which I had the exact same issue where I would get exceptions trying to update my "Timestamp" Date/Time column. Queries worked as expected.

    The final issue concerned the "Timestamp" column. It seems the "Timestamp" label was mistakenly being parsed as a command and not a column name.

    The solutions was to add quotes around the name. As shown below:



    If the column variable did not have the bracketing quotes a SQL exception would occur.

    Note: If the column name changed, no SQL exceptions occured.

    As a rule of thumb, from now on all my column names will have quotes.

    Hope this helps someone out in the future.


    Stephen
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: MSAccess and JDBC
     
    Similar Threads
    sql Date, and getting Primary Key!
    MySQL 4.0.17 PreparedStatement Fails!
    save Date() into mysql Timestamp
    Inserting date into an MS Access DB
    PostgreSQL prepared statement failure