JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result.

Slides:



Advertisements
Similar presentations
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 PostgreSQL JDBC interface documentation
Advertisements

Advanced Java Class Java Database Connectivity (JDBC)
Object-Oriented Enterprise Application Development Advanced JDBC.
1 JDBC Java Database Connectivity. 2 c.pdf
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 12: JDBC.
Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
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.
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.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
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.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
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.
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.
1 Session 2 Module 3: Scrollable Resultset and Rowsets.
JDBC IV IS Outline  Quiz 1  Stored procedures  Batch processing  Transactions  Homework #2.
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.
JDBC™ Advanced Concepts
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
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.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC.
JDBC Java Data Base Connectivity נערך ע"י: אורי רוטנברג הנחיה: ד"ר תמר בניה קורס: סדנא ב-Java.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
JDBC – Java DataBase Connectivity
Database: JDBC Overview
Chapter 38 Advanced Java Database Programming
JDBC – Java Database Connectivity
JDBC III IS
Web Technologies IT230 Dr Mohamed Habib.
Chapter 41 Advanced Java Database Programming
Database JDBC Overview CS Programming Languages for Web Applications
Objectives List and describe scrollable result sets
Database Management Systems
JDBC – Java DataBase Connectivity
Objectives In this lesson, you will learn about:
JDBC – Java Database Connectivity
Database Programming Using JDBC and MySQL C API
Java Database Connectivity
JDBC – Java DataBase Connectivity
Corporate Training Center
Presentation transcript:

JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result Sets 5.Mapping SQL Data Types to Java 6.Batch Processing & SQL MetaData 7.Lab

JDBC Session 2 Interface PreparedStatement Extends Statement - More flexible - More efficient See PreparedSQL.java for use in a SELECT query For more info: PreparedStatementPreparedStatement Prepared Statements

JDBC Session 2 Operation successful: Commit the transaction Operation failed: Roll back the transaction Java transaction processing: Method setAutoCommit Method commit Method rollback See InsertSQL.java for use in an INSERT query Transaction Processing

JDBC Session 2 Interface CallableStatement Extends Statement Used to invoke a Stored Procedure Stored Procedures Store SQL statements in a database Advantages –Speed of execution –Security –Encapsulation –Reuse Callable Statements & Stored Procedures (p.1)

JDBC Session 2 Stored Procedures Oracle – written in PL/SQL SQL Server – written in T-SQL DB2 - ??? Access – possible using “Data Access Objects” (DAO) but beyond the scope of this class Callable Statements & Stored Procedures (p.2)

JDBC Session 2 Oracle Example Callable Statements & Stored Procedures (p.3) CREATE OR REPLACE PROCEDURE insert_employee (p_eid IN CHAR,p_fname IN VARCHAR2,p_lname IN VARCHAR2,p_age IN NUMBER,p_entrydate OUT DATE ) IS BEGIN p_entrydate := SYSDATE; INSERT INTO employee (eid, fname, lname, age, entrydate) VALUES (p_eid, p_fname, p_lname, p_age, p_entrydate); END insert_employee;... CallableStatement stmt = con.prepareCall( "{call insert_employee(?, ?, ?, ?, ?)}"); stmt.setString(1, " "); stmt.setString(2, "John"); stmt.setString(3, "Smith"); stmt.setInt(4, 30); stmt.registerOutParameter(5, Types.DATE); stmt.execute(); System.out.println("entry date = " + stmt.getDate(5));...

JDBC Session 2 Scrollable & Updatable Result Sets (p.1) From the API: ResultSet Field Summary static int TYPE_FORWARD_ONLY(default) The constant indicating the type for a ResultSet object whose cursor may move only forward. static int TYPE_SCROLL_INSENSITIVE The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes made by others. static int TYPE_SCROLL_SENSITIVE The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes made by others. static int CONCUR_READ_ONLY(default) The constant indicating the concurrency mode for a ResultSet object that may NOT be updated. static int CONCUR_UPDATABLE The constant indicating the concurrency mode for a ResultSet object that may be updated. Use Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2"); // rs will be scrollable, will not show changes made by others, // and will be updatable

JDBC Session 2 Scrollable & Updatable Result Sets (p.2) Updatable Result Sets Using updateXXX methods The updateXXX methods may be used in two ways: 1.to update a column value in the current row. In a scrollable ResultSet object, the cursor can be moved backwards and forwards, to an absolute position, or to a position relative to the current row. The following code fragment updates the NAME column in the fifth row of the ResultSet object rs and then uses the method updateRow to update the data source table from which rs was derived. rs.absolute(5);// moves the cursor to the fifth row of rs rs.updateString("NAME", "AINSWORTH"); // updates the NAME column of row 5 to be AINSWORTH rs.updateRow(); // updates the row in the data source 2.to insert column values into the insert row. An updatable ResultSet object has a special row associated with it that serves as a staging area for building a row to be inserted. The following code fragment moves the cursor to the insert row, builds a three-column row, and inserts it into rs and into the data source table using the method insertRow. rs.moveToInsertRow(); // moves cursor to the insert row rs.updateString(1, "AINSWORTH"); // updates the first column of the insert row to be AINSWORTH rs.updateInt(2,35); // updates the second column to be 35 rs.updateBoolean(3, true); // updates the third row to true rs.insertRow(); rs.moveToCurrentRow(); Scrollable Result Sets See MurachBooks for use

JDBC Session 2 Mapping SQL Data Types to Java (p.1) Java Data Type  JDBC Data Type  Data Base Data Type

JDBC Session 2 Mapping SQL Data Types to Java (p.2) Microsoft Access Data Types

JDBC Session 2 Mapping SQL Data Types to Java (p.3) Conversions by ResultSet.getXXX "x" means that the method can retrieve the JDBC type "X" means that the method is recommended for retrieving the JDBC type See: Mapping SQL - Java Types Mapping SQL - Java Types

JDBC Session 2 Batch Processing & SQL MetaData Batch Processing Allows multiple update statements (INSERT, UPDATE, DELETE) in a single data base request See Movies/ CreateMovieTables.java SQL Metadata See Movies/ QueryMovieTables.java