0

I have a function in Java that is meant to update the description field and the title field of a table in a mysql database that looks like this:

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

When this function is called with:

updateDescription(desc, title, urlID);

Nothing is put into the table. There are no errors, it just seems to ignore it. Any ideas what is wrong here?

Thanks

2
  • have you print the query and try to execute it manually on your data base ? Commented Apr 10, 2014 at 8:22
  • It only fails if the where condition did not fetch any records. Commented Apr 10, 2014 at 8:35

3 Answers 3

1

You said nothing is put into the table.

But UPDATE doesn't put things into the table - it only modifies rows that already exist. You need to use INSERT instead of UPDATE.

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

5 Comments

I'm sorry I was not clear enough. I already have values in the other two columns. In the description and title field at the moment is just a blank value. There are four columns, two of which had values inserted with the following command String query = "INSERT INTO urls VALUES ('"+urlID+"','"+url+"','','')"; Description and title are the final two columns in the query
OK, what is the type of column urlID? The INSERT statement in your comment suggests that it's a character type, but your Java code suggests it's a numeric type. Are you sure that those values have gone into the columns that you think they've gone into?
Its is VARCHAR because I have been concatenating it with multiple URLID's separated by commas e.g. 2,3,4,7,8
Is your updateDescription method throwing an SQLException then? Because you're comparing it to a numeric value in your SQL. Also, what is the value in that column, on the row that you actually want to update?
By the way, having multiple values in a single column is usually a bad idea. Is there a reason why you can't have one row per URL ID?
0
connection.commit();

or

connection.setAutoCommit(true);

should help.

Comments

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

Try the above code, as you have stated that urlid is VARCHAR in database

3 Comments

Still nothing is being inserted into the title or description columns
Print out string 'query' and try running the SQL query with the value for urlid directly on the database. Check if there is any change as your query seems fine
I printed the query and then copy and pasted it exactly how it appeared and it worked! So I can't think why it wouldn't work in the function?

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.