Database
Database
Database
The Acrobat Database Connectivity (ADBC) plug-in provides some basic JavaScript properties and methods for connecting to a database. These can be used to obtain information about the databases available on the system, the tables contained within each database and the data types used within any given table. More importantly, JavaScript can be used to execute SQL commands and retrieve data, which can, in turn, be used to populate a PDF file. Conversely, through JavaScript and SQL, field values of a PDF form can be saved to a database, data can be added or updated as needed. All of this is possible without the use of a web server or CGI script. These queries and manipulations can take place on a desktop PC/workstation. When combined with the powerful batch processing capability of Acrobat 5.0, large scale database tasks involving PDF files can be performed in a automated manner. Currently, ADBC is a Windows only feature and requires Open Database Connectivity (ODBC) provided by Microsoft. The purpose of this article is to describe the basic steps for getting started; this article is not a tutorial in SQL. A good online reference to SQL is the article Introduction to Structured Query Language by James Hoffman. Fundamentally, this paper covers the following topics. The ADBC Demo Files: A brief discussion of the ADBC demo files in this folder. Registering an ODBC Data Source Connect and Execute SQL Connect to the Data Source Executing a SQL statement Retrieving Data and Populating a Form Updating the Database
ADBCdemo.mdb: Both PDF demo files use this database. Before using the demos, you must transfer ADBCdemo.mdb to your hard drive, then register it with the ODBC Data Source Administrator. Instructions for registering the database are contained in the ADBCdemo.pdf file or in the section Registering an ODBC Data Source, below. ADBCdemo.pdf: This is a two page file. On the first page, it is demonstrated how to extract information from ODBC and list rows of data from your databases. This page also illustrates some of the new forms features, the dynamic creation (and removal) of form fields through JavaScript, for example. Complete instructions are contained on the page. The second page illustrates how to connect directly to a particular database (ADBCdemo.mdb). On this page, you can connect, page through the database, and even update the entries. FormsBatch.pdf: This demo illustrates how to populate a form (FormsBatch.pdf) and save the populated form to the hard drive all in batch mode. The batch sequence used is called Populate and Save, located in the Batch folder of the Acrobat CD. Additional information on Populate and Save can be found in the file BatchSequences.pdf; you should read the discussion on Populate and Save before trying to use the FormsBatch.pdf demo file.
This code assumes a fixed data source, "my Data". Alternatively, the script can get the data sources name from a text field of the PDF form you are trying to process; see, for example the file ADBCdemo.pdf on the Acrobat 5.0 CD. Data sources, such as a Microsoft Access file, that have been password protected can be accessed by either providing the password at connect time, through a popup dialog that appears on the screen, or, by providing the password programmatically
connect = ADBC.newConnection("my Data", "Admin", "dps017");
Alternatively, back in the ODBC Data Source Administrator, you can configure your Access file so that the password is supplied automatically. In the ODBC Microsoft Access Setup dialog, click on the Advanced button, then fill in the Login name and Password in the Default Authorization box.
statement.execute(SQL)
To retrieve data from the database, you must execute the SQL statement SELECT/FROM.
SELECT <list of columns> FROM <table>
For example,
SELECT "FirstName", "LastName" FROM "my Table"
Note, the above syntax is not presented in the most general form.
try { connect = ADBC.newConnection("my Data"); if (connect == null) throw "Could not connect"; statement = connect.newStatement(); if (statement == null) throw "Could not execute newStatement"; // statement.execute returns true if successful // Note the use of the single quotes and double quotes here, // double quotes are needed if the name has white space in it. if(statement.execute( 'Select * from "my Table"' )) throw "Could not execute the requested SQL"; } catch(e) { app.alert(e); }
If the column names contain white space, using the syntax row.FirstName.value will not work, instead use a bracket notation:
try { statement.nextRow(); var row = statement.getRow(); // get a row object this.getField("id").value = row["ID"].value; this.getField("firstname").value = row["First Name"].value; this.getField("lastname").value = row["Last Name"].value; ...... more fill in statements .......... this.getField("telephone").value = row["Telephone"].value; this.getField("income").value = row["Income"].value; } catch(e) { app.alert("No more data.");
See the Statement Object in the Acrobat JavaScript Object Specification for more details.
Final Comments
This short article just touches only on the major steps needed for connecting to a database and for retrieving information. Study the demo files in the database folder of the Acrobat CD for additional examples, techniques and ideas. Acrobat and ADBC now provides powerful tools for solving local database problems and it is hoped that developers will take these new tools and produce for the business community some useful and worthwhile applications.