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();
    }

2 Answers2

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

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • Your current quotes do exactly nothing to the java `String` object, I think you forgot to add the single quotes `'` around urlid – Ceiling Gecko Apr 10 '14 at 11:19
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

jmail
  • 5,944
  • 3
  • 21
  • 35
Krish0912
  • 1
  • 1
  • Still nothing is being inserted into the title or description columns – user3519169 Apr 10 '14 at 11:35
  • 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. – Krish0912 Apr 10 '14 at 11:40
  • you can also call this method this way updateDescription("abc", "def", 1) – Krish0912 Apr 10 '14 at 11:43