Download presentation
Presentation is loading. Please wait.
Published byAmos May Modified over 8 years ago
1
DAT602 Database Application Development Lecture 8 Advanced JDBC
2
Advanced JDBC programming supports advanced needs. These advanced needs break down into two categories: optimizations and extended functionality. This lecture dives into all of the extended functionality included in the JDBC Core API. Prepared SQL and Stored Procedure Database Application Development - Lecture 8
3
Prepared SQL -Each SQL statement you sent to the database required the database to treat the statement as a brand-new query and thus build a new query plan for it. -If you are going to execute some statement repeatedly, creating new query plan wastes process power. Database Application Development - Lecture 8
4
Prepared SQL -For example, your banking application: UPDATE ACCOUNT SET BALANCE = XXX WHERE ACCOUNT_ID = YYY -Databases enable you to optimize repeated calls through prepared SQL. It can help you to save the process power. Database Application Development - Lecture 8
5
Databases provide two kinds of prepared SQL: prepared statements and stored procedures. Prepared SQL provides an advantage over the simple SQL; a database can get the SQL ahead of time and create a query plan while you are doing other application logic. Database Application Development - Lecture 8
6
Prepared Statements It enables a SQL statement to contain parameters like a function definition, and you can execute a single statement repeatedly with different values for those parameters. updating a group of objects stored on the same table Database Application Development - Lecture 8
7
Example of without using prepared statement: Statement statement = c.createStatement( ); int i; for(i=0; i<accounts.length; i++) statement.executeUpdate("UPDATE account " + "SET balance = " + accounts[i].getBalance( ) +"WHERE id = " + accounts[i].getId( )); c.commit( ); statement.close( ); This statement creates the same query plan each time through the loop. Database Application Development - Lecture 8
8
Example of using prepared statement: PreparedStatement ps= c.prepareStatement( "UPDATE account " + "SET balance = ? " + "WHERE id = ?"); int i; for(i=0; i<accounts.length; i++) { ps.setFloat(1, accounts[i].getBalance( )); ps.setInt(2, accounts[i].getId( )); ps.execute( ); ps.clearParameters( ) } c.commit( ); statement.close( ); Database Application Development - Lecture 8
9
Stored Procedures While prepared statements let you access similar database queries through a single PreparedStatement object, stored procedures attempt to take the "black box" concept for database access one step further. Database Application Development - Lecture 8
10
How stored procedure works? A stored procedure is built inside the database before you run your application. You access that stored procedure by name at runtime. In other words, a stored procedure is almost like a method you call in the database. Database Application Development - Lecture 8
11
Advantages of stored procedure - Because the procedure is precompiled in the database for most database engines, it executes much faster than dynamic SQL. - Syntax errors in the stored procedure can be caught at compile time rather than at runtime. - Java developers need to know only the name of the procedure and its inputs and outputs. Database Application Development - Lecture 8
12
A simple example of stored procedure DROP PROCEDURE sp_select_min_bal GO CREATE PROCEDURE sp_select_min_bal @balance, AS SELECT account_id FROM account WHERE balance > @balance GO Database Application Development - Lecture 8
13
What more stored procedure can do? CREATE OR REPLACE PROCEDURE sp_interest (id IN INTEGER, bal IN OUT FLOAT) IS BEGIN SELECT balance INTO bal FROM account WHERE account_id = id; bal := bal + bal * 0.03; UPDATE account SET balance = bal WHERE account_id = id; END; Database Application Development - Lecture 8
14
How to call the stored procedure in Java? try { CallableStatement statement; int i; statement = c.prepareCall("{call sp_interest[(?,?)]}"); statement.registerOutParameter(2, java.sql.Types.FLOAT); for(i=1; i<accounts.length; i++) { statement.setInt(1, accounts[i].getId( )); statement.execute( ); System.out.println("New balance: " + statement.getFloat(2)); } c.commit( ); statement.close( ); c.close( ); } Database Application Development - Lecture 8
15
What Kind of Statement to Use? So far, we know there are three Statement, PreparedStatement, and CallableStatement. How do you determine which kind is best for you? Database Application Development - Lecture 8
16
Avoid to use plain SQL statement The plain SQL statements represented by the Statement class are almost never a good idea. Their only place is in quick and dirty coding. Plain SQL statements are also more error prone (no automatic handling of data formatting, for example) and do not read as cleanly as prepared SQL. Database Application Development - Lecture 8
17
Prepared statements VS. stored procedures First: which is faster ? There is no guarantee that you will see better performance in stored procedures. prepared statement is very unlikely to be faster than its stored procedure counterpart and that the stored procedure counterpart is likely to be moderately faster than the prepared statement Database Application Development - Lecture 8
18
Prepared statements VS. stored procedures Usability Stored procedures are truer to the black-box concept than prepared statements. The JDBC programmer needs to know only stored procedure inputs and outputs. programmer needs to know the underlying table structure in addition to the inputs and outputs for prepared SQL. Database Application Development - Lecture 8
19
Prepared statements VS. stored procedures Stored procedures enable you to perform complex logic inside the database. However, you should never have any processing logic in the database. This feature should, therefore, be avoided by three-tier developers. Database Application Development - Lecture 8
20
Reference : Database.Porgramming With Jdbc And Java 2nd Edition.OReilly Chapter 4. Database Application Development - Lecture 8
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.