DAT602 Database Application Development Lecture 8 Advanced JDBC.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
MC365 JDBC and Server-Side Programming: Updating a database via JDBC & Connection Pooling.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science 1620 Programming & Problem Solving.
Java Database Connectivity By: Abe Marji CS616. Agenda 1.Quick Review of Databases 2.What is SQL? 3.What is JDBC? 4.Advanced Functions of JDBC 5.Summary.
C++ fundamentals.
Database Management Systems (DBMS)
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
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.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
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 and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Dr Gordon Russell, Napier University Unit Embedded SQL - V3.0 1 Embedded SQL Unit 5.1.
Oracle Data Integrator Procedures, Advanced Workflows.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
Advanced SQL: Cursors & Stored Procedures
Sumanth M Ganesh B CPSC 620.  SQL Injection attacks allow a malicious individual to execute arbitrary SQL code on your server  The attack could involve.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
ABSTRACT The JDBC (Java Database Connectivity) API is the industry standard for database- independent connectivity between the Java programming language.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 8: Applications Programming for Relational Databases Instructor’s.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Exception Handling How to handle the runtime errors.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
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. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
JDBC.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
CS 440 Database Management Systems
JDBC Database Management Database connectivity
Web Technologies IT230 Dr Mohamed Habib.
UNIT - V STORED PROCEDURE.
The Client/Server Database Environment
Error Handling Summary of the next few pages: Error Handling Cursors.
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
PL/SQL Scripting in Oracle:
Database Application Development
Query Optimization Techniques
CS 440 Database Management Systems
Query Optimization Techniques
Presentation transcript:

DAT602 Database Application Development Lecture 8 Advanced JDBC

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

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

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

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

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

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

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

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

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

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

A simple example of stored procedure DROP PROCEDURE sp_select_min_bal GO CREATE PROCEDURE AS SELECT account_id FROM account WHERE balance GO Database Application Development - Lecture 8

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

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

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

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

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

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

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

Reference : Database.Porgramming With Jdbc And Java 2nd Edition.OReilly Chapter 4. Database Application Development - Lecture 8