2

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)?

1
  • Why are you using this "id=(?)"? Just "id=?" is not enough? I'm not saying that this is wrong, ok? It's because I can't remember about parameters in JDBC PreparedStatements. From what I remember, the parentheses is not necessary... Commented Feb 1, 2013 at 19:23

2 Answers 2

5

Looks like an unclosed quote issue ?

name='updatedname and address
Sign up to request clarification or add additional context in comments.

4 Comments

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'updatedname'
@Eslam.. Check the type of name column in database. I suspect you set it as Double. It should be Varchar.
the type of the column does not match the value type you are trying to insert, String to Double perhaps ?
I SWEAR THAT I'AM SETTING BOTH NAME AND ADDRESS AS VARCHAR(45)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.