Results 1 to 5 of 5
  1. #1
    nowone is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Question [SOLVED] MS Access UPDATE and PreparedStatement

    Hello, sorry to bother, but I have this problem and I really don't see where I'm mistaking.

    This code here:
    Java Code:
     public void update(Cliente c) {
            if (isConnected()) {
                PreparedStatement stmt = null;
                try {
                    stmt = this.connection.prepareStatement("UPDATE Contatti SET "
                            + "Codice=?, Denominazione=?, Indirizzo=?, CAP=?, Citta=?, Nazione=?, "
                            + "CF=?, PIVA=?, Telefono1=?, Telefono2=?, Fax=?, Email=?, "
                            + "IBAN=?, Pagamento=?, Accisa1=?, Accisa2=?, Accisa3=?, Ruolo1=?, Ruolo2=?, Ruolo3=?, "
                            + "Note=?, Tipo=? "
                            + "WHERE ID=?");
                    stmt.setString(1, c.getCodice());
                    stmt.setString(2, c.getDenominazione());
                    stmt.setString(3, c.getIndirizzo());
                    stmt.setString(4, c.getCAP());
                    stmt.setString(5, c.getCitta());
                    stmt.setInt(6, c.getNazione());
                    stmt.setString(7, c.getCodiceFiscale());
                    stmt.setString(8, c.getPartitaIVA());
                    stmt.setString(9, c.getTelefono());
                    stmt.setString(10, c.getCellulare());
                    stmt.setString(11, c.getFax());
                    stmt.setString(12, c.getEmail());
                    stmt.setString(13, c.getIBAN());
                    stmt.setString(14, c.getMetodoPagamento());
                    stmt.setString(15, c.getAccisa1());
                    stmt.setString(16, c.getAccisa2());
                    stmt.setString(17, c.getAccisa3());
                    stmt.setInt(18, c.getRuolo1());
                    stmt.setInt(19, c.getRuolo2());
                    stmt.setInt(20, c.getRuolo3());
                    stmt.setString(21, c.getNote());
                    stmt.setString(22, c.getTipo());
                    stmt.setInt(23, c.getID());
    
                    stmt.executeUpdate();
                    // this.connection.commit();
    
                } catch (SQLException ex) {
                    Logger.getLogger(DBEngine.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        stmt.close();
                    } catch (Exception ex) {
                        Logger.getLogger(DBEngine.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    It's supposed to update a table in a MS access 2007 DB using a PreparedStatement. Pretty basic I think. Everything else (INSERT, DELETE) works fine. UPDATE don't and JDBC/ODBC gives me a "Syntax error in the UPDATE query". :confused:

    Table field names are correct (that was the first thing to check, right?). The error should be pretty obvious but I can't see it.

    Thanks in adavnce,
    Fabio.
    Last edited by nowone; 02-11-2011 at 08:33 AM. Reason: Solved.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Are any of those things provided in "c" an empty string or null? If so, you probably want to use setNull rather than setString.

  3. #3
    nowone is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply. I'm trying something like:
    Java Code:
    private void setString(PreparedStatement s, int n, String c) throws SQLException {
            if (null == c) {
                s.setNull(n, Types.VARCHAR);
            } else {
                s.setString(n, c);
            }
        }
    to set Strings but nothing changes. :(
    Fabio.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Try, in addition, surrounding all columnnames with [], i.e. [Accisa3]= instead of Accisa3=

    Maybe one of those columns is a reserved word. I.E. Fax, Email, CF, and Note as possibilities, there.

  5. #5
    nowone is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. PreparedStatement using batch update
    By swati.jyoti in forum New To Java
    Replies: 3
    Last Post: 06-02-2009, 10:05 AM
  2. Update a record in Random access file
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 10:07 PM
  3. Using PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 11:24 AM
  4. PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 03:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •