Connection Information

To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.

Connection Type

Accessing the database – PB Docs 80 – PowerBuilder Library

Accessing the database – PB Docs 80

Accessing the database

Before you can display data in a DataWindow control, you must
get the data stored in the data source into that control. The most
common way to get the data is to access a database.

An application goes through several steps in accessing a database:

  1. Set the appropriate values
    for the transaction object.
  2. Connect to the database.
  3. Set the transaction object for the DataWindow control.
  4. Retrieve and update data.
  5. Disconnect from the database.

This
section provides instructions for setting the transaction object
for a DataWindow control and for using the DataWindow object to
retrieve and update data.

To learn more about setting values for the
transaction object, connecting to the database, and disconnecting
from the database, see:

Setting the transaction object for the DataWindow control

There are two ways to handle database connections and transactions
for the DataWindow control. You can use:

  • Internal
    transaction management
  • A separate transaction object

The two methods provide different levels of control over database
transactions.

note.gif If you are displaying a PSR file in the control You do not need to use a transaction object or make a database
connection if you are displaying a PSR file in the DataWindow control.

note.gif If you change the DataWindow object If you change the DataWindow object associated with a DataWindow
control during execution, you may need to call the SetTrans or SetTransObject
method again.

PowerBuilder You always need to call one of the methods to set the transaction
object.

Web ActiveX You need to call SetTransObject again only when you are using a
separate transaction object.

JavaBeans component If you chose the connectivity option Make
an explicit call to setTrans or setTransObject
,
you need to call the appropriate method again.

These options are described in this section.

Internal transaction management

What it does

When the DataWindow control uses internal transaction management,
it handles connecting, disconnecting, commits, and rollbacks. It automatically
performs
connects and disconnects as needed; any errors that occur cause
an automatic
rollback.

Whenever the DataWindow needs to access the database (such
as when a Retrieve or Update method is executed), the DataWindow
issues an internal CONNECT statement, does the appropriate data
access, then issues an internal DISCONNECT.

Whether to use it

When not to use it Do not use internal transaction management when:

  • Your application requires the best possible performance
    Internal transaction management is slow and uses considerable
    system resources because it must connect and disconnect for every
    database access.
  • You want control over when a transaction is committed
    or rolled back
    Because internal transaction management must disconnect after
    a database access, any changes are always committed immediately.

When to use it If the number of available connections at your site is limited, you
might want to use internal transaction management because connections are
not held open.

Internal transaction management is appropriate in simple situations
when you are doing pure retrievals (such as in reporting) and do
not need to hold database locks–when application control
over committing or rolling back transactions is not an issue.

How it works

PowerBuilder To use internal transaction management, you specify connection
values for a transaction object, which could be the automatically instantiated
SQLCA. Then you call the SetTrans method, which copies the values
from a specified transaction object to the DataWindow control’s
internal transaction object.

note.gif Connecting to the database When you use SetTrans, you do not need to explicitly code
a CONNECT or DISCONNECT statement in a script. CONNECT and DISCONNECT statements
are automatically issued when needed.

For more information about PowerBuilder transaction objects,
see PowerBuilder Application Techniques
.

Web ActiveX To use internal transaction management, set the transaction properties
for the DataWindow Web ActiveX instead of using a DataWindow Transaction
Object control. You can set the properties via Param elements or in
a script. This example sets the dbParm property and calls Retrieve
in a script:

For internal transaction management, you do not call SetTransObject.
If you change the DataWindow object during execution, the connection
information is still available and the DataWindow connects as needed.
You can change the connection information by changing the value
of the dbParm property.

JavaBeans component There are two ways to set up internal transaction management.
You can:

  • Use the custom properties dialog to
    choose Connect using property settings and
    specify database connection properties.
    This causes setTrans to be called automatically; you do not
    need to write code for it.
  • Add a transaction object to the form and set up
    its connection information; in the custom properties dialog for
    the DataWindow control, select “Explicit call to setTrans or setTransObject”;
    then call setTrans using code like this:

    If you choose this option and then change the DataWindow object
    in the control during execution, you need to call setTrans again.

Transaction management with a separate transaction
object

How it works

When you use a separate transaction object, you control the
duration of the database transaction. Your scripts explicitly connect
to and disconnect from the database. If the transaction object’s
AutoCommit property is set to False, you also program when an update
is committed or rolled back.

Typically, a script for data retrieval or update involves
these statements:

  • Connect
  • SetTransObject
  • Retrieve or Update
  • Commit or Rollback
  • Disconnect

In PowerBuilder, you use embedded SQL for connecting and committing.
For the Web ActiveX and JavaBeans component, the transaction object
has methods that perform these actions.

The transaction object also stores error messages returned
from the database in its properties. You can use the error information
to determine whether to commit or roll back database changes.

When to use it

When the DataWindow control uses a separate transaction object,
you have more control of the database processing and are responsible
for managing the database transaction.

There are several reasons to use a separate transaction object:

  • You have several DataWindow controls that connect
    to the same database and you want to make one database connection
    for all of them, saving the overhead of multiple connections
  • You want to control transaction processing
  • You require the improved performance provided by
    keeping database connections open

How it works

PowerBuilder The SetTransObject method associates a transaction object with
the DataWindow control. PowerBuilder has a default transaction object called
SQLCA that is automatically instantiated. You can set its connection properties,
connect, and assign it to the DataWindow control.

The following statement uses SetTransObject to associate the
DataWindow control dw_emp with the default transaction
object (SQLCA):

Instead of or in addition to using the predefined SQLCA transaction
object, you can define your own transaction object in a script.
This is necessary if your application needs to connect to more than
one database at the same time.

The following statement uses SetTransObject to associate dw_customer
with a programmer-created transaction object (trans_customer):

Web ActiveX To use a separate transaction object for the Web ActiveX,
you add an OBJECT element for the Sybase DataWindow Transaction
Object control to the Web page. You can set its connection properties
using Param elements or a script.

A script that connects and retrieves data would have statements
like these:

JavaBeans component To use a separate transaction object, you add a transaction
object to your form and set its connection properties. The transaction
object is on the Database tab of the Component palette.

There are two ways to use the transaction object. You choose
the one you want in the custom properties dialog for the DataWindow
control:

  • Select Use an
    existing transaction object
    and select the transaction
    object you created from the list.
    This causes setTransObject to be called automatically; you
    do not need to write code for it.
  • Select Make an explicit call
    to setTrans or setTransObject
    , then call setTransObject
    with code like this:

    If you choose this option and then change the DataWindow object
    in the control during execution, you need to call setTransObject
    again.

For more information

For more information about database transaction
processing:

  • PowerBuilder See the chapter on using transaction objects in Application
    Techniques
  • PowerJ See the chapter on connecting to databases in the PowerJ Programmer’s
    Guide

For more information about SetTrans and SetTransObject
methods, see the DataWindow Reference
.

Retrieving and updating data

You call the following two methods to access a database through
a DataWindow control:

  • Retrieve
  • Update

Basic data retrieval

After you have set the transaction object for your DataWindow
control, you can use the Retrieve method to retrieve data from the
database into that control:

For the JavaBeans component, the method name is all lowercase:

The Web DataWindow server component has a second form of the
method, RetrieveEx, for use when the method requires arguments.
For more information about retrieving data with the Web DataWindow,
see “Specifying the database
connection and retrieving data”
and “Passing page-specific data
to the reloaded page”
.

Using retrieval arguments

About retrieval arguments

Retrieval arguments qualify the SELECT statement associated
with the DataWindow object, reducing the rows retrieved according
to some criteria. For example, in the following SELECT statement,
Salary is a retrieval argument defined in the DataWindow painter:

When you call the Retrieve method, you supply a value for
Salary. In PowerBuilder, the code looks like this:

Special considerations for each environment
are explained below.

When coding Retrieve with arguments, specify them in the order
in which they are defined in the DataWindow object. Your Retrieve
method can provide more arguments than a particular DataWindow object
expects. Any extra arguments are ignored. This allows you to write
a generic Retrieve that works with several different DataWindow
objects.

Omitting retrieval arguments If your DataWindow object takes retrieval arguments but you
do not pass them in the Retrieve method, the DataWindow control
prompts the user for them when Retrieve is called.

More than 16 arguments

The Retrieve method is limited to 16 arguments in some environments.

PowerBuilder You can specify any number of retrieval arguments.

Web DataWindow You can specify a maximum of 16 arguments using the RetrieveEx
method.

Web ActiveX You can specify a maximum of 16 arguments for Retrieve. If you
need to specify more, use the RetrieveEx method for the Web ActiveX
and pass an array where each array element is a retrieval argument.

JavaBeans component You can specify a maximum of 16 arguments with the Arg class
for individual arguments. For more than 16 arguments, define an array
using the DWRetrieveArgs class, described next.

Retrieval arguments in the JavaBeans component

For the DataWindow JavaBeans component, the DWRetrieveArgs
class handles the various data types that retrieval arguments have.
Retrieval arguments can have any of these data types, as a single
value or an array:

  • Numeric short, int, double, float, long
  • String java.lang.String
  • Date and time java.sql.Date, java.sql.Time, java.sql.Timestamp

There are two techniques for specifying retrieval arguments:

  • Use the DWRetrieveArgs class to set up an array
    where each value in the array corresponds to a retrieval argument
  • Use the Arg class to set up individual arguments

Using the DWRetrieveArgs class DWRetrieveArgs is an unbounded mixed-type array of argument
values. You add enough elements to the array to handle the number
of retrieval arguments defined for your DataWindow object.

You add elements to the array using the setArgument method,
in which you specify the number of a retrieval argument and its
value.

This example sets up an array with three retrieval arguments
and then calls retrieve:

Using the Arg class The Arg class provides a simple way to use arguments when
calling the retrieve method. You can use this technique for DataWindow objects
that have fewer than 16 retrieval arguments.

This example uses Arg to set up the same three retrieval arguments
illustrated above for DWRetrieveArgs:

Updating data

After users have made changes to data in a DataWindow control,
you can use the Update method to save those changes in the database.

In PowerBuilder, the code looks like this:

Update sends to the database all inserts, changes, and deletions
made in the DataWindow control since the last Update method. When
you are using an external transaction object, you can then commit
(or roll back) those database updates. In PowerBuilder, you use
SQL statements. In the Web ActiveX and PowerJ, you use methods and
properties of the transaction object. In the Web DataWindow client
control, update requests call the update method in the server component,
which handles the commit or rollback.

For more specifics on how a DataWindow control
updates the database (that is, which SQL statements are sent in
which situations), see “Updating the database”.

Examples

The following examples show code that connects, retrieves,
updates, commits or rolls back, and disconnects from the database.
The first example is for PowerBuilder; the second is Java.

Although the example shows all database operations in a single
script or function, most applications separate these operations.
In a PowerBuilder application, for example, an application could
connect to the database in the application Open event, retrieve
and update data in one or more window scripts, and disconnect from
the database in the application Close event.

PowerBuilder The following statements retrieve and update data using the transaction
object EmpSQL and the DataWindow control dw_emp:

JavaBeans component The following Java statements retrieve and update data using
the transaction object EmpSQL and the DataWindow control dw_emp.

Here is the same example in Java:

For the Web ActiveX, the code is similar; the main difference
is that the method names begin with a capital letter.

Handling retrieval or update errors

A production application should include error tests after
each database operation.

For more about checking for errors, see “Handling DataWindow
errors”
.


Document get from Powerbuilder help
Thank you for watching.
Was this article helpful?
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x