Download - JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2
![Page 1: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/1.jpg)
JDBC IV
IS 3134.24.2003
![Page 2: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/2.jpg)
Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2
![Page 3: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/3.jpg)
Quiz 1 Ave: 9.25 Mode: 10 Min: 3 Max: 15
![Page 4: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/4.jpg)
Stored Procedures
![Page 5: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/5.jpg)
Distributed Application
Clientprogram
Database
JDBC Driver
![Page 6: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/6.jpg)
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
![Page 7: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/7.jpg)
Client-Server Trade-off
Data small Data large
Server available
Either OK Server
Server busy
Client It depends
![Page 8: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/8.jpg)
Stored Procedures Database-resident code Serve many purposes
computations too complex for SQL triggers move computation to the data
![Page 9: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/9.jpg)
Steps1. Write stored procedure2. Create a callable statement3. Set the parameters4. Call the procedure5. Possibly get return values
![Page 10: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/10.jpg)
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 …
![Page 11: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/11.jpg)
From clientCallableStatement 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();
![Page 12: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/12.jpg)
Return values?
stmt.registerOutParameter (3, Types.INTEGER);
stmt.execute();
int result = Stmt.getInt(3);
![Page 13: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/13.jpg)
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
![Page 14: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/14.jpg)
JDBC Classes
DriverManagerConnection
Statement PreparedStatement CallableStatement
ResultSet Data Types
«Creates»getConnection
«Creates»createStatement
«Creates»prepareStatement
«Creates»prepareCall
«Creates»executeQuery
«Creates»executeQuery
«Creates»getXXX
«Modifies»setXXX
«Modifies»setXXX
«Creates»getXXX
![Page 15: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/15.jpg)
Batch Updating
![Page 16: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/16.jpg)
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”
![Page 17: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/17.jpg)
Non-linear Communication Costs
Com
mun
icat
ion
time
Data quantity
![Page 18: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/18.jpg)
Example Suppose 12 small operations Execution time
= 12 * (op_time + comm_in + comm_out) If grouped together
= 12 * op_time + comm_in + comm_out
![Page 19: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/19.jpg)
Steps1. Turn off auto commit2. For each update, call addBatch instead of
executeUpdate3. when complete, call executeBatch
![Page 20: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/20.jpg)
Batch processingcon.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);
![Page 21: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/21.jpg)
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
![Page 22: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/22.jpg)
Transactions Goal
In the face of machine failure, keep the database consistent
![Page 23: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/23.jpg)
Transactions Atomicity
No partial success Consistency
End point is consistent Isolation
Transaction invisible until completed Durability
Committed actions survive system failure
![Page 24: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/24.jpg)
Example Reservation is added Actions
Record inserted into Reservation table Capacity table updated with new reservation
count
![Page 25: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/25.jpg)
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
![Page 26: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/26.jpg)
Exampletry
{ 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); }
}
![Page 27: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/27.jpg)
Homework #2
![Page 28: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/28.jpg)
Homework #2 Frequent Flyer Program Database Interactive text mode
![Page 29: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/29.jpg)
Interaction% java -cp "." FFTextModeSetting up connection to jdbc:odbc:hwk2FF: levelEnter benefit level: 1Id Name Since Level5 Rubble, Barney 02/01/01 19 Mouse, Minnie 02/03/85 110 Spratt, Jack 11/11/99 1FF: add-flightEnter flight id: 2032Enter flight no: 359Enter date (mm/dd/yy): 3/4/03Enter origin: ORDEnter destination: PHXEnter miles: 1500Enter member ids: (-1 to halt) 2Enter member ids: (-1 to halt) 8Enter member ids: (-1 to halt) -1FF: exit
![Page 30: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/30.jpg)
Commands add-flight find-by-level help exit flights delete-flight find by name members
![Page 31: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/31.jpg)
How to design?
![Page 32: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/32.jpg)
Adaptability What are the most likely vectors of
change?
![Page 33: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/33.jpg)
Command Pattern Represent users action with an object Framework in which command objects are
created and executed
![Page 34: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/34.jpg)
Central Loop Loop
Create command object Set parameters Execute
![Page 35: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/35.jpg)
Command Line Interaction% java FFTextMode delete-flight 37
%
![Page 36: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/36.jpg)
Command Hierarchy+setInitialized()+isInitialized() : boolean+setParameter(in key : String, in value : String)+getParameter(in key : String) : String+setParameters()+setParameters(in parameters : List)+execute()#executeCommand()+newInstance() : Command+getName() : String+getDescription() : String+getParameterNames() : String [ ]+getParameterPrompts() : String [ ]
-m_initialized : boolean = false
Command
+executeCommand()+executeDatabaseCommand()
-s_connection : Connection
DatabaseCommand
+getParameterNames() : String [ ]+getParameterPrompts() : String [ ]+isInitialized() : boolean
SimpleCommand
UnknownCommand ExitCommand HelpCommand
HashMap1
1
m_params
AddMemberCommand
AddFlightCommand
ShowFlightsCommand
FindByLevelCommand
![Page 37: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/37.jpg)
Advice Start right away Don’t use JDK 1.3 Close statements and result sets Start right away
![Page 38: JDBC IV IS 313 4.24.2003. Outline Quiz 1 Stored procedures Batch processing Transactions Homework #2](https://reader035.vdocument.in/reader035/viewer/2022062322/5697bf751a28abf838c7ffdf/html5/thumbnails/38.jpg)
Programming example Exit command ShowFlights command