ICS 321 Fall 2009 ODBC, JDBC & Stored Procedures Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/8/20091Lipyeow.

Slides:



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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
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
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
CSE470 Software Engineering Fall Database Access through Java.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
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.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
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.
CS 405G: Introduction to Database Systems Database programming.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
CSCD34 - Data Management Systems – A. Vaisman1 Database Application Development.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
Advanced SQL: Cursors & Stored Procedures
JDBC – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
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.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
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.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
1 Database Application Development Chapter 6 Sections –
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC Session 14.
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
1 Database Application Development Programming With Databases.
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
Interacting with Database
JDBC 15-Apr-18.
Database Application Development
Database Application Development
JDBC 21-Aug-18.
Client Access, Queries, Stored Procedures, JDBC
JDBC 15-Nov-18.
Database Application Development
Database Application Development
Presentation transcript:

ICS 321 Fall 2009 ODBC, JDBC & Stored Procedures Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/8/20091Lipyeow Lim -- University of Hawaii at Manoa

Alternative to Embedded SQL What if we want to compile an application without the need for a DBMS-specific pre-compiler ? Use a library of database calls – Standardized (non-DBMS-specific) API – Pass SQL-strings from host language and presents result sets in a language friendly way – Eg. ODBC for C/C++ and JDBC for Java – DBMS-neutral A driver traps the calls and translates them into DBMS-specific code 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa2 Application DBMS-speciic Driver ODBC/JDBC API DBMS network

ODBC/JDBC Architecture Application – Initiates connections – Submits SQL statements – Terminates connections Driver Manager – Loads the right JDBC driver Driver – Connects to the data source, – Transmit requests, – Returns results and error codes Data Source – DBMS 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa3 Application Driver Manager Driver Data Source

4 Types of Drivers Type I: Bridge – Translate SQL commands to non-native API – eg. JDBC-ODBC bridge. JDBC is translated to ODBC to access an ODBC compliant data source. Type II: Direct Translation to native API via non-Java driver – Translates SQL to native API of data source. – Needs DBMS-specific library on each client. Type III: Network bridge – SQL stmts sent a middleware server that talks to the data source. Hence small JDBC driver at each client Type IV: Direct Translation to native API via Java driver – Converts JDBC calls to network protocol used by DBMS. – Needs DBMS-specific Java driver at each client. 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa4

High Level Steps 1.Load the ODBC/JDBC driver 2.Connect to the data source 3.[optional] Prepare the SQL statements 4.Execute the SQL statements 5.Iterate over the resultset 6.Close the connection 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa5

Prepare Statement or Not ? Executing without preparing statement – After DBMS receives SQL statement, The SQL is compiled, An execution plan is chosen by the optimizer, The execution plan is evaluated by the DBMS engine The results are returned conn.prepareStatement – Compiles and picks an execution plan pstmt.executeUpdate – Evaluates the execution plan with the parameters and gets the results 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa6 String sql=“SELECT * FROM books WHERE price < ?”; PreparedStatement pstmt = conn.prepareStatement(sql); Pstmt.setFloat(1, usermaxprice); Pstmt.executeUpdate(); cf. Static vs Dynamic SQL

ResultSet Iterate over the results of a SQL statement -- cf. cursor Note that types of column values do not need to be known at compile time 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa7 ResultSet rs = stmt.executeQuery(sqlstr); while( rs.next() ){ col1val = rs.getString(1); … } SQL TypeJava Classaccessor BITBooleangetBoolean CHAR, VARCHAR StringgetString DOUBLE, FLOAT DoublegetDouble INTEGERIntegergetInt REALDoublegetFloat DATEJava.sql.DategetDate TIMEJava.sql.TimegetTime TIMESTAMPJava.sql.TimeStampgetTimestamp

RowSet When inserting lots of data, calling an execute statement for each row can be inefficient – A message is sent for each execute Many APIs provide a rowset implementation – A set of rows is maintained in-memory on the client – A single execute will then insert the set of rows in a single message Pros: high performance Cons: data can be lost if client crashes. Analogous rowset for reads (ie. ResultSet) also available 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa8

Stored Procedures What ? – A procedure that is called and executed via a single SQL statement – Executed in the same process space of the DBMS server – Can be programmed in SQL, C, java etc – The procedure is stored within the DBMS Advantages: – Encapsulate application logic while staying close to the data – Re-use of application logic by different users – Avoid tuple-at-a-time return of records through cursors 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa9

SQL Stored Procedures CREATE PROCEDURE ShowNumReservations SELECT S.sid, S.sname, COUNT(*) FROM Sailors S, Reserves R WHERE S.sid = R.sid GROUP BY S.sid, S.sname Parameters modes: IN, OUT, INOUT CREATE PROCEDURE IncreaseRating ( IN sailor_sid INTEGER, IN increase INTEGER ) UPDATE Sailors SET rating = rating + increase WHERE sid = sailor_sid 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa10

Java Stored Procedures CREATE PROCEDURE TopSailors ( IN num INTEGER) LANGUAGE JAVA EXTERNAL NAME “file:///c:/storedProcs/rank.jar” 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa11

Calling Stored Procedures SQL: CALL IncreaseRating(101, 2); Embedded SQL in C: EXEC SQL BEGIN DECLARE SECTION int sid; int rating; EXEC SQL END DECLARE SECTION EXEC SQL CALL IncreaseRating(:sid, :rating); JDBC CallableStatement cstmt = conn.prepareCall(“{call Show Sailors}); ResultSet rs=cstmt.executeQuery(); ODBC SQLCHAR *stmt = (SQLCHAR *)"CALL ShowSailors"; cliRC = SQLPrepare(hstmt, stmt, SQL_NTS); cliRC = SQLExecute(hstmt); 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa12

User Defined Functions (UDFs) Extend and add to the support provided by SQL built-in functions Three types of UDFs – Scalar: returns a single-valued answer. Eg. Built- ing SUBSTR() – Column: returns a single-valued answer from a column of values. Eg. AVG() – Table: returns a table. Invoked in the FROM clause. Programable in SQL, C, JAVA. 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa13

Scalar UDFs Returns the tangent of a value CREATE FUNCTION TAN (X DOUBLE) RETURNS DOUBLE LANGUAGE SQL CONTAINS SQL RETURN SIN(X)/COS(X) Reverses a string CREATE FUNCTION REVERSE(INSTR VARCHAR(4000)) RETURNS VARCHAR(4000) CONTAINS SQL BEGIN ATOMIC DECLARE REVSTR, RESTSTR VARCHAR(4000) DEFAULT ''; DECLARE LEN INT; IF INSTR IS NULL THEN RETURN NULL; END IF; SET (RESTSTR, LEN) = (INSTR, LENGTH(INSTR)); WHILE LEN > 0 DO SET (REVSTR, RESTSTR, LEN) = (SUBSTR(RESTSTR, 1, 1) CONCAT REVSTR, SUBSTR(RESTSTR, 2, LEN - 1), LEN - 1); END WHILE; RETURN REVSTR; END 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa14

Table UDFs returns the employees in a specified department number. CREATE FUNCTION DEPTEMPLOYEES (DEPTNO CHAR(3)) RETURNS TABLE ( EMPNO CHAR(6), LASTNAME VARCHAR(15), FIRSTNAME VARCHAR(12)) LANGUAGE SQL READS SQL DATA RETURN SELECT EMPNO, LASTNAME, FIRSTNME FROM EMPLOYEE WHERE EMPLOYEE.WORKDEPT = DEPTEMPLOYEES.DEPTNO 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa15

Java UDFs CREATE FUNCTION tableUDF ( DOUBLE ) RETURNS TABLE ( name VARCHAR(20), job VARCHAR(20), salary DOUBLE ) EXTERNAL NAME 'MYJAR1:UDFsrv!tableUDF‘ LANGUAGE JAVA PARAMETER STYLE DB2GENERAL NOT DETERMINISTIC FENCED NO SQL NO EXTERNAL ACTION SCRATCHPAD 10 FINAL CALL DISALLOW PARALLEL NO import COM.ibm.db2.app.UDF; public void tableUDF( double inSalaryFactor, String outName, String outJob, double outNewSalary) throws Exception { int intRow = 0; … } // tableUDF } // UDFsrv class 10/8/2009Lipyeow Lim -- University of Hawaii at Manoa16