0

I have created a table in ms access. I have set the data type of ID to Auto Number in MS-access. In java when I try to update a record. the netBeans IDE gives me the error of " data type missmatch in criteria expression". But when I changed the ID number that was not in the table already it works well. The code is below.

String sql = "Update table1 set price ='" + txtPrice.getText() + "', quantity='" + txtQuantity.getText() + "', description='" + txtDescription.getText() + "' where id= " + txtid.getText() + "";
        try {
             pst = conn.prepareStatement(sql);
            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Updated");
            UpdateJTable();
        } catch (Exception e) {

            JOptionPane.showMessageDialog(null, e);
        }
1
  • It's good to post the stacktrace, if you are receiving error. Commented Jun 3, 2012 at 5:45

4 Answers 4

1

I think you may be confusing a regular Statement and PreparedStatement objects, too. PreparedStatement gives you the opportunity to use placeholders (the ? you might see in a lot of sql statements) and let the PreparedStatement do a bit more work for you.

Try writing your sql statement like this:

update table1 set price = ?, quantity = ?, description = ? where id = ?

Then with your PreparedStatement object, you would do:

pStmt.setInteger(1, Integer.valueOf(txtPrice.getText())

for each parameter. It has been a few years since I used PreparedStatement directly, but if memory serves, parameters are 1-based.

But your basic problem is one of casting. It also might make your code cleaner (and more secure) to use a PreparedStatement for this.

Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming the price and quantity columns are integers, hence you dont need to quote them.

Try the following sql:

String sql = "Update table1 set price =" + txtPrice.getText() + ",
              quantity=" + txtQuantity.getText() + ", description='" +
              txtDescription.getText() + "' where id= " + txtid.getText() + "";

Note the single-quotes are missing from price and quantity

1 Comment

Suraj chandran there is another error after doing that the error is of "Too few parameters" no whats this.
0

Try to use the type casting in your id field first. like:

int id = Integer.parseInt(txtid.getText());

Then execute query :

String sql = "Update table1 set price ='" + txtPrice.getText() + "', quantity='" +
              txtQuantity.getText() + "', description='" + txtDescription.getText() + "' 
              where id= " + id + "";

Comments

0

for the too few parameters error that you are getting - double check the field names in you query.

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.