SQL/MX Programming Manual for Java
Sample Programs
HP NonStop SQL/MX Programming Manual for Java—523726-003
C-10
SQLJ Source File
// Throw an SQL exception if an error occurs
// during execution
catch (SQLException e) {
System.err.println ("Exception: " + e);
}
} // End of main() method
/* The doDML() method executes simple DML operations:
* INSERT, DELETE, UPDATE, and SELECT statements */
void doDML() throws SQLException {
// Declare host variables
int empNum = 996;
String firstName = new String("Gary");
String lastName = new String("Ragbrai");
int deptNum = 4000;
int jobCode = 450;
java.math.BigDecimal salary = new java.math.BigDecimal("60000.00");
java.math.BigDecimal total;
/* Set the default catalog and schema for the database
* objects of this method. */
#sql [ctx] {DECLARE CATALOG 'SAMDBCAT'};
#sql [ctx] {DECLARE SCHEMA 'PERSNL'};
// Insert a row of data from host variables
#sql [ctx] {INSERT INTO employee
VALUES (:empNum, :firstName, :lastName,
:deptNum, :jobCode, :salary)};
// Insert a row of data by using literal values
#sql [ctx] {INSERT INTO employee
VALUES (999, 'Ravish', 'Smith',
4000, 450, 65000)};
// Delete a row in the table
#sql [ctx] {DELETE FROM employee
WHERE empnum = 996};
// Delete a row in the table
#sql [ctx] {DELETE FROM employee
WHERE empnum = 999};
// Update the salary column for all sales reps
#sql [ctx] {UPDATE employee
SET salary = salary + 5000.00
WHERE jobcode = 300};
// Select and then print the sum of the salary column
#sql [ctx] {SELECT SUM(salary)
INTO :total
FROM employee};
System.out.println("Sum of the salaries is " + total);
// Total should be 2838725.57 for the first time.
} // End of doDML() method
Example C-2. SampleDML.sqlj—Inserting, Deleting, Updating, and Selecting
Data (page 2 of 3)