SQL/MX Programming Manual for Java
Sample Programs
HP NonStop SQL/MX Programming Manual for Java—523726-003
C-14
Translating the Program
Translating the Program
At an OSS prompt, enter this command to translate the SQLJ source file by running
the SQLJ translator:
java sqlj.tools.Sqlj -createMDF MultiThread1.sqlj
Running the Program
At an OSS prompt, enter this command to run the SQLJ program:
java MultiThread1
catch (SQLException se) {
System.err.println("SQL exception: " + se);
}
catch (Exception e) {
System.err.println("Exception: " + e);
}
}
//Overriding the run() method
public void run() {
try {
//Instantiating an explicit execution context object
ExecutionContext execCtx = new ExecutionContext();
#sql [execCtx] { UPDATE samdbcat.sales.parts
SET price = price * (1 +
(:percent/100))
WHERE partnum = :partNum };
System.out.println("Updated the price of " + partNum +
" by " + percent + " percent");
}
catch (SQLException se) {
System.err.println("Error updating part number " + partNum
+ ": " + se);
}
}
//Method that shows all part numbers and their prices in the
//PARTS table
static void showPrices()
throws SQLException {
PartsPricesIter1 iter;
#sql iter = {SELECT partnum, price
FROM samdbcat.sales.parts};
while (iter.next()) {
System.out.println("Part number: " + iter.partnum() + " "
+ "price: " + iter.price());
}
iter.close();
}
}
Example C-3. MultiThread1.sqlj—Threads With Explicit Execution
Contexts (page2of2)