Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » SQL Anywhere Server - Programming » JDBC support » Data access using JDBC

 

Using prepared INSERT and DELETE statements from JDBC

A sample JDBC application is called from the database server to insert and delete rows in the Departments table using prepared statements.

Prerequisites

A Java Development Kit (JDK) must be installed.

To create an external procedure, you must have the CREATE PROCEDURE and CREATE EXTERNAL REFERENCE system privileges. You must also have SELECT, DELETE, and INSERT privileges on the database object you are modifying.

 Task
  1. Connect to the database from Interactive SQL.

  2. Ensure the JDBCExample class has been installed.

    For more information about installing the Java examples classes, see Preparing for the JDBC examples.

  3. Define a stored procedure named JDBCInsert that acts as a wrapper for the JDBCExample.Insert method in the class:

    CREATE PROCEDURE JDBCInsert(IN arg1 INTEGER, IN arg2 CHAR(50))
      EXTERNAL NAME 'JDBCExample.Insert(ILjava/lang/String;)V'
      LANGUAGE JAVA;
  4. Call the JDBCExample.Insert method as follows:

    CALL JDBCInsert( 202, 'Southeastern Sales' );

    The Insert method causes the InsertDynamic method to be invoked.

  5. Confirm that a row has been added to the Departments table.

    SELECT * FROM Departments;

    The example program displays the updated contents of the Departments table in the database server messages window.

  6. There is a similar method in the example class called DeleteDynamic that shows how to delete the row that has just been added.

    Define a stored procedure named JDBCDelete that acts as a wrapper for the JDBCExample.Delete method in the class:

    CREATE PROCEDURE JDBCDelete(IN arg1 INTEGER)
      EXTERNAL NAME 'JDBCExample.Delete(I)V'
      LANGUAGE JAVA;
  7. Call the JDBCExample.Delete method as follows:

    CALL JDBCDelete( 202 );

    The Delete method causes the DeleteDynamic method to be invoked.

  8. Confirm that the row has been deleted from the Departments table.

    SELECT * FROM Departments;

    The example program displays the updated contents of the Departments table in the database server messages window.

Results

Rows are inserted and deleted from a table using prepared SQL statements in a server-side JDBC application.

 See also