230 DB2 Deployment Guide
Step 3:
To get hold of the exit code, we have to wait for the process to end. If we do
not call proc.waitFor() the script is still executed, but we will not be able to
check the exit code.
Creating database layout and database objects from Java
From a Java point of view, there is no difference in executing an SQL statement
or a DDL statement. This means that creating the database layout and database
objects are plain execution of statements against DB2. First of all, you require a
connection to the database. From the connection, you obtain a Java Statement
object, which is used to execute our SQL and DDL statements.
The set of DDL statements is typically read from a file. In our example we read
the itsodb.ddl file, where each command is separated by the default delimiter,
semicolon. This makes it very easy to parse the file and retrieve the DDL
statements.
Example 5-11 shows how to execute a list of DDL statements within a Java
application. How these are loaded are not shown, as it is not important for the
example. In the example we execute all the statements in one transaction, that
is, in one unit of work.
Example 5-11 Executing DDL statements from a Java application
// Step 1
Connection con = getConnection();
Statement stmt = con.createStatement();
String currentStatement = null;
try {
// Step 2
for ( int ix = 0; ix < statements.size(); ix++) {
currentStatement = statements.get(ix);
stmt.execute(currentStatement);
}
// Step 3
con.commit();
}
catch (SQLException e) {
// Step 4
con.rollback();
System.out.println( “Error executing : " + currentStatement);
}
Note: Output from our script becomes input to the process object. This is
why the output is retrieved as getInputStream().