Matija
Matija

Reputation: 483

update statement Java sql exception: Too few parameters expected one more

So I have a method for updating some fields in a MS Access 2007 table, and whenever I try to update I get the following exception: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 11. It always shows that one more is expected than there are in the query.

The code of the method is:

try {
        String upit = "UPDATE Izlagac SET NazivIzlagaca=?, PIB=?, Telefon=?, "
                + " KontaktOsoba=?, Email=?, TipIzlagaca=?, Ulica=?, Broj=?, Mesto=?, Drzava=? WHERE "
                + " SifraIzlagaca = " + i.getSifraIzlagaca() + ";";
        PreparedStatement pstmt = con.prepareStatement(upit);
        pstmt.setString(1, i.getNazivIzlagaca());
        pstmt.setString(2, i.getPIB());
        pstmt.setString(3, i.getTelefon());
        pstmt.setString(4, i.getKontaktOsoba());
        pstmt.setString(5, i.getEmail());
        pstmt.setString(6, i.getTipIzlagaca());
        pstmt.setString(7, i.getUlica());
        pstmt.setString(8, i.getBroj());
        pstmt.setString(9, i.getMesto());
        pstmt.setString(10, i.getDrzava());
        System.out.println(pstmt.toString());
        pstmt.executeUpdate();
        pstmt.close();
        return true;
    } catch (SQLException ex) {
        Logger.getLogger(DBBroker.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }

I am not using aliases so that doesn't seem to be the reason why the driver would expect additional parameter. All the column names are correctly spelled, triple checked it.

Upvotes: 1

Views: 667

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123829

There is no need to use string concatenation to build the WHERE clause; parameters are just as valid there as they are in the other parts of the PreparedStatement. So, just use

String upit = "UPDATE Izlagac SET NazivIzlagaca=?, PIB=?, Telefon=?, "
        + " KontaktOsoba=?, Email=?, TipIzlagaca=?, Ulica=?, Broj=?, Mesto=?, Drzava=? WHERE "
        + " SifraIzlagaca=?";
PreparedStatement pstmt = con.prepareStatement(upit);
pstmt.setString(1, i.getNazivIzlagaca());
pstmt.setString(2, i.getPIB());
pstmt.setString(3, i.getTelefon());
pstmt.setString(4, i.getKontaktOsoba());
pstmt.setString(5, i.getEmail());
pstmt.setString(6, i.getTipIzlagaca());
pstmt.setString(7, i.getUlica());
pstmt.setString(8, i.getBroj());
pstmt.setString(9, i.getMesto());
pstmt.setString(10, i.getDrzava());
pstmt.setString(11, i.getSifraIzlagaca());

Upvotes: 1

Related Questions