Download presentation
Presentation is loading. Please wait.
Published byElwin McCarthy Modified over 9 years ago
1
DAT602 Database Application Development Lecture 9 Advanced JDBC 2
2
In this lecture, we continue to introduce some advanced features of JDBC -Batch Processing -Scrollable Result Sets -Updatable Result Sets Database Application Development - Lecture 9
3
What is Batch Processing ? Batch processing is execution of a series of operations on a program without human interaction. Why we need batch processing ? Bank application automatically add interest to each account. Database Application Development - Lecture 9
4
JDBC 2.0 introduced new functionality to address the specific issues of batch processing. Using the JDBC 2.0 batch facilities, you can assign a series of SQL statements to a JDBC Statement (or one of its subclasses) to be submitted together for execution by the database. Database Application Development - Lecture 9
5
The advantage of applying batch processing. Here we use the bank application for illustration. The bank application adds interest to each account monthly. Without using batch processing, the steps should like: 1. Prepare statement. 2. Bind parameters. 3. Execute. 4. Repeat steps 2 and 3 for each account. Database Application Development - Lecture 9
6
In previous lecture, the example that not apply batch processing requires a lot of "back and forth" between the Java application and the database. These “back and forth” cost a lot of time. JDBC 2.0 batch processing provides a simpler, more efficient approach to this kind of processing, you can find in next slide. Database Application Development - Lecture 9
7
JDBC 2.0 provides batch processing: 1. Prepare statement. 2. Bind parameters. 3. Add to batch. 4. Repeat steps 2 and 3 until interest has been assigned for each account. 5. Execute. Here, you can see that only one execute in whole operation, which means there is only one communication between application and database. Database Application Development - Lecture 9
8
Under batch processing, there is no "back and forth" between the database for each account. Instead, all Java-level processing—the binding of parameters—occurs before you send the statements to the database. Database Application Development - Lecture 9
9
Example: how to use a Statement object to batch process interest calculation: Statement stmt = conn.createStatement( ); int[] rows; for(int i=0; i<accts.length; i++) { accts[i].calculateInterest( ); stmt.addBatch("UPDATE account " + "SET balance = " + accts[i].getBalance( ) + "WHERE acct_id = " + accts[i].getID( )); } rows = stmt.executeBatch( ); Database Application Development - Lecture 9
10
Problems happen when the batch processing crash before it executes all statements. Database Application Development - Lecture 9 Statement 1 (done) Statement 2 (done) Statement 3 (error) Statement 4 (unfinished) Statement 5 (unfinished) Statement 6 (unfinished) …..
11
What will happen when error comes ? All accounts before the error will have their new balance stored in the database. The subsequent accounts will not have had their interest calculated. The account where the error occurred will have an account object whose state is inconsistent with the database. Database Application Development - Lecture 9
12
How should we to handle this issue ? The key is know where the error happened, and we continue batch processing from the fail point. Database Application Development - Lecture 9 Statement 1 (done) Statement 2 (done) Statement 3 (error) Statement 4 (unfinished) Statement 5 (unfinished) Statement 6 (unfinished) …..
13
You can use the getUpdateCounts( ) method in the BatchUpdateException thrown by executeBatch() to get the value executeBatch() should have otherwise returned. Then you can continue your batch processing from the position getUpdateCounts() + 1. Database Application Development - Lecture 9
14
Batch processing by using prepared statement The main difference is that a batch prepared or callable statement represents a single SQL statement with a list of parameter groups, and the database should create a query plan only once. Database Application Development - Lecture 9
15
Calculating interest with a prepared statement would look like this: PreparedStatement stmt = conn.prepareStatement( "UPDATE account SET balance = ? WHERE acct_id = ?"); int[] rows; for(int i=0; i<accts.length; i++) { accts[i].calculateInterest( ); stmt.setDouble(1, accts[i].getBalance( )); stmt.setLong(2, accts[i].getID( )); stmt.addBatch( ); } rows = stmt.executeBatch( ); Database Application Development - Lecture 9
16
Scrollable Result Sets What features scrollable can provide to us ? So far, if we want to loop all records of a result set, we only can loop forward. By using the scrollable result sets, we can loop both forward or backward, we can even jump to a specific position of result set. Database Application Development - Lecture 9
17
How to create a scrollable result set ? Statement stmt = Conn.createStatement( TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); First parameter of this constructor is used for indicating the scroll type, it also can be value TYPE_FORWARD_ONLY. Database Application Development - Lecture 9
18
Navigation of Scrollable Result Set While next() moves one row forward, previous() moves one row backward. If it moves back beyond the first row, it returns false. Otherwise, it returns true. If you want loop whole set backward, you should call the method afterLast() first, this method move your to the row after last record of this set. Database Application Development - Lecture 9
19
Skeleton of using previous method to loop whole set. stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery("SELECT * from test ORDER BY test_id"); rs.afterLast( ); while(rs.previous( )) { String str; a = rs.getInt("test_id"); } Database Application Development - Lecture 9
20
Move to specific position. You can call method absolute(int rowNumber) to move to specific position. Example of absolute() resultSet.absolute(2) What is more? beforeFirst(), last(), first() Database Application Development - Lecture 9
21
Determining Where You Are You can use following method to determe where you are, there methods return value true or false except method getRow(), getRow returns current row number as an integer. isFirst(), isLast(), isBeforeFirst(), isAfterLast(), and getRow( ). Database Application Development - Lecture 9
22
Updatable Result Sets So far, as we know, result is not updatable, which means you can’t modify the content of ResultSet, the content is read only. When our update is based on the stored content, we need two transaction to achieve our goal: select and update. Database Application Development - Lecture 9
23
Why we need updatable result set ? An updatable result set enables you to perform in-place changes to a result set and have them take effect using the current transaction. Database Application Development - Lecture 9
24
How to create a updatable result set ? Statement: Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSIT IVE, ResultSet.CONCUR_UPDATABLE); Prepared Statement PreparedStatement stmt = conn.prepareStatement("SELECT acct_id, balance FROM account“, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); Database Application Development - Lecture 9
25
Your updatable set must include primary key. The most important thing to remember about updatable result sets is that you must always select from a single table and include the primary key columns. If you don't, the concept of the result set being updatable is nonsensical. Database Application Development - Lecture 9
26
How to perform update in updatable result set ? while( rs.next( ) ) { long acct_id = rs.getLong(1); double balance = rs.getDouble(2); balance = balance + (balance * 0.03)/12; rs.updateDouble(2, balance); rs.updateRow( ); } Database Application Development - Lecture 9 idbalance 10135940.30 102584920.49 1033847.20 ……
27
Insert new row into updatable result set example: rs.moveToInsertRow( ); rs.updateString(1, "newuid"); rs.updateString(2, "newpass"); rs.insertRow( ); rs.moveToCurrentRow( ); Delete row You just have to call deleteRow( ). This method will delete the current row. Database Application Development - Lecture 9
28
Reference: Database Programming with JDBC and Java, Second Edition, George Reese O'Reilly & Associates, 2000 Chapter 3,4 Database Application Development - Lecture 9
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.