JDBC IV IS 313 4.24.2003. Outline  Quiz 1  Stored procedures  Batch processing  Transactions  Homework #2.

Slides:



Advertisements
Similar presentations
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
Advertisements

ASP.NET Best Practices Dawit Wubshet Park University.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
Basic JDBC Use Oracle JDBC Drivers must be in the CLASSPATH
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
CSE 305 Theory of Database Tutorial on Connecting with Sybase from Java program and Developing GUI Jalal Mahmud, TA, CSE 305.
Databases: Queries with Java Dr Andy Evans. JDBC SQL Three methods: Statements: Standard, simple, SQL. PreparedStatements: Compiled SQL statements that.
 2004 Keith Vander Linden. All Rights Reserved. Calvin CollegeDepartment of Computer Science(1/25) Database Programming with JDBC Keith Vander Linden.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
CS 405G: Introduction to Database Systems Database programming.
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.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
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.
Domain Driven Web Development With WebJinn Sergei Kojarski College of Computer & Information Science Northeastern University joint work with David H. Lorenz.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
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.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
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.
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 8: Applications Programming for Relational Databases Instructor’s.
SQL and Java The vision for Java is to be the concrete and nails that people use to build this incredible network system that is happening all around us.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
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. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
JDBC.
JDBC Java Data Base Connectivity נערך ע"י: אורי רוטנברג הנחיה: ד"ר תמר בניה קורס: סדנא ב-Java.
1. Writing a Java program to connect to SQL Server 2008 and Create a table Populate the table (insert data) Perform queries to retrieve information from.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JDBC IV IS Outline  Batch processing  Transactions  Homework #2  Examples.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
Transactions. Contents ● Transactions ● Save Points ● Batch Updates.
CS3220 Web and Internet Programming Database Access with JDBC
1st Semester, 2017 Sanghyun Park
ATS Application Programming: Java Programming
JDBC III IS
Web Technologies IT230 Dr Mohamed Habib.
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
JDBC(Java Database Connectivity
The PROCESS of Queries John Deardurff Website: ThatAwesomeTrainer.com
Bolat Azamat, Kim Dongmin
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

JDBC IV IS

Outline  Quiz 1  Stored procedures  Batch processing  Transactions  Homework #2

Quiz 1  Ave: 9.25  Mode: 10  Min: 3  Max: 15

Stored Procedures

Distributed Application

Where to put computation?  All client use database just to store copy to client to compute  All server use client just to display use database for computation

Client-Server Trade-off Data smallData large Server available Either OKServer Server busy ClientIt depends

Stored Procedures  Database-resident code  Serve many purposes computations too complex for SQL triggers move computation to the data

Steps 1. Write stored procedure 2. Create a callable statement 3. Set the parameters 4. Call the procedure 5. Possibly get return values

Java Stored Procedure (Oracle) public static void updateAvailableRoomData (int hotelId, String roomType, int mumOfRooms, String updateFlag) { Connection connection = null; // Database Connection Object try { connection = new OracleDriver().defaultConnection(); String addBookedSeats = " + "; String subTotalSeats = " - "; if (updateFlag.equals("COUT")) { addBookedSeats = " - "; subTotalSeats = " + "; } if (roomType.equals("ORCL")) roomType = "OTHR"; PreparedStatement pstmt = connection.prepareStatement("UPDATE ROOM_AVAILABILITY " + " SET BOOKED_" + roomType + " = BOOKED_" + roomType + addBookedSeats + mumOfRooms + ", TOTAL_" + roomType + " = TOTAL_" + roomType + subTotalSeats + mumOfRooms + " WHERE HOT_ID = ? AND BOOKING_DATE = " + " ( SELECT MAX(BOOKING_DATE) FROM ROOM_AVAILABILITY " + " WHERE HOT_ID = ? )" ); pstmt.setInt(1,hotelId); // Bind the Hotel ID input parameter pstmt.setInt(2,hotelId); // Bind the Hotel Id input parameter int noRecordsUpdated = pstmt.executeUpdate(); // Execute the Statement … et cetera …

From client CallableStatement stmt = con.prepareCall (“Call Hotel_HotelBookingsSample_updateAvailableRoomData (?,?,?,?)”); stmt.setInt (1, 5); stmt.setString (2, “Single”); stmt.setInt (3, 1); stmt.setString (4, “COUT”); stmt.execute();

Return values? stmt.registerOutParameter (3, Types.INTEGER); stmt.execute(); int result = Stmt.getInt(3);

Summary – Stored Procedures  Embed computation in database using database-specific code  benefits move computation to data  drawbacks SP code not portable maintenance problematic  still frequently used for performance benefits

JDBC Classes

Batch Updating

Limit Data Movement  Cost of moving data between client and server  One solution Stored procedure: move computation to data  Another Try to reduce the cost by reducing overhead Communication costs are “chunky”

Non-linear Communication Costs

Example  Suppose 12 small operations  Execution time = 12 * (op_time + comm_in + comm_out)  If grouped together = 12 * op_time + comm_in + comm_out

Steps 1. Turn off auto commit 2. For each update, call addBatch instead of executeUpdate 3. when complete, call executeBatch

Batch processing con.setAutoCommit(false); … JDBC calls … Statement stmt = con.prepareStatement (“INSERT...;”);... set parameters... // ready to update stmt.addBatch(); // instead of executeUpdate()... more stuff... stmt2.addBatch ();... more stuff... stmt3.addBatch (); // updates complete con.executeBatch(); con.setAutoCommit(true);

Summary - Batch Processing  Useful when there are many updates  Benefits Lower communication overhead to database  Problems Each statement treated separately  No chance to handle exceptions that affect later updates

Transactions  Goal In the face of machine failure, keep the database consistent

Transactions  Atomicity No partial success  Consistency End point is consistent  Isolation Transaction invisible until completed  Durability Committed actions survive system failure

Example  Reservation is added  Actions Record inserted into Reservation table Capacity table updated with new reservation count

Steps  1. Set auto commit to false  2. perform updates as normal  3. Commit transaction  4. Set auto commit back to true  If there is a failure, rollback the transaction typically exception handler

Example try { conn.setAutoCommit (false); addReservation (conn); updateCapacity (conn); conn.commit (); } catch (SQLException e) { try { conn.rollback (); DBUtilities.displaySQLException (e); } catch (SQLException e2) { DBUtilities.displaySQLException (e2); } } finally { try { conn.setAutoCommit (true); } catch (SQLException e3) { DBUtilities.displaySQLException (e3); } }

Homework #2

 Frequent Flyer Program  Database  Interactive text mode

Interaction % java -cp "." FFTextMode Setting up connection to jdbc:odbc:hwk2 FF: level Enter benefit level: 1 IdNameSinceLevel 5Rubble, Barney02/01/011 9Mouse, Minnie02/03/851 10Spratt, Jack11/11/991 FF: add-flight Enter flight id: 2032 Enter flight no: 359 Enter date (mm/dd/yy): 3/4/03 Enter origin: ORD Enter destination: PHX Enter miles: 1500 Enter member ids: (-1 to halt) 2 Enter member ids: (-1 to halt) 8 Enter member ids: (-1 to halt) -1 FF: exit

Commands  add-flight  find-by-level  help  exit  flights  delete-flight  find by name  members

How to design?

Adaptability  What are the most likely vectors of change?

Command Pattern  Represent users action with an object  Framework in which command objects are created and executed

Central Loop  Loop Create command object Set parameters Execute

Command Line Interaction % java FFTextMode delete-flight 37 %

Command Hierarchy

Advice  Start right away  Don’t use JDK 1.3  Close statements and result sets  Start right away

Programming example  Exit command  ShowFlights command