14
Interacting with Databases
Executing SQL Statements
There are two steps required to obtain a row of data. First, invoke the
statement
object’s
nextRow
method; this makes it possible to retrieve the row’s information. Then, invoke the
statement
object’s
getRow
method, which returns a
Row
generic object representing
the current row.
In the example shown below, the first row of information will be displayed in the console.
Note that the syntax is simplified in this case because there are no spaces in the column
names:
// Create the SQL statement:
var st = ‘Select firstName, lastName, ssn from \"Employee Info\"’;
// Execute the SQL statement:
myStatement.execute(st);
// Make the next row (the first row in this case) available:
myStatement.nextRow();
// Obtain the information contained in the first row (a Row object):
var firstRow = myStatement.getRow();
// Display the information retrieved:
console.println("First name: " + firstRow.firstName.value);
console.println("Last name: " + firstRow.lastName.value);
console.println("Social Security Number: " + firstRow.ssn.value);
If the column names contain spaces, the syntax can be modified as shown below:
// Create the SQL statement:
var st = ‘Select \"First Name\", \"Last Name\" from \"Employee Info\"’;
// Execute the SQL statement:
myStatement.execute(st);
// Make the next row (the first row in this case) available:
myStatement.nextRow();
// Obtain the information contained in the first row (a Row object):
var firstRow = myStatement.getRow();
// Display the information retrieved:
console.println("First name: " + firstRow["First Name"].value);
console.println("Last name: " + firstRow["Last Name"].value);
242
Acrobat JavaScript Scripting Guide