Search code examples
javasqljdbcsqlexception

sql exception:Parameter index out of range


I wrote the following method to execute update statement inside the database:

 public boolean updateEmployee(int id){
    try {
        String update="UPDATE employee set name='updatedName' and address='updatedaddress' where id=(?)";
        pst=connection.prepareStatement(update);
        pst.setInt(1, id);
        pst.executeUpdate();

    } catch (SQLException ex) {
        Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
    return true;
 }

But the following exception occurs:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

And this exception is also thrown:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'UpdatedName'

What's the problem here? In addition, how can I return the result of an update statement (since executeQuery doesn't work with DML Statements)?


Solution

  • I might be wrong, but perhaps the statement should be

    UPDATE employee set name='updatedName' , address='updatedaddress' where id=(?)
    

    instead of

    UPDATE employee set name='updatedName' and address='updatedaddress' where id=(?)

    and should be used only in the where clause.