1 Session 2 Module 3: Scrollable Resultset and Rowsets.

Slides:



Advertisements
Similar presentations
Advanced Java Class Java Database Connectivity (JDBC)
Advertisements

Object-Oriented Enterprise Application Development Advanced JDBC.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Database applications with JDBC Jiafan Zhou. DBMS Database management systems (DBMS) is an organised collection of data. Usually database stores data.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
What is JDBC? Java Database Connectivity (JDBC) is an API for the Java programming language that defines how a client may access a database. provides.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
DAT602 Database Application Development Lecture 9 Advanced JDBC 2.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
1 Session 3 Module 4: Java Security Module 5: Cryptography.
Session 30 Basics of JDBC. Java Simplified / Session 30 / 2 of 33 Review A Swing menu consists of a menubar, menuitems and menus. Trees are used to depict.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
JDBC By 朱志興. Four driver types of JDBC (1) JDBC-ODBC bridge plus ODBC driver: Java Database Client JDBC- ODBC bridge ODBC driver Database Server A B C.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Lesson 6: Database Programming with Java.
JDBC™ Advanced Concepts
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
Adv Java Chapter 1.JDBC Chapter 2.Servlets Chapter 3.JSP.
JDBC - Resultset The java.sql.ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
JDBC.
JDBC Java Data Base Connectivity נערך ע"י: אורי רוטנברג הנחיה: ד"ר תמר בניה קורס: סדנא ב-Java.
R EMAINING TOPICS IN JDBC Sagun Dhakhwa. O VERVIEW Introduction to JDBC API Types of JDBC Drivers Introduction to ODBC.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
Database: JDBC Overview
Chapter 38 Advanced Java Database Programming
DEPTT. OF COMP. SC & APPLICATIONS
Chapter 41 Advanced Java Database Programming
Database JDBC Overview CS Programming Languages for Web Applications
Objectives List and describe scrollable result sets
Objectives In this lesson, you will learn about:
Interacting with Database
Java Database Connectivity
Java API for Database Connectivity
Presentation transcript:

1 Session 2 Module 3: Scrollable Resultset and Rowsets

Scrollable Resultset and Rowsets / Session2 / 2 of 34 Module 1,2 - Review (1)  JDBC is a Java Database Connectivity API that is a part of the Java Enterprise API.  JDBC driver type: 4 types JDBC-ODBC bridge plus ODBC driver Native-API partly Java driver JDBC-Net pure Java driver Native-protocol pure Java driver

Scrollable Resultset and Rowsets / Session2 / 3 of 34 Module 1,2 - Review (2)  Database meta information: The structure and property of the data stored in a DB.  Store procedures: Contain SQL statements using constructors and control structures.

Scrollable Resultset and Rowsets / Session2 / 4 of 34 Module 3 - Objectives  Scrollable, updatable “resultset”  Batch updates and transactions  Rowsets  JDBCRowsets  CachedRowsets

Scrollable Resultset and Rowsets / Session2 / 5 of 34 Scrollable “ResultSet”  ResultSet object Maintains a cursor pointing before the first row.  Scrollable ResultSet  ResultSet constant values TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE  Characteristics of ResultSet are: Scrollable. Updatable. Holdable.

Scrollable Resultset and Rowsets / Session2 / 6 of 34 Methods of returning Scrollable ResultSet (1)  public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException public StatementSQLException  public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException public PreparedStatementStringSQLException  public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException public CallableStatementString SQLException

Scrollable Resultset and Rowsets / Session2 / 7 of 34 Methods of returning Scrollable ResultSet (2) Slide 7/

Scrollable Resultset and Rowsets / Session2 / 8 of 34 Methods of returning Scrollable ResultSet (3) Slide 8/

Scrollable Resultset and Rowsets / Session2 / 9 of 34 Some methods of ResultSet (1)

Scrollable Resultset and Rowsets / Session2 / 10 of 34 Some methods of ResultSet (2)  boolean relative(int rows) throws SQLException SQLException  boolean absolute(int row) throws SQLException SQLException [ absolute(1) ≈ first() & absolute(-1) ≈ last() ]

Scrollable Resultset and Rowsets / Session2 / 11 of 34 Updatable ResultSet  Concept of Updatable ResultSet rs.newUpdateXXX()  Concurrency: The constant values can be assigned for specifying the concurrency types: CONCURRENCY.READ_ONLY CONCURRENCY.UPDATABLE  Consist of 3 actions: Updating a row Inserting a row Deleting a row

Scrollable Resultset and Rowsets / Session2 / 12 of 34 Updating a row  Step 1: Positioning the Cursor Statement st = cn.createStatement(ResultSet.TYPE_SCROLL_SE NSITIVE, ResultSet.CONCUR_UPDATABLE) ResultSet rs = st.executeQuery(“SELECT NAME, EMPLOEE_ID FROM EMPLOYEES”); rs.first();  Step 2: Updating the columns rs.updateInt(2,2345); //rs.update  Step 3: Committing the update rs.updateRow();

Scrollable Resultset and Rowsets / Session2 / 13 of 34 Inserting a row  Step 1: Positioning the Cursor Statement st = cn.createStatement(ResultSet.TYPE_SCROLL_SE NSITIVE, ResultSet.CONCUR_UPDATABLE) ResultSet rs = st.executeQuery(“SELECT NAME, EMPLOEE_ID FROM EMPLOYEES”); rs.first();  Step 2: Updating the columns rs.update  Step 3: inserting a row rs.insertRow();

Scrollable Resultset and Rowsets / Session2 / 14 of 34 Deleting a row Step 1: Positioning the cursor // Move the cursor to the last row of the result set rs.last(); Step 2: Deleting the row // Deleting the row from the result set rs.deleteRow();

Scrollable Resultset and Rowsets / Session2 / 15 of 34 Batch updates  A set of multiple update statements that is submitted to the database for processing as a batch  Statement, PreparedStatement and CallableStatement can be used to submit batch updates

Scrollable Resultset and Rowsets / Session2 / 16 of 34 Implementing Batch Update using “Statement” interface

Scrollable Resultset and Rowsets / Session2 / 17 of 34 Implementing Batch Update using “PreparedStatement” Interface

Scrollable Resultset and Rowsets / Session2 / 18 of 34 Implementing Batch Update using “CallableStatement Interface”

Scrollable Resultset and Rowsets / Session2 / 19 of 34 Introduction to Transactions  Concept of Transaction: One or more statement executed together Indivisible unit of work  Properties of Transaction Atomicity Consistency Isolation Durability

Scrollable Resultset and Rowsets / Session2 / 20 of 34 Implementing Transactions using JDBC Step 1: Start the Transaction // Disable auto-commit mode cn.setAutoCommit(false); Step 2: Perform Transactions

Scrollable Resultset and Rowsets / Session2 / 21 of 34 Step 3: Use SavePoint Step 4: Close the Transaction … // End the transaction cn.rollback(svpt); OR … cn.commit();

Scrollable Resultset and Rowsets / Session2 / 22 of 34 Rowsets  RowSet is an interface in package javax.sql  It is derived from the ResultSet interface.  It typically contains a set of row from a source of tabular data like a result set.  It is a JavaBeans component, it has features: Properties JavaBeans Notification Mechanism

Scrollable Resultset and Rowsets / Session2 / 23 of 34 Benefits of using “RowSet”  The main features of using a RowSet are: Scrollability Updatability  A Rowset object, being a JavaBeans component can be used to notify other registered GUI components of a change.

Scrollable Resultset and Rowsets / Session2 / 24 of 34 Different types of RowSet

Scrollable Resultset and Rowsets / Session2 / 25 of 34 Implementation of connected “RowSet”  A connected RowSet is implemented by JDBCRowSet class.  There two ways to create a JDBCRowSet object: Using a ResultSet object Using a default constructor

Scrollable Resultset and Rowsets / Session2 / 26 of 34 Implementation of disconnected “RowSet”  A CacheRowSet object is an example of a disconnected RowSet object.  There two ways to create a CachedRowSet object: Using a default constructor Using the SyncProvider implementation

Scrollable Resultset and Rowsets / Session2 / 27 of 34 Create a CachedRowSet object: Using a default constructor CachedRowSet crs = new CachedRowSetImpl(); crs.setUserName(“scott”); crs.setPassword(“tiger”); crs.setUrl(“jdbc:protocolName:datasourceName”); crs.setCommand(“Select * From Employee”); crs.execute();

Scrollable Resultset and Rowsets / Session2 / 28 of 34 Create a CachedRowSet object: Using the SyncProvider implementation CachedRowSet crs2 = new CachedRowSetImpl(“com.myJava.providers.HighAvailabi lityProvider”); crs2.setUserName(“scott”); crs2.setPassword(“tiger”); crs2.setUrl(“jdbc:protocolName:datasourceName”); crs2.setCommand(“Select * From Employee”); crs2.execute();

Scrollable Resultset and Rowsets / Session2 / 29 of 34 Introduction to JDBCRowSet  A JDBCRowSet object is derived from ResultSet object  The uses of a JDBCRowSet object are: To make a ResultSet object scrollable and thereby make better use of legacy drivers that do not support scrolling. To use the ResultSet object as a JavaBeans component. This feature enables its use as a tool to select a JDBC driver in an application.

Scrollable Resultset and Rowsets / Session2 / 30 of 34 Using JDBCRowSet object  A row of data can be updated, inserted and deleted in a way similar to an updatable ResultSet object.  Any changes made to a JDBCRowSet object’s data are also reflected on the DB.

Scrollable Resultset and Rowsets / Session2 / 31 of 34 CachedRowset (1)  CachedRowSet stores/caches its data in memory so that it can operate on its own data rather than depending on the data stored in a DB.  Disconnected RowSet objects are serializable.This enables a disconnected RowSet to be transmitted over a network to thin clients, such as PDA’s or mobile phone.  A CachedRowSet has all the capabilities of a connected RowSet.

Scrollable Resultset and Rowsets / Session2 / 32 of 34 CachedRowset (2)  A row of data can be updated, inserted and deleted in a CachedRowSet object.  Changes in data are reflected on the DB by invoking the aceptChanges() methods.

Scrollable Resultset and Rowsets / Session2 / 33 of 34 Module 3 - Summary (1)  Scrollable result sets provide the ability to move the cursor forward and backward to a specified position or to a position relative to the current position  Updatable resultset is the ability to update rows in a result set using methods in the java programming language rather than SQL commands  A batch update is a set of multiple update statements that is submitted to the database

Scrollable Resultset and Rowsets / Session2 / 34 of 34 Module 3 - Summary (2)  Rowset is an interface that derived from the ResultSet interface  A JDBCRowset is the only implementation of a connected rowset. Any change made to a JDBCRowset object is reflected on the database directly  Cachedrowset ideal for transmitting data over the network. Any change made to a disconnected Rowset is actually made to the object memory and has to be reflected in the database using code explicitly