Contents Index Inserts, updates, and deletes using JDBC Queries using JDBC

ASA Programming Guide
  JDBC Programming
    Using JDBC to access data

Passing arguments to Java methods


We can expand the InsertFixed method to illustrate how arguments are passed to Java methods.

The following method uses arguments passed in the call to the method as the values to insert:

public static void InsertArguments(
                String id, String name)  {
try {
      conn = DriverManager.getConnection(
                    "jdbc:default:connection" );

String sqlStr =  "INSERT INTO Department "
   + " ( dept_id, dept_name )"
   + " VALUES (" +  id +  ", '"  + name + "')";

      // Execute the statement
      Statement stmt = conn.createStatement();
      Integer IRows = new Integer( 
         stmt.executeUpdate( sqlStr.toString() ) );

      // Print the number of rows updated
      System.out.println(IRows.toString() + " row inserted" );
    }
    catch ( Exception e ) {
      System.out.println("Error: " + e.getMessage());
      e.printStackTrace();
    }
  }
Notes 

To use a Java method with arguments

  1. If you have not already installed the JDBCExamples.class file into the sample database, do so.

  2. Connect to the sample database from Interactive SQL and enter the following command:

    call JDBCExamples>>InsertArguments( 
        '203', 'Northern Sales' )
  3. Verify that an additional row has been added to the Department table:

    SELECT *
    FROM Department
  4. Roll back the changes to leave the database unchanged:

    ROLLBACK

Contents Index Inserts, updates, and deletes using JDBC Queries using JDBC