7 Using DataDirect Test™ : DataDirect Test™ Tutorial

DataDirect Test™ Tutorial
This DataDirect Test tutorial explains how to use the most important features of DataDirect Test (and the JDBC API) and assumes that you can connect to a database with the standard available demo table or fine-tune the sample SQL statements shown in this example as appropriate for your environment.
NOTE: The step-by-step examples used in this tutorial do not show typical clean-up routines (for example, closing result sets and connections). These steps have been omitted to simplify the examples. Do not forget to add these steps when you use equivalent code in your applications.
Configuring DataDirect Test™
The default DataDirect Test configuration file is:
install_dir/testforjdbc/Config.txt
where install_dir is your product installation directory.
The DataDirect Test configuration file can be edited as appropriate for your environment using any text editor. All parameters are configurable, but the most commonly configured parameters are:
A list of comma-separated JDBC URLs. The first item in the list appears as the default in the database selection window. You can use one of these URLs as a template when you make a JDBC connection. The default Config.txt file contains example URLs for most databases.
Set to com.sun.jndi.fscontext.RefFSContextFactory if you are using file system data sources, or com.sun.jndi.ldap.LdapCtxFactory if you are using LDAP.
The location of the .bindings file if you are using file system data sources, or your LDAP Provider URL if you are using LDAP.
A list of comma-separated JDBC data sources. The first item in the list appears as the default in the data source selection window.
To connect using a data source, DataDirect Test needs to access a JNDI data store to persist the data source information. By default, DataDirect Test is configured to use the JNDI File System Service Provider to persist the data source. To download the JNDI File System Service Provider, go to:
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#7110-jndi-1.2.1-oth-JPR
Make sure that the fscontext.jar and providerutil.jar files from the download are on your classpath.
Starting DataDirect Test™
How you start DataDirect Test depends on your platform:
■
As a Java application on Windows—Run the testforjdbc.bat file located in the testforjdbc subdirectory of your product installation directory.
On Windows 9x and Me, double-clicking testforjdbc.bat opens a DOS window and displays the error "Out of environment space." To prevent this, use the following procedure:
a
b
Right-click testforjdbc.bat and select Properties. After the properties display, select the Memory tab.
c
On the Memory tab, locate the Initial environment setting. From this drop-down list, select 1024. Then, select the Protected check box. Click OK. A testforjdbc shortcut is created in the same directory with testforjdbc.bat.
d
NOTE: Do not delete the testforjdbc shortcut; the 1024 environment setting will be lost if the shortcut is deleted.
■
As a Java application on Linux/UNIX—Run the testforjdbc.sh shell script located in the testforjdbc subdirectory in the installation directory.
After you start DataDirect Test, the Test for JDBC Tool window appears.
Main Test for JDBC Tool window
The main Test for JDBC Tool window shows the following information:
■
■
■
TIP: DataDirect Test windows contain two Concatenate check boxes. Select a Concatenate check box to see a cumulative record of previous actions; otherwise, only the last action is shown. Selecting Concatenate can degrade performance, particularly when displaying large result sets.
Connecting Using DataDirect Test™
You can use either of the following methods to connect using DataDirect Test:
■
■
Connecting Using a Data Source
To connect using a data source, DataDirect Test needs to access a JNDI data store to persist the data source information. By default, DataDirect Test is configured to use the JNDI File System Service Provider to persist the data source. To download the JNDI File System Service Provider, go to:
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#7110-jndi-1.2.1-oth-JPR
Make sure that the fscontext.jar and providerutil.jar files from the download are on your classpath.
To connect using a data source:
1
From the main Test for JDBC Tool window menu, select Connection / Connect to DB via Data Source. The Select A Datasource window appears.
2
Select a data source from the Defined Datasources pane. In the User Name and Password fields, type values for the User and Password connection properties; then, click Connect. For information about JDBC connection properties, refer to the appropriate driver chapter in the DataDirect Connect Series for JDBC User’s Guide.
3
If the connection was successful, the Connection window appears and shows the Connection Established message in the JDBC/Database Output scroll box.
Connection window displaying a Connection Established message
Connecting Using Driver/Database Selection
1
From the main Test for JDBC Tool window menu, select Driver / Register Driver. DataDirect Test prompts for a JDBC driver name.
2
In the Please Supply a Driver URL field, specify a driver (for example com.ddtek.jdbc.sqlserver.SQLServerDriver); then, click OK.
If the driver was registered successfully, the main Test for JDBC Tool window appears with a confirmation in the JDBC/Database Output scroll box.
Main Test for JDBC Tool window with a confirmation message in the JDBC/Database Output scroll box
3
Select Connection / Connect to DB from the main Test for JDBC Tool menu. The Select A Database window prompts with a list of default connection URLs.
4
Select one of the default driver connection URLs. In the Database field, modify the default values of the connection URL appropriately for your environment.
NOTE: There are two entries for DB2—one with locationName and another with databaseName. If you are connecting to:
■
■
5
In the User Name and Password fields, type the values for the User and Password connection properties; then, click Connect. For information about JDBC connection properties, Refer to the appropriate driver chapter in the DataDirect Connect Series for JDBC User’s Guide.
6
If the connection was successful, the Connection window appears and shows the Connection Established message in the JDBC/Database Output scroll box.
Connection window displaying a Connection Established message
Executing a Simple Select Statement
This example explains how to execute a simple Select statement and return the results.
To Execute a Simple Select Statement:
1
From the Connection window menu, select Connection / Create Statement. The connection window indicates that the creation of the statement was successful.
2
Select Statement / Execute Stmt Query. DataDirect Test displays a dialog box that prompts for a SQL statement.
Execute SQL Query dialog box with an example Select statement specified
3
Type a Select statement and click Submit. Then, click Close.
4
Select Results / Show All Results. The data from your result set displays in the JDBC/Database Output scroll box.
Connection window with an example of result set data displayed
5
Executing a Prepared Statement
This example explains how to execute a parameterized statement multiple times.
To Execute a Prepared Statement:
1
From the Connection window menu, select Connection / Create Prepared Statement. DataDirect Test prompts you for a SQL statement.
2
Type an Insert statement and click Submit. Then, click Close.
Get Prepared Statement SQL dialog box with an example Insert statement displayed
3
Select Statement / Set Prepared Parameters. To set the value and type for each parameter:
a
b
c
d
Click Set to pass this information to the JDBC driver.
The Set Prepared Statement Parameters window showing the parameter numbers, types, and values.
4
5
Select Statement / Execute Stmt Update. The JDBC/Database Output scroll box indicates that one row has been inserted.
Connection window showing one row has been inserted
6
7
If you repeat the steps described in “Executing a Simple Select Statement”, you will see that the previously inserted records are also returned.
Connection window showing that the previously inserted records are also returned
Retrieving Database Metadata
1
From the Connection window menu, select Connection / Get DB Meta Data.
2
Select MetaData / Show Meta Data. Information about the JDBC driver and the database to which you are connected is returned.
Connection window showing that information about the JDBC driver and the database to which you are connected is returned
3
Metadata also allows you to query the database catalog (enumerate the tables in the database, for example). In this example, we will query all tables owned by the user SCOTT.
4
Select MetaData / Tables.
5
Get Tables dialog box with a value of SCOTT in the Schema Pattern field
6
Click Ok. The Connection window indicates that getTables() succeeded.
7
Select Results / Show All Results. All tables owned by SCOTT are returned.
Connection window listing all of the tables owned by SCOTT
Scrolling Through a Result Set
1
From the Connection window menu, select Connection / Create JDBC 2.0 Statement. DataDirect Test prompts for a result set type and concurrency.
2
a
b
Get JDBC 2.0 Statement Properties dialog box with specified values
c
Click Submit; then, click Close.
3
Select Statement / Execute Stmt Query.
4
Type a Select statement and click Submit. Then, click Close.
Execute SQL Query dialog box with an example Select statement
5
Select Results / Scroll Results. The Scroll Result Set window indicates that the cursor is positioned before the first row.
Scroll Result Set dialog box indicating that the cursor is positioned before the first row
6
Click the Absolute, Relative, Before, First, Prev, Next, Last, and After buttons as appropriate to navigate through the result set. After each action, the Scroll Result Set window displays the data at the current position of the cursor.
Scroll Result Set dialog box displaying the data at the current position of the cursor
7
Click Close.
Batch Execution on a Prepared Statement
Batch execution on a prepared statement allows you to update or insert multiple records simultaneously. In some cases, this can significantly improve system performance because fewer round trips to the database are required.
To Execute a Batch on a Prepared Statement:
1
From the Connection window menu, select Connection / Create Prepared Statement.
Type an Insert statement and click Submit. Then, click Close.
Get Prepared Statement SQL dialog box with an example Insert statement
2
Select Statement / Add Stmt Batch.
3
a
b
c
d
Click Set.
The Set Batch Parameters window showing parameter numbers, types, and values.
4
Click Add to add the specified set of parameters to the batch. To add multiple parameter sets to the batch, repeat Step 2 through Step 4 as many times as necessary. When you are finished adding parameter sets to the batch, click Close.
5
Select Statement / Execute Stmt Batch. DataDirect Test displays the rowcount for each of the elements in the batch.
Connection window displaying the rowcount for each of the elements in the batch
6
If you re-execute the Select statement from “Executing a Simple Select Statement”, you see that the previously inserted records are returned.
.Connection window showing that the previously inserted records are returned
Returning ParameterMetaData
NOTE: Returning ParameterMetaData requires a J2SE 1.4 or higher JVM.
1
From the Connection window menu, select Connection / Create Prepared Statement.
Type the prepared statement and click Submit. Then, click Close.
Get Prepared Statement SQL dialog box with an example Insert statement
2
Select Statement / Get ParameterMetaData. The Connection window displays ParameterMetaData.
Connection window showing ParameterMetaData
Establishing Savepoints
NOTE: Savepoints require a J2SE 1.4 or higher JVM.
1
From the Connection window menu, select Connection / Connection Properties.
2
Select TRANSACTION_COMMITTED from the Transaction Isolation drop-down list. Do not select the Auto Commit check box.
Connection Properties dialog box
3
Click Set; then, click Close.
4
From the Connection window menu, select Connection / Load and Go. The Get Load And Go SQL window appears.
5
Get Load And Go SQL window
6
Select Connection / Set Savepoint.
7
Set Savepoints window
8
Click Apply; then, click Close. The Connection window indicates whether or not the savepoint succeeded.
Connection window indicating that the savepoint succeeded
9
Get Load And Go SQL window
10
Select Connection / Rollback Savepoint. In the Rollback Savepoints window, specify the savepoint name.
Rollback Savepoints window with the savepoint name specified
11
Click Apply; then, click Close. The Connection window indicates whether or not the savepoint rollback succeeded.
Connection window indicating that the savepoint rollback succeeded
12
Get Load And Go SQL window
Click Submit; then, click Close. The Connection window displays data that was inserted before the first Savepoint. The second insert was rolled back.
Connection window displaying data that was inserted before the first savepoint
Updatable Result Sets
The following examples explain the concept of updatable result sets by deleting, inserting, and updating a row.
Deleting a Row
1
From the Connection window menu, select Connection / Create JDBC 2.0 Statement.
2
a
b
Get JDBC 2.0 Statement Properties dialog box
3
Click Submit; then, click Close.
4
Select Statement / Execute Stmt Query.
5
Specify the Select statement and click Submit. Then, click Close.
Execute SQL Query dialog box with an example Select statement
6
Select Results / Inspect Results. The Inspect Result Set window appears.
Inspect Result Set window
7
Click Next. Current Row changes to 1.
8
Click Delete Row.
9
To verify the result, return to the Connection menu and select Connection / Load and Go. The Get Load And Go SQL window appears.
10
Get Load And Go SQL window
11
Connection window displaying one row returned
Inserting a Row
1
From the Connection window menu, select Connection / Create JDBC 2.0 Statement.
2
a
b
Get JDBC 2.0 Statement Properties dialog box
3
Click Submit; then, click Close.
4
Select Statement / Execute Stmt Query.
5
Execute SQL Query window with an example Select statement
6
Select Results / Inspect Results. The Inspect Result Set window appears.
Inspect Result Set window
7
Click Move to insert row; Current Row is now Insert row.
8
9
10
11
Click Insert Row.
12
To verify the result, return to the Connection menu and select Connection / Load and Go. The Get Load And Go SQL window appears.
13
Get Load And Go SQL window
14
Connection window displaying two rows returned
NOTE: The ID will be 3 for the row you just inserted because it is an auto increment column.
Updating a Row
1
From the Connection window menu, select Connection / Create JDBC 2.0 Statement.
2
a
b
Get JDBC 2.0 Statement Properties dialog box
3
Click Submit; then, click Close.
4
Select Statement / Execute Stmt Query.
5
Execute SQL Query dialog box with an example Select statement
6
Click Submit; then, click Close.
7
Select Results / Inspect Results. The Inspect Result Set window appears.
Inspect Result Set window
8
Click Next. Current Row changes to 1.
9
In Set Cell Value, type RALEIGH. Then, click Set Cell.
10
Click Update Row.
11
To verify the result, return to the Connection menu and select Connection / Load and Go. The Get Load And Go SQL window appears.
12
Get Load And Go SQL window
13
Click Submit; then, click Close.
14
Connection window displaying a successfully updated row
Large Object (LOB) Support
NOTE: LOB support (Blobs and Clobs) requires a J2SE 1.4 or higher JVM.
The following example uses Clob data; however, this procedure also applies to Blob data. This example illustrates only one of multiple ways in which LOB data can be processed.
1
From the Connection window menu, select Connection / Create Statement.
2
Select Statement / Execute Stmt Query.
3
Execute SQL Query window with an example Select statement
4
Click Submit; then, click Close.
5
Select Results / Inspect Results. The Inspect Result Set window appears.
6
Click Next. Current Row changes to 1.
Inspect Result Set window
7
Deselect Auto Traverse. This disables automatic traversal to the next row.
8
Click Get Cell. Values are returned in the Get Cell Value field.
Inspect Result Set window
9
Inspect Result Set window
10
Click Get Cell. The Clob data window appears.
Clob Data dialog box
11
Click Get Cell. Values are returned in the Cell Value field.
Clob Data dialog box