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(); } }
The two arguments are the department id (an integer) and the department name (a string). Here, both arguments pass to the method as strings, because they are part of the SQL statement string.
The INSERT is a static statement and takes no parameters other than the SQL itself.
If you supply
the wrong number or type of arguments, you receive the Procedure
Not Found
error.
Using a Java method with arguments
If you have not already installed the JDBCExamples.class file into the sample database, do so.
Connect to the sample database from Interactive SQL, and enter the following command:
call JDBCExamples>>InsertArguments( '203', 'Northern Sales' )
Verify that an additional row has been added to the Department table:
SELECT * FROM Department
Roll back the changes to leave the database unchanged:
ROLLBACK