Table of Contents Previous Next


5 Executing SQL Commands with executeUpdate() : 5.1 Using executeUpdate() to INSERT Data

Listing 1.3 demonstrates using the executeUpdate() method to add a row to the emp table.
NOTE: the following example is not a complete application, only a method - the samples in the remainder of this document do not include the code required to set up and tear down a Connection. To experiment with the example, you must provide a class that invokes the sample code.
The updateEmployee() method expects a single argument from the caller, a Connection object that must be connected to an Advanced Server database.:
Next, updateEmployee() prompts the user for an employee name and number:
updateEmployee() concatenates the values returned by console.readline() into an INSERT statement and pass the result to the executeUpdate() method.
For example, if the user enters an employee number of 6000 and a name of Jones, the INSERT statement passed to executeUpdate() will look like this:
The executeUpdate() method returns the number of rows affected by the SQL statement (an INSERT typically affects one row, but an UPDATE or DELETE statement can affect more). If executeUpdate() returns without throwing an error, the call to System.out.println displays a message to the user that shows the number of rows affected.
You can use executeUpdate() with any SQL command that does not return a result set. Some simple syntax examples using executeUpdate() with SQL commands follow:
To use the UPDATE command with executeUpdate() to update a row:
To use the DELETE command with executeUpdate()to remove a row from a table:
To use the DROP TABLE command with executeUpdate() to delete a table from a database:
To use the CREATE TABLE command with executeUpdate() to add a new table to a database:
stmt.executeUpdate("CREATE TABLE tablename (fieldname NUMBER(4,2),
fieldname2 VARCHAR2(30))");
To use the ALTER TABLE command with executeUpdate() to change the attributes of a table:
stmt.executeUpdate("ALTER TABLE tablename ADD COLUMN colname BOOLEAN");

5 Executing SQL Commands with executeUpdate() : 5.1 Using executeUpdate() to INSERT Data

Table of Contents Previous Next