Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6.

Slides:



Advertisements
Similar presentations
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Advertisements

CSC056-Z1 – Database Management Systems – Vinnie Costa – Hofstra University1 Database Management Systems Session 6 Instructor: Vinnie Costa
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 250 Database Systems I SQL in a Server Environment.
Objective: To access a database from an application program (as opposed to interactive interfaces) Why? An interactive interface is convenient but not.
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.
Distributed Application Development B. Ramamurthy.
Murali Mani SQL with other Programming Languages.
1 Database Application Development Chapter 6. 2 Overview  SQL in application code  Embedded SQL  Cursors  Dynamic SQL  JDBC  Stored procedures.
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
1 Database Application Development Chapter 6. 2 Overview  SQL in application code  Embedded SQL  Cursors  Dynamic SQL  JDBC  Stored procedures.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
Database Application Development R&G Chapter 6 Lecture 16.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
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.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Database Programming in Java Corresponds with Chapter 32, 33.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Announcements Read JDBC Project Step 5, due Monday.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 1 DATADABASE PROGRAMMING 2Chapter 13 from our text.
Chapter 8 Using SQL in an Application. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 CSC 4480 outline Intro (3-9) Static SQL (10-11)
CS 405G: Introduction to Database Systems Database programming.
Database Application Development SQL In Application Code : 1. Embedded SQL 2. Database API’s 3. SQLJ.
ICS 321 Fall 2009 ODBC, JDBC & Stored Procedures Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/8/20091Lipyeow.
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.
CSCD34 - Data Management Systems – A. Vaisman1 Database Application Development.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
ICS 321 Fall 2009 DBMS Application Programming Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/06/20091Lipyeow.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
1 Database Application Development Chapter 6 Sections 6.1 –
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.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Chapter 6 MORE SQL: Assertions, Views, and Programming Techniques Copyright © 2004 Pearson Education, Inc.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections –
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections 6.1 –
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
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 Database Application Development Chapter 6 Sections –
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC Session 14.
Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
Database Application Development Database Programming Embedded SQL Dynamic SQL Embedded SQL in Java Database APIs: Alternative to embedding Embedded SQL.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
1 Database Application Development Programming With Databases.
1 Database Application Development Chapter 6. 2 Overview  SQL in application code  Embedded SQL  Cursors  Dynamic SQL  JDBC.
COP Introduction to Database Structures
JDBC Database Management Database connectivity
Database Application Development
Interacting with Database
Database Application Development
Java API for Database Connectivity
Database Application Development
Database Application Development
Presentation transcript:

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper2 Outline How do applications connect to a database?  App  DBMS: Embedded SQL  App  Driver  DBMS: JDBC/ODBC or SQLJ  App  Server  Driver  DBMS: JDBC/ODBC  Note: architecture generally transparent to app. What mechanisms exist to retrieve/modify data?  Retrieval/Modification  Static Queries: Embedded SQL, SQLJ  Dynamic Queries: JDBC/ODBC, Dynamic SQL  Extending Dynamic Queries: Stored Procedures  Result Storage  Cursors: a mechanism for handling sets in languages which do not support these as fundamental types.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper3 SQL in Application Code  SQL commands can be called from within a host language (e.g., C++ or Java ) program.  SQL statements can refer to host variables (including special variables used to return status).  Must include a statement to connect to the right database.  Two main integration approaches:  Embed SQL in the host language (Embedded SQL, SQLJ) – Static Method (precompiler-based)  API to call SQL commands (JDBC/ODBC) – Dynamic Method (runtime control)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper4 Embedded SQL  Approach: Embed SQL in the host language.  A preprocessor converts the SQL statements into API calls.  Then a regular compiler is used to compile the code.  Language constructs:  Connecting to a database: EXEC SQL CONNECT  Declaring variables: EXEC SQL BEGIN (END) DECLARE SECTION  Statements: EXEC SQL Statement;  Two special “error” variables:  SQLCODE (long, is negative if an error has occurred)  SQLSTATE (char[6], predefined codes for common errors)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper5 Embedding SQL in C: An Example char SQLSTATE[6]; EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; short c_minrating; float c_age; EXEC SQL END DECLARE SECTION c_minrating = random(); EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname, S.age FROM Sailors S WHERE S.rating > :c_minrating; do { EXEC SQL FETCH sinfo INTO :c_sname, :c_age; printf(“%s is %d years old\n”, c_sname, c_age); } while (SQLSTATE != ‘02000’); EXEC SQL CLOSE sinfo;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper6 Dynamic SQL  SQL query strings are not always known at compile time (e.g., spreadsheet, graphical DBMS frontend): Allow construction of SQL statements on-the-fly  Example: char c_sqlstring[]= {“DELETE FROM Sailors WHERE rating>5”}; EXEC SQL PREPARE readytogo FROM :c_sqlstring; EXEC SQL EXECUTE readytogo;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper7 Database APIs: Alternative to embedding Rather than modify compiler (or require preprocessor), add library with database calls (API)  Special standardized interface: procedures/objects  Pass SQL strings from language, presents result sets in a language-friendly way (using cursor-like objects)  Sun’s JDBC: Java API  DBMS-neutral  a “driver” interprets the calls and translates them into DBMS-specific code  database can be across a network

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper8 JDBC: Architecture  Four architectural components:  Application (initiates and terminates connections, submits SQL statements)  Driver manager (load JDBC driver)  Driver (connects to data source, transmits requests and returns/translates results and error codes)  Data source (processes SQL statements)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper9 JDBC Architecture (Contd.) Four types of drivers: Bridge:  Translates JDBC SQL cmds into non-native, non-Java API. Example: JDBC-ODBC bridge. Code for ODBC and JDBC driver needs to be available on each client. Direct translation to native API, non-Java driver:  Translates JDBC SQL commands to native API of data source. Need OS-specific binary on each client. Network bridge:  Send cmds over network to middleware server that talks to data source. Needs only small JDBC driver at each client. Direction translation to native API via Java driver:  Converts JDBC calls directly to network protocol used by DBMS. Needs DBMS-specific Java driver at each client.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper10 JDBC Classes and Interfaces Steps to submit a database query:  Load the JDBC driver  Connect to the data source  Execute SQL statements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper11 JDBC Driver Management  All drivers are managed by the DriverManager class  Loading a JDBC driver:  In the Java code: // locate the driver Class.forName("org.gjt.mm.mysql.Driver");  Configure JBuilder to locate driver when requested: To add the driver as a required library, within JBuilder select menu item: project; project properties; paths; required libraries; add; new; add (fill in a library name, e.g., "JDBC Driver", and location, e.g., project, and browse for and choose the ".jar" file you downloaded); Click OK.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper12 Connections in JDBC We interact with a data source through sessions. Each connection identifies a logical session.  JDBC URL: jdbc: : Example: Connection con; try{ // create the connection con = DriverManager.getConnection("jdbc:mysql://james.cedarville.edu/test", "cs3610", ""); } catch SQLException excpt { …}

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper13 Matching Java and SQL Data Types getTimestamp()java.sql.TimeStampTIMESTAMP getTime()java.sql.TimeTIME getDate()java.sql.DateDATE getFloat()DoubleREAL getInt()IntegerINTEGER getDouble()DoubleFLOAT getDouble()DoubleDOUBLE getString()StringVARCHAR getString()StringCHAR getBoolean()BooleanBIT ResultSet get methodJava classSQL Type

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper14 JDBC: Exceptions and Warnings  Most of java.sql can throw and SQLException if an error occurs.  SQLWarning is a subclass of SQLException; not as severe (they are not thrown and their existence has to be explicitly tested)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper15 Warning and Exceptions (Contd.) try { stmt=con.createStatement(); warning=con.getWarnings(); while(warning != null) { // handle SQLWarnings; warning = warning.getNextWarning(): } con.clearWarnings(); stmt.executeUpdate(queryString); warning = con.getWarnings(); … } //end try catch( SQLException SQLe) { // handle the exception }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper16 Stored Procedures  What is a stored procedure:  Program executed through a single SQL statement  Executed in the process space of the server  Advantages:  Can encapsulate application logic while staying “close” to the data  Reuse of application logic by different users  Avoid tuple-at-a-time return of records through cursors

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper17 Summary  Embedded SQL allows execution of parametrized static queries within a host language  Dynamic SQL allows execution of completely ad-hoc queries within a host language  Cursor mechanism allows retrieval of one record at a time and bridges impedance mismatch between host language and SQL  APIs such as JDBC introduce a layer of abstraction between application and DBMS  Stored procedures execute application logic directly at the server

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper18 Executing SQL Statements  Three different ways of executing SQL statements:  Statement (both static and dynamic SQL statements)  PreparedStatement (semi-static SQL statements)  CallableStatment (stored procedures)  PreparedStatement class: Precompiled, parametrized SQL statements:  Structure is fixed  Values of parameters are determined at run-time

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper19 SQLJ Complements JDBC with a (semi-)static query model: Compiler can perform syntax checks, strong type checks, consistency of the query with the schema  All arguments always bound to the same variable: #sql = { SELECT name, rating INTO :name, :rating FROM Books WHERE sid = :sid;  Compare to JDBC: sid=rs.getInt(1); if (sid==1) {sname=rs.getString(2);} else { sname2=rs.getString(2);}  SQLJ (part of the SQL standard) versus embedded SQL (vendor-specific)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper20 SQLJ Code Int sid; String name; Int rating; // named iterator #sql iterator Sailors(Int sid, String name, Int rating); Sailors sailors; // assume that the application sets rating #sailors = { SELECT sid, sname INTO :sid, :name FROM Sailors WHERE rating = :rating }; // retrieve results while (sailors.next()) { System.out.println(sailors.sid + “ “ + sailors.sname)); } sailors.close();

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper21 SQLJ Iterators Two types of iterators (“cursors”):  Named iterator  Need both variable type and name, and then allows retrieval of columns by name.  See example on previous slide.  Positional iterator  Need only variable type, and then uses FETCH.. INTO construct: #sql iterator Sailors(Int, String, Int); Sailors sailors; #sailors = … while (true) { #sql {FETCH :sailors INTO :sid, :name} ; if (sailors.endFetch()) { break; } // process the sailor }