Home

JSP and Beyond

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


Deleting Data from a Database

Just as in the above example, the Statement object stmt, will be used to execute SQL by way of the executeUpdate method.

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

// Execute the block of SQL code
stmt.executeUpdate(“DELETE FROM tblCustomer 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 will allow you to add a record to the database.

stmt.executeUpdate(“DELETE FROM tblCustomer WHERE CUSTOMERID = 21”);
With the stmt Statement object you can leverage the executeUpdate method to accept your SQL string with the delete 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 to garbage collector to recollect memory as early as possible, which in turn affects performance.

JDBC is an essential component for application development. From the information above you should now have a good grasp on the fundamentals, allowing you to perform CRUD within your own applications. For more information, an excellent amount of detail about JDBC can be found at
http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html