0

I am trying to update the SSN for a customer by searching for them based on the old SSN then updating it. What am I missing? This will not return a result even though i know i have matches for ssNum in the database. Thanks.

String query = "UPDATE Customers SET ss_num = ('" + updateSsn
                + "') WHERE ss_num = ('" + ssNum + "')";
2
  • 1
    You should specify your DBMS and the exception that's been thrown Commented Dec 1, 2012 at 2:23
  • Please use PreparedStatement with a parameterized query to guard against things like SQL injection. Commented Dec 1, 2012 at 8:54

2 Answers 2

3

That type of query is unsafe (vulnerable to SQL injection). Write your query as follows and use PreparedStatement:

String query = "UPDATE Customers SET ss_num = ? WHERE ss_num = ?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, updateSsn);
ps.setString(2, ssnNum);
Sign up to request clarification or add additional context in comments.

Comments

1

you need to use executeUpdate() method, which doesn't return ResultSet, but it will return numberOfRowsUpdated

Use PreparedStatement instead

1 Comment

Thanks! It was the executeUpdate() that was the issue.

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.