Home

JSP and Beyond

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


Adding Data to a Database

The statement object that was discussed in the last example is also used to insert data into the database.

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

// Execute the block of SQL code
stmt.executeUpdate(”INSERT INTO tblCustomer VALUES (’Smith’, ‘Boston’, 2006)”);

// 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(”INSERT INTO tblCustomer VALUES (’Smith’, ‘Boston’, 2006)”);
With the stmt Statement object you can leverage the executeUpdate method to accept your SQL string with the insert 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.