SQL/MX Programming Manual for Java
Sample Programs
HP NonStop SQL/MX Programming Manual for Java—523726-003
C-9
SampleDML.sqlj—Inserting, Deleting, Updating, and
Selecting Data
SampleDML.sqlj—Inserting, Deleting,
Updating, and Selecting Data
The SampleDML program performs INSERT, DELETE, UPDATE, and SELECT
operations. This SQLJ program uses tables in the SQL/MX sample database. To install
the sample database, see the SQL/MX Quick Start.
SQLJ Source File
Example C-2. SampleDML.sqlj—Inserting, Deleting, Updating, and Selecting
Data (page 1 of 3)
import java.sql.*;
import sqlj.runtime.*;
// Declare a ConnectionContext class named DefConCtx
#sql context DefConCtx;
public class SampleDML {
private DefConCtx ctx = null; // Declares and initializes
// a connection context
/* The following constructor instantiates a connection context
* object named ctx and establishes a default database
* connection. The try-catch block reports errors connecting
* to the database. */
public SampleDML() {
try {
ctx = DefConCtx.getDefaultContext();
#sql [ctx] { MODULE samdbcat.persnl.dmlmod };
}
catch (Exception exception) {
System.err.println ("Error connecting to the database: "
+ exception);
}
}
// main() method
public static void main (String [] args) {
SampleDML s1 = new SampleDML();
try {
s1.doDML(); // Invokes a method that executes
// DML statements
s1.doIterator(); // Invokes a method that executes
// an SQLJ iterator
s1.ctx.close(); // Closes the connection for the
// ctx connection context
}