Reece Kenney
Reece Kenney

Reputation: 2964

Mysql statement in java not executing

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

Upvotes: 0

Views: 81

Answers (3)

user2881767
user2881767

Reputation: 300

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

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

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.

Upvotes: 1

cadrian
cadrian

Reputation: 7376

connection.commit();

or

connection.setAutoCommit(true);

should help.

Upvotes: 0

Related Questions