copyright oracle corporation, 1998. all rights reserved. 4 accessing a database using jbcl
TRANSCRIPT
Copyright Oracle Corporation, 1998. All rights reserved.
44
Accessing a Database Using JBCL
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
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
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
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?
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:
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
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
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"));
}
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
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?
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
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
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?
}
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
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
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
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
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
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
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");
}
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:
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
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
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
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
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
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
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) { }
}
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?
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
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
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
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
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
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
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