copyright oracle corporation, 1998. all rights reserved. 4 accessing a database using jbcl

37
Copyright Oracle Corporation, 1998. All rights reserved. 4 4 Accessing a Database Using JBCL

Upload: audra-walton

Post on 02-Jan-2016

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.

44

Accessing a Database Using JBCL

Accessing a Database Using JBCL

Page 2: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-22

ObjectivesObjectives

After completing this lesson, you should be able to do the following:

• Describe the key JBCL data components

• Specify properties for JBCL data component

• Build a simple form to query and update a database

After completing this lesson, you should be able to do the following:

• Describe the key JBCL data components

• Specify properties for JBCL data component

• Build a simple form to query and update a database

Page 3: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-33

OverviewOverview

JBCL includes many components that simplify database connectivityJBCL includes many components that simplify database connectivity

JOB

CLERK

SALESMAN

MANAGER

MANAGER

ENAME

ADAMS

ALLEN

BLAKE

CLARK

SAL

1100

1600

2850

2540

Page 4: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-44

Getting Started: The Database Component

Getting Started: The Database Component

Add a Database component to your applet

• Manages the database connection

Add a Database component to your applet

• Manages the database connection

Page 5: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-55

Configuring the Database Properties

Configuring the Database Properties

The Database properties govern the way in which the database connection is made

• Connection details

• Transaction isolation level

• Use schema and table name?

• Use space padding andstatement caching?

The Database properties govern the way in which the database connection is made

• Connection details

• Transaction isolation level

• Use schema and table name?

• Use space padding andstatement caching?

Page 6: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-66

Specifying connection Details:JDBC Driver

Specifying connection Details:JDBC Driver

connection is a complex property, and is set using the Connection Editor

Driver tab specifies the JDBC Driver to use:

connection is a complex property, and is set using the Connection Editor

Driver tab specifies the JDBC Driver to use:

Page 7: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-77

Specifying connection Details:Database Details

Specifying connection Details:Database Details

In the Database tab, specify details for the database

• Details required here depend on which driver you have selected

In the Database tab, specify details for the database

• Details required here depend on which driver you have selected

Page 8: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-88

Testing the Database Connection

Testing the Database Connection

In the Test Connection tab, test that the database connection can be established

• Tests databaseURL and availability of the JDBC driver

In the Test Connection tab, test that the database connection can be established

• Tests databaseURL and availability of the JDBC driver

Page 9: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-99

Code Generated by the Connection Editor

Code Generated by the Connection Editor

Here is an example of the code generated by the Connection EditorHere is an example of the code generated by the Connection Editorpublic class Applet1 extends Applet {

public void jbInit()… {

database1.setConnection(

new borland.sql.dataset.ConnectionDescriptor(

"jdbc:oracle:thin:@HOSTID:1521:ORCL",

"scott", "tiger", false,

"oracle.jdbc.driver.OracleDriver"));

}

Page 10: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1010

Performing a Query: The QueryDataSet Component

Performing a Query: The QueryDataSet Component

Add a QueryDataSet component to your applet

• Specifies the SQL query string

Add a QueryDataSet component to your applet

• Specifies the SQL query string

Page 11: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1111

Configuring the QueryDataSet Properties

Configuring the QueryDataSet Properties

The QueryDataSet properties define the SQL query to be executed

• SQL query string

• Sort criteria

• Should errors be displayed?

• How is modified data written to the database?

The QueryDataSet properties define the SQL query to be executed

• SQL query string

• Sort criteria

• Should errors be displayed?

• How is modified data written to the database?

Page 12: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1212

Configuring a QueryDataSet:Specifying the query Property Configuring a QueryDataSet:Specifying the query Property

Specify the Database component, and the SQL query stringSpecify the Database component, and the SQL query string

1

2

3

Page 13: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1313

Testing the QueryTesting the Query

Click the Test Query button to verify that the SQL string against the DatabaseClick the Test Query button to verify that the SQL string against the Database

Displays the outcome hereDisplays the outcome here

Page 14: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1414

Code Generated by the Query Editor

Code Generated by the Query Editor

Example:Example:

public void jbInit()… {

queryDataSet1.setQuery(

new borland.sql.dataset.QueryDescriptor(

database1, // Database instance

"select ENAME, SAL from EMP where SAL < 2500",

null, // No parameterRow ref

true, // Execute immediately?

false)); // Asynchronous exec?

}

Page 15: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1515

Guided Practice: Specifying Complex SQL Strings

Guided Practice: Specifying Complex SQL Strings

Given the following EMP table:

•EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO

and the following DEPT table:

•DEPTNO, DNAME, LOC

define a SQL query that gets the employee name, department name, and salary for all clerks, in order of salary

Given the following EMP table:

•EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO

and the following DEPT table:

•DEPTNO, DNAME, LOC

define a SQL query that gets the employee name, department name, and salary for all clerks, in order of salary

Page 16: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1616

Using QueryDataSet ResultsUsing QueryDataSet Results

•QueryDataSet executes a SQL string, and caches the data set locally

• Results can be displayed using data-aware controls

• Data-aware controls appear in Controls tab, with gray grid background

•QueryDataSet executes a SQL string, and caches the data set locally

• Results can be displayed using data-aware controls

• Data-aware controls appear in Controls tab, with gray grid background

Page 17: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1717

Example: Displaying a Data Set in a GridControl

Example: Displaying a Data Set in a GridControl

• Add a GridControl to the applet

• Specify its dataSet property

• Add a GridControl to the applet

• Specify its dataSet property

Page 18: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1818

What Happens at Run Time?What Happens at Run Time?

• The Database connection is opened

• The QueryDataSet query is executed, and results are cached locally

• Results are displayed in bound data controls

• The Database connection is opened

• The QueryDataSet query is executed, and results are cached locally

• Results are displayed in bound data controls

Page 19: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-1919

Modifying the Display Format Modifying the Display Format

Each of the columns in the QueryDataSet has its own propertiesEach of the columns in the QueryDataSet has its own properties

Page 20: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2020

Display and Edit MasksDisplay and Edit Masks

Each column can have display and edit masks Each column can have display and edit masks

Page 21: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2121

Data ValidationData Validation

A validation routine can bedefined for each columnA validation routine can bedefined for each column

void column2_validate(DataSet ds,

Column col,

Variant var) throws Exception {

BigDecimal min = new BigDecimal(200);

BigDecimal sal = var.getAsBigDecimal();

if (sal.compareTo(min) < 0)

throw new VetoException("Salary too small");

}

Page 22: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2222

Viewing the Customized Columns at Run Time

Viewing the Customized Columns at Run Time

The appearance of the GridControl after modifying the column properties:The appearance of the GridControl after modifying the column properties:

Page 23: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2323

Using Other Data-Aware Controls

Using Other Data-Aware Controls

• Other data-aware controls can be added

• Specify the dataSet and columnName properties for each control

• Other data-aware controls can be added

• Specify the dataSet and columnName properties for each control

Page 24: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2424

Data-Aware Controls at Run Time

Data-Aware Controls at Run Time

The data-aware controls display data for the “current row”

• Synchronized with the GridControl

• Edits are reflected in all controls

The data-aware controls display data for the “current row”

• Synchronized with the GridControl

• Edits are reflected in all controls

Page 25: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2525

Navigating a Data Set Using a NavigatorControl

Navigating a Data Set Using a NavigatorControl

Helps you to scroll, save, delete, and modify the data setHelps you to scroll, save, delete, and modify the data set

NavigatorControl

Page 26: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2626

Scrolling with NavigatorControl

Scrolling with NavigatorControl

NavigatorControl includes buttons to scroll through the data set

• First, Prior, Next, Last

NavigatorControl includes buttons to scroll through the data set

• First, Prior, Next, Last

Page 27: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2727

Modifying Rows with NavigatorControlModifying Rows with NavigatorControl

NavigatorControl includes buttons to modify rows

• Insert, Delete, Post, Cancel, Ditto

NavigatorControl includes buttons to modify rows

• Insert, Delete, Post, Cancel, Ditto

Page 28: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2828

Saving and Refreshing Data with NavigatorControl

Saving and Refreshing Data with NavigatorControl

NavigatorControl includes buttons to save data, and to refresh the QueryDataSet with the latest data

• Save, Refresh

NavigatorControl includes buttons to save data, and to refresh the QueryDataSet with the latest data

• Save, Refresh

Page 29: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-2929

Navigating Rows ProgrammaticallyNavigating Rows Programmatically

void btnFirst_actionPerformed(ActionEvent e) {

try {

queryDataSet1.first();

}

catch (DataSetException dse) { }

}

Page 30: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3030

Guided Practice:Navigating Programmatically

Guided Practice:Navigating Programmatically

NavigatorControl provides the following additional buttons:

• How can the same effect be achieved programmatically?

NavigatorControl provides the following additional buttons:

• How can the same effect be achieved programmatically?

Page 31: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3131

Locating Rows in a Data Set Using a LocatorControl

Locating Rows in a Data Set Using a LocatorControl

LocatorControl

Page 32: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3232

LocatorControl at Run TimeLocatorControl at Run Time

LocatorControl provides an incremental search on string columnsLocatorControl provides an incremental search on string columns

Page 33: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3333

Customizing Database “Save”Behavior

Customizing Database “Save”Behavior

A QueryResolver can be used to fine-tune the way data is saved to a databaseA QueryResolver can be used to fine-tune the way data is saved to a database

QueryResolver

Page 34: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3434

void queryResolver1_deletingRow(

ReadWriteRow readWriteRow,

ResolverResponse resolverResponse)…{

BigDecimal hi = new BigDecimal(6000);

BigDecimal sal = readWriteRow.getBigDecimal("SAL");

if (sal.compareTo(hi) >= 0)

resolverResponse.skip();

}

Useful QueryResolver EventsUseful QueryResolver Events

QueryResolver triggers eventsfor you to customize behaviorQueryResolver triggers eventsfor you to customize behavior

deletingRow

Page 35: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3535

Associating a QueryDataSet with a QueryResolver

Associating a QueryDataSet with a QueryResolver

The QueryDataSet can now use the QueryResolver to resolve its save operations

The QueryDataSet can now use the QueryResolver to resolve its save operations

Page 36: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3636

SummarySummary

The JBCL provides many components to simplify database connectivity

• Database component

•QueryDataSet component

• Data-aware controls, such as GridControl

•NavigatorControl and LocatorControl components

The JBCL provides many components to simplify database connectivity

• Database component

•QueryDataSet component

• Data-aware controls, such as GridControl

•NavigatorControl and LocatorControl components

Page 37: Copyright  Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL

Copyright Oracle Corporation, 1998. All rights reserved.4-4-3737

Practice 4-1 OverviewPractice 4-1 Overview

• Use the JBCL data components to connect to a database, and perform a query

• Display the result set using data-aware controls

• Customize the controls using edit and display masks

• Navigate the result set using the Navigator and Locator controls

• Use the JBCL data components to connect to a database, and perform a query

• Display the result set using data-aware controls

• Customize the controls using edit and display masks

• Navigate the result set using the Navigator and Locator controls