• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MSAccess and JDBC

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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!
     
    Ranch Hand
    Posts: 1923
    Scala Postgres Database Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Perhaps you only mixed up java.util.Date (not suitable for jdbc) and java.sql.Date (that 's the right one)?
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic