Search code examples
jdbcsql-updateucanaccessmemo

Update datetime and a string in Memo field using Java and UCanAccess


I want to update datetime and a string in Memo field using UCanAccess. For example - 5/27/2015 System : some string, this is all I have to update in memo field.

What I have tried - I am converting date to a string using following code:

DateFormat df = new SimpleDateFormat("yyyyMMdd");
Date today = Calendar.getInstance().getTime();              
String reportDate = df.format(today);

and then using update query of ucanaccess like this:

st.executeUpdate(" update tblCaseInventory set fldContactNotes = " + reportDate   + "' System : CAR Report '" +  "  where fldCaseNumber = " +  rs1.getInt("fldCaseNumber"));

but I am getting an error:

Unexpected token : System.

How to achieve this?


Solution

  • You should use a PreparedStatement to run a parameterized query, something like this:

    String sql = 
            "UPDATE tblCaseInventory " +
            "SET fldContactNotes=? " +
            "WHERE fldCaseNumber=? ";
    try (PreparedStatement ps = conn.prepareStatement(sql)) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");                                                   
        java.util.Date today = Calendar.getInstance().getTime();              
        String reportDate = df.format(today);
        ps.setString(1, reportDate + " System : CAR Report");
        ps.setInt(2, rs1.getInt("fldCaseNumber"));
        ps.executeUpdate();
    }