JDBC CS Delete/Insert with executeUpdate

How to use SQL Delete or SQL Insert with CallableStatement object
executeUpdate() method?

We will first do an SQL delete and an SQL Insert with NO DB-table key control.

With a DB connection, you must write the SQL statement
and let this statement
be the parameter to the below specified prepareCall() method.

To delete a row in the DB-table, the SQL statement must do a call to a
database stored procedure that do a SQL Delete into a database table

To insert a row in the DB-table, the SQL statement must do a call to a
database stored procedure that do a SQL Insert into a database table

If any parametric placeholder exists in your
SQL statement which can result in an output
from the database-stored procedure or function
you want to call, then it is time to
set the type for these using the CallableStatement
registerOutPutParameter() methods.

If you have any parametric placeholders for
values in your SQL statement, then it is time to
set these using the CallableStatement
setXXX() methods.

With the returned CallableStatement object, you can execute the following
method to perform the SQL Update statement:

Method in CallableStatement interface

Description

int executeUpdate()

Executes the SQL procedure call statement in this
CallableStatement object. The database procedure must do an SQL Data Manipulation
Language (DML) statement, such as INSERT, UPDATE or DELETE;
or an SQL statement that returns nothing, such as a DDL statement.

The CallableStatement object will now contain
requested database data, which can be retrieved with the
getXXX() methods.

Example of using this executeUpdate() and a CallableStatement object
to Delete and Insert rows in a database table:

How to do SQL Insert with DB-table key control using the executeUpdate() method?

With a DB connection, you must write the SQL statement
and let this statement
be the parameter to the below specified prepareCall() method.

To insert a row in the DB-table, the SQL statement must do a call to a
database stored procedure of function that do a SQL Insert into a database table
and returns a DB-table key value.

If you have any parametric placeholders for
values in your SQL statement, then it is time to
set these using the CallableStatement
setXXX() methods.

If any parametric placeholder exists in your
SQL statement which can result in an output
from the database-stored procedure or function
you want to call, then it is time to
set the type for these using the CallableStatement
registerOutPutParameter() methods.

With the returned CallableStatement object, you can execute the following
method to perform the SQL Update statement:

Method in CallableStatement interface

Description

int executeUpdate()

Executes the SQL procedure call statement in this
CallableStatement object. The database procedure must do an SQL Data Manipulation
Language (DML) statement, such as INSERT, UPDATE or DELETE;
or an SQL statement that returns nothing, such as a DDL statement.

The CallableStatement object will now contain
requested database data, which can be retrieved with the
getXXX() methods.

Example of using this executeUpdate() method and a CallableStatement object
to Insert rows in a database table with TB-table key control:

You should use the close() method in the Statement object when you do not need it anymore.

You should also use the close() method in the Connection object when you do not need it anymore.

You can download this example here(needed tools can be found in the right menu on this page).

If we run this application the result should be:

As pure output

Translated by a Browser

<table border='1' ><tr><th colspan='5'>
The new car rental ID is:
108<br/> which is for the car:
</th></tr><tr><th>regNo</th><th>cartype</th><th>model</th><th>day<br/>Price</th><th>days</th></tr><tr><td>DE34562</td><td>OPEL</td><td>2001</td><td>340.0</td><td>10.0</td></tr></table>