Home

JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies


Updating Data within a Database

An update is very similar to the insert above, the only difference being the SQL that is passed into the executeUpdate method of the statement object.

// Obtain a statement object
Statement stmt = cnDB.createStatement();

// Execute the block of SQL code
stmt.executeUpdate(“UPDATE tblCustomer SET Name = ‘Bob’ WHERE CUSTOMERID = 21”);

// Close the result set
stmt.close();

// Close the connection
cnDB.close();

Statement stmt = cnDB.createStatement();
Assuming that you are already using the cnDB database connection object, you can create a statement object that allows you to add a record to the database.

stmt.executeUpdate(“UPDATE tblCustomer SET Name = ‘Bob’ WHERE CUSTOMERID = 21”);
With the stmt Statement object you can leverage the executeUpdate method to accept your SQL string with the update SQL as shown above.

stmt.close();
Close statement object as soon as you finish working with that. It explicitly gives a chance for garbage collector to recollect memory as early as possible, which in turn affects performance.

cnDB.close();
Close statement object as soon as you finish working with that. It explicitly gives a chance for garbage collector to recollect memory as early as possible, which in turn affects performance.