Download presentation
Presentation is loading. Please wait.
Published byAudra Walton Modified over 9 years ago
1
Copyright Oracle Corporation, 1998. All rights reserved. 4 Accessing a Database Using JBCL
2
Copyright Oracle Corporation, 1998. All rights reserved. 4-2 Objectives 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
3
Copyright Oracle Corporation, 1998. All rights reserved. 4-3 Overview JBCL includes many components that simplify database connectivity JOB CLERK SALESMAN MANAGER ENAME ADAMS ALLEN BLAKE CLARK SAL 1100 1600 2850 2540
4
Copyright Oracle Corporation, 1998. All rights reserved. 4-4 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
5
Copyright Oracle Corporation, 1998. All rights reserved. 4-5 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 and statement 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 and statement caching?
6
Copyright Oracle Corporation, 1998. All rights reserved. 4-6 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:
7
Copyright Oracle Corporation, 1998. All rights reserved. 4-7 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
8
Copyright Oracle Corporation, 1998. All rights reserved. 4-8 Testing the Database Connection In the Test Connection tab, test that the database connection can be established Tests database URL and availability of the JDBC driver In the Test Connection tab, test that the database connection can be established Tests database URL and availability of the JDBC driver
9
Copyright Oracle Corporation, 1998. All rights reserved. 4-9 Code Generated by the Connection Editor Here is an example of the code generated by the Connection Editor public 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")); }
10
Copyright Oracle Corporation, 1998. All rights reserved. 4-10 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
11
Copyright Oracle Corporation, 1998. All rights reserved. 4-11 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?
12
Copyright Oracle Corporation, 1998. All rights reserved. 4-12 Configuring a QueryDataSet : Specifying the query Property Specify the Database component, and the SQL query string 1 2 3
13
Copyright Oracle Corporation, 1998. All rights reserved. 4-13 Testing the Query Click the Test Query button to verify that the SQL string against the Database Displays the outcome here
14
Copyright Oracle Corporation, 1998. All rights reserved. 4-14 Code Generated by the Query Editor 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? }
15
Copyright Oracle Corporation, 1998. All rights reserved. 4-15 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
16
Copyright Oracle Corporation, 1998. All rights reserved. 4-16 Using 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
17
Copyright Oracle Corporation, 1998. All rights reserved. 4-17 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
18
Copyright Oracle Corporation, 1998. All rights reserved. 4-18 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
19
Copyright Oracle Corporation, 1998. All rights reserved. 4-19 Modifying the Display Format Each of the columns in the QueryDataSet has its own properties
20
Copyright Oracle Corporation, 1998. All rights reserved. 4-20 Display and Edit Masks Each column can have display and edit masks
21
Copyright Oracle Corporation, 1998. All rights reserved. 4-21 Data Validation A validation routine can be defined 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"); }
22
Copyright Oracle Corporation, 1998. All rights reserved. 4-22 Viewing the Customized Columns at Run Time The appearance of the GridControl after modifying the column properties:
23
Copyright Oracle Corporation, 1998. All rights reserved. 4-23 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
24
Copyright Oracle Corporation, 1998. All rights reserved. 4-24 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
25
Copyright Oracle Corporation, 1998. All rights reserved. 4-25 Navigating a Data Set Using a NavigatorControl Helps you to scroll, save, delete, and modify the data set NavigatorControl
26
Copyright Oracle Corporation, 1998. All rights reserved. 4-26 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
27
Copyright Oracle Corporation, 1998. All rights reserved. 4-27 Modifying 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
28
Copyright Oracle Corporation, 1998. All rights reserved. 4-28 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
29
Copyright Oracle Corporation, 1998. All rights reserved. 4-29 Navigating Rows Programmatically void btnFirst_actionPerformed(ActionEvent e) { try { queryDataSet1.first(); } catch (DataSetException dse) { } }
30
Copyright Oracle Corporation, 1998. All rights reserved. 4-30 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?
31
Copyright Oracle Corporation, 1998. All rights reserved. 4-31 Locating Rows in a Data Set Using a LocatorControl LocatorControl
32
Copyright Oracle Corporation, 1998. All rights reserved. 4-32 LocatorControl at Run Time LocatorControl provides an incremental search on string columns
33
Copyright Oracle Corporation, 1998. All rights reserved. 4-33 Customizing Database “Save” Behavior A QueryResolver can be used to fine- tune the way data is saved to a database QueryResolver
34
Copyright Oracle Corporation, 1998. All rights reserved. 4-34 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 Events QueryResolver triggers events for you to customize behavior deletingRow
35
Copyright Oracle Corporation, 1998. All rights reserved. 4-35 Associating a QueryDataSet with a QueryResolver The QueryDataSet can now use the QueryResolver to resolve its save operations
36
Copyright Oracle Corporation, 1998. All rights reserved. 4-36 Summary 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
37
Copyright Oracle Corporation, 1998. All rights reserved. 4-37 Practice 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
38
Copyright Oracle Corporation, 1998. All rights reserved. 4-38 Full Notes Page for Practices
39
Copyright Oracle Corporation, 1998. All rights reserved. 4-39 Full Notes Page for Practices
40
Copyright Oracle Corporation, 1998. All rights reserved. 4-40 Full Notes Page for Practices
41
Copyright Oracle Corporation, 1998. All rights reserved. 4-41 Full Notes Page for Practices
42
Copyright Oracle Corporation, 1998. All rights reserved. 4-42
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.