Re: how do you update a java.sql.date type in an ms access database




"chose" wrote...

> when i update a row in an access database the date field
> does not update to the correct date it brings up some
> funny date which i dont know where it comes from.

It comes from db's attempt to parse an invalid date-string...

In Access/Jet, it needs to be enclosed within #:s, and with the date parts
in a specific order...

Another thing is that java.sql.Date only contains the date part, whereas the
DateTime data type in Access/Jet is more like a java.sql.Timestamp.

> Below is the method i use to update. maybe
> the date types are not compatable?

Well, partly, but you're not using the date AS a date in the SQL statement,
but as a String. This will in most cases lead to errors, as the parsing of
such is left to the database, not the driver...

> s.executeUpdate("update Rooms set CustomerId=" + custID
> + ", BookINDate = " + bkInDate + ", Occupied = " + yes
> + " where RoomNo = " + rNumber + " ");


It's really bad practice to concatenate the string for an SQL statement in
that way, because of differences between different dbs. Instead you should
use a parameterized statement.

Use java.sql.Timestamp instead of java.sql.Date.
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Date.html

Read up on how to use a PreparedStatement, and it will solve your problem.

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/PreparedStatement.html

// Bjorn A


.



Relevant Pages

  • Re: Dates in a SQL Statement
    ... I'm using ADODB to connect to an ACCESS database I've set up. ... fields that are Date types and I send the SQL statement via a string, ... My SQL Statement ... it'll turn my variable dateFrom into just a string. ...
    (microsoft.public.excel.programming)
  • Re: Send Message to filemaker 5.5 from windows form application
    ... Script created by Steve Carson and it was modified by Kristina ... ' Chrto represent double quotes if you are passing a string. ... Dim pHyperlink As IHyperlink ... 'opens Microsoft Access database ...
    (comp.databases.filemaker)
  • Re: email report per record
    ... is a couple of Access database ... Dim strSubject As String 'Email Subject ... Dim stDocName As String ...
    (microsoft.public.access.reports)
  • Network user count question
    ... an MS Access database on a network server? ... Public Function AddUser(mUserID As String, ... Dim bOK As Boolean ... Dim obj As User ...
    (microsoft.public.vb.database.ado)
  • Cannot Open Any more databases
    ... records from an Access database. ... Function PopDataTable(ByVal ods As DataSet, ByVal sTable As String, ... OleDbConnection = Nothing, Optional ByVal ocomSel As OleDbCommand = ... sRelation, shChildCount) ...
    (microsoft.public.dotnet.framework.adonet)