0

I am trying to write a function in Java that updates the description and title fields of a Mysql table however when I pass the int variable 'urlid' nothing is added. If I change the urlid variable (at the very end of the query) to another int variable (for example int i = 2) then it works fine. What is it about this urlid that makes things go wrong?

public void updateDescription( String desc, String title, int urlid ) throws SQLException, IOException {
        String cutDesc = desc.substring(0, 99);
            Statement stat = connection.createStatement();
            String query = "UPDATE urls SET description = '"+cutDesc+"', title = '"+title+"' WHERE urlid =" + urlid;
            stat.executeUpdate( query );
            stat.close();
    }
3
  • What does transaction mean? Sorry I am fairly new to Mysql and I have never heard that term before? Commented Apr 10, 2014 at 11:16
  • 1
    For the benefit of anyone who might post an answer without knowing all the background - stackoverflow.com/questions/22982527/… Commented Apr 10, 2014 at 11:16
  • paste the stacktrce here Commented Apr 10, 2014 at 11:36

2 Answers 2

0

i guess you miss the quotes ,

"UPDATE urls SET description = '"+cutDesc+"', title = '"+title+"' WHERE urlid ='"+ urlid+"'";

Hope this helps !!

Note: use prepared statements to avoid sql injection . see here

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

1 Comment

Your current quotes do exactly nothing to the java String object, I think you forgot to add the single quotes ' around urlid
0

I guess you can try with WHERE urlid ='"+ urlid+"'"; at the end.

for your reference http://dev.mysql.com/doc/refman/5.7/en/type-conversion.html

3 Comments

Still nothing is being inserted into the title or description columns
what values are you passing into the method updateDescription( String desc, String title, int urlid ) ?? if you are passing correct values in correct format it should definitely work. kindly check the values you are providing to the method. you can always use a System.out.println(); to check the values.
you can also call this method this way updateDescription("abc", "def", 1)

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.