6. Application Programming

Similar documents
CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

The JAVA Way: JDBC and SQLJ

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

CS346: Database Programming.

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

DATABASDESIGN FÖR INGENJÖRER - 1DL124

SQL and programming languages

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

More SQL: Assertions, Views, and Programming Techniques

CS/CE 2336 Computer Science II

SQL and Programming Languages. SQL in Programming Languages. Applications. Approaches

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

JDBC (Java / SQL Programming) CS 377: Database Systems

Database Application Development. Overview. SQL in Application Code. Chapter 6

SQL and Java. Database Systems Lecture 19 Natasha Alechina

MAKING ORACLE AND SQLJ WORK FOR YOU John Jay King, King Training Resources

Microsoft SQL Server Features that can be used with the IBM i

Java Server Pages and Java Beans

1 SQL Data Types and Schemas

Java Application Developer Certificate Program Competencies

Database System Concepts

Applets, RMI, JDBC Exam Review

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

CACHÉ: FLEXIBLE, HIGH-PERFORMANCE PERSISTENCE FOR JAVA APPLICATIONS

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

An Oracle White Paper June Migrating Applications and Databases with Oracle Database 12c

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

Mimer SQL. Programmer s Manual. Version 8.2 Copyright 2000 Mimer Information Technology AB

SQL is capable in manipulating relational data SQL is not good for many other tasks

Statement Level Interface. Call Level Interface. Static SQL. Status. Connections. Transactions. To connect to an SQL database, use a connect statement

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Programming Database lectures for mathema

Accesssing External Databases From ILE RPG (with help from Java)

Micro Focus Database Connectors

FileMaker 11. ODBC and JDBC Guide

FileMaker 14. ODBC and JDBC Guide

Security Module: SQL Injection

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

Java and Microsoft Access SQL Tutorial

Japan Communication India Skill Development Center

2/9/2010. Standard Approach to Distribution. Remote Procedure Calls (RPC) Example: Music Jukebox in the Cloud

Performance Tuning for the JDBC TM API

High-Performance Oracle: Proven Methods for Achieving Optimum Performance and Availability

Version Overview. Business value

CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

12 Embedding SQL in Programming languages

Chapter 4: SQL. Schema Used in Examples

DB2 Application Development and Migration Tools

N3612: Desiderata of a C++11 Database Interface

1 File Processing Systems

Oracle8/ SQLJ Programming

Brazil + JDBC Juin 2001, douin@cnam.fr

Real SQL Programming. Embedded SQL Call-Level Interface Java Database Connectivity

PHP Language Binding Guide For The Connection Cloud Web Services

1. What is SQL Injection?

Oracle to MySQL Migration

Database Programming with PL/SQL: Learning Objectives

Precondition for a good understanding: knowledge of a higher programming language (e.g. C, Java, Perl, Pascal, Basic,... ) basic knowledge of html

DB2 Connect for NT and the Microsoft Windows NT Load Balancing Service

Risks to Database Activities

Using ORACLE in the CSLab

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Real SQL Programming 1

Migrating Non-Oracle Databases and their Applications to Oracle Database 12c O R A C L E W H I T E P A P E R D E C E M B E R

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

CSc 230 Software System Engineering FINAL REPORT. Project Management System. Prof.: Doan Nguyen. Submitted By: Parita Shah Ajinkya Ladkhedkar

Programming Language Inter-conversion

12 Embedding SQL in Programming languages

Japan Communication India Skill Development Center

Querying Databases Using the DB Query and JDBC Query Nodes

SQL Server 2005 Reporting Services (SSRS)

A Brief Introduction to MySQL

Oracle Database 12c Enables Quad Graphics to Quickly Migrate from Sybase to Oracle Exadata

How I hacked PacketStorm ( )

Japan Communication India Skill Development Center

Database Access via Programming Languages

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

FileMaker 12. ODBC and JDBC Guide

Using SQL Developer. Copyright 2008, Oracle. All rights reserved.

Java (12 Weeks) Introduction to Java Programming Language

Exploiting Java Technology with the SAS Software Barbara Walters, SAS Institute Inc., Cary, NC

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Elements of Advanced Java Programming

MSIcode Scripting Technology

Using the SQL Server Linked Server Capability

Japan Communication India Skill Development Center

Object Oriented Design with UML and Java. PART XVIII: Database Technology

Lesson 4 Web Service Interface Definition (Part I)

Transcription:

6. Application Programming 6.1 Introduction 6.1 Introduction Database and programming languages 6. Application Programming SQL is a powerful declarative query language. The formulation of queries in SQL is usually simpler than the coding of the same queries in an all-purpose programming language. There are at least two reasons for accessing a database with a programming language from a user perspective: Not all queries can be expressed in SQL (little functionality for everyday programming) since SQL does not have the full expressive power of a programming language. In order to be able to express such queries, SQL can be embedded into a more powerful language. Applications are usually developed in imperative and object-oriented languages (C, Cobol, Fortran, Java, C++,...). Non-declarative actions like printing, interaction with the user, or transmission of query results to a graphical user interface are outside of SQL. Task sharing: query processing and updates with SQL, all other tasks with the aid of a programming language. 145

6. Application Programming 6.1 Introduction SQL queries can be automatically optimized and efficiently executed. The use of a programming language only makes an automatic optimization extraordinarily difficult. Ad hoc queries are posed mostly by experts and more seldom. Frequently non-interactive batch applications are needed. Often the possibilities of the DBMS for representing data are limited and unsuitable for user requirements. Special integration problem (impedance mismatch): programming language supports the processing of single data records (tuple-oriented approach). SQL supports the processing of data records, i.e., of relations (set-oriented approach). consequently the question: How can the programming of database tasks be combined with the usual tasks without abandoning the benefits of SQL? 146

6. Application Programming 6.1 Introduction Alternatives for coupling loose coupling: Constructs of the database language are embedded into a program of a programming language and specially marked. Particular methods are employed to migrate from the set-oriented processing of SQL to the processing of single variables/records of the programming language. CALL interface + provision of libraries embedding with preprocessor (embedded SQL) + static: structure of the SQL commands is predefined + dynamic: arbitrary SQL commands are allowed integration: A special database language is developed which incorporates the usual programming language concepts and the set-oriented operations of the relational DBMS in a most possible unified way. language extensions + of SQL + of an imperative or object-oriented programming language script languages + languages similar to BASIC without type concept + simple connection to windows- and graphic-oriented interfaces 147

6.2 Procedural CALL Interface Use of a library by employing the Oracle Call Interface (OCI) source files host compiler host linker DBMS library AP DBMS server database 148

Components of the CALL interface data structures shared by the AP (application program) and the database server for establishing the communication for processing a query cursor concept: data structure used in the AP for accessing the relations of the DB in AP storage of SQL queries in a string type checking only possible in the AP binding of the variables of the AP to the data structures of the DBMS server running an AP: establishing the connection to the DBMS server initialising a cursor parsing an SQL statement binding input variables to an SQL statement executing an update or a query closing the cursor decoupling from the server 149

executing a query requesting the output parameters binding the output to variables of the AP positioning the cursor abort of the query drawbacks of the CALL interface complicated programming error-prone advantage: high flexibility JDBC - a CALL interface in Java database programming in Java together with a use of SQL JDBC (Java Data Base Connectivity) protocol allows Java applications to access relational databases, independently of a particular DBMS. Queries are transmitted as uninterpreted strings to the DBMS. Results are sent through objects of a class ResultSet from the DBMS to the AP. 150

client-server concept DBMS runs as server for several clients on another computer than the AP. uniform interface for different DBMS use of strong typing (whenever possible) support of important concepts static queries SQL queries that can be parameterized support of very large objects dynamic queries and metadata Client-server coupling in JDBC Native coupling Client JDBC DBMS-Net Server DBMS Special net software of the DBMS is installed on the client. Methods in JDBC are based on the functions of the underlying net software. 151

disadvantages + installation of the net software on all clients + no independence from the database system ODBC coupling Client JDBC ODBC + database driver Server DBMS ODBC ODBC and corresponding database driver are installed each client. Methods of JDBC are based on the functionality of ODBC. ODBC: The standard Open Database Connectivity is an application program interface (API) that was suggested by Microsoft in 1991. ODBC supports a special limited SQL which is characterized by a minimal set of instructions. advantage: independence from special database systems disadvantages: + installation of ODBC + database drivers on all clients + overhead of ODBC 152

native pure Java client Client JDBC DBMS-Net download Server DBMS DBMS-Net similar to the first solution, but the net software is reimplemented in Java and downloaded from the server on request (see e.g. the current coupling to Oracle). advantage: no installation of special software on the client disadvantage: dependence on database manufacturer native protocol pure Java clients Client JDBC + database driver Server DBMS JDBC A special network connection implemented in Java transmits the requirements of the clients to a server. The server forwards the requirements to the DBMS. advantages + database independence + no software on the client 153

Establishing a connection creation of a connection object Connection con = DriverManager.getConnection( jdbc:oracle:thin:@venus.mathematik.uni-marburg.de:1521:init_db, scott, tiger ); First string corresponds to an URL to the database. Second string is the user name. Third string is the password. Before creating the connection object the corresponding driver class has to selected. Class.forName( oracle.jdbc.driver.oracledriver ); Interpreted queries SQL query is interpreted (translated and at the same time executed). The result of the query is transfered to an object of class ResultSet. A repeated execution of the query requires a new interpretation. The query itself cannot be parameterized. 154

example: // Creation of a new object of class Statement Statement stmt = con.createstatement(); // Translation of the query and creation of a new object of class ResultSet ResultSet rs = stmt.executequery( select count(*) as number from user_tables ); // Operation next provides the functionality of an iterator. rs.next(); // Access to the attribute values with get functions System.out.println( Number of tables:, + rs.getint(1)); System.out.println( Number of tables:, + rs.getstring( number )); // alternatively Precompiled queries In these queries the translation of an SQL query is separated from the execution of the query. example: // An SQL query is translated with two parameters. PreparedStatement stmt = con.preparestatement( select x, y from Points where x <? and x >? ); 155

// The parameters of the query are set. stmt.setint(1, 20); stmt.setint(2, 10); // The query is executed. ResultSet rs = stmt.executequery(); advantages If queries are executed several times in a similar way, time is saved for the repeated translation process. high optimization costs only once due to one translation Dynamic SQL in Java JDBC permits to pose queries dynamically, since an arbitrary object of class String is expected as input for the execution of an SQL statement. example: String str;... ResultSet rs = con.createstatement().executequery(str); problem: type of the result is unknown at run time 156

In order to provide such type information at run time, the class ResultSetMetaData is used. This class offers operations to query for metadata like the number of attributes and the database types of the result. An object of the class is then created by ResultSetMetaData rsmd = rs.getmetadata(); Afterwards the number of attributes of the result relation can be determined, for example, with the statement int count = rsmd.getcolumncount(); and with for (int i = 0; i < count; i++) { int sqltype = rsmd.getcolumntype(i);... } an integer is returned in each loop, which yields the type of the ith attribute. For each type the corresponding get function can then be called, for example. 157

6. Application Programming 6.3 Embedded SQL in Java (esql, SQLJ) 6.3 Embedded SQL in Java (esql, SQLJ) Basic principles use of a preprocessor static determination of database operations at translation time type checking between AP and database through the preprocessor simple transmission of data from the database into the AP use of the cursor principle for traversing relations source files preprocessor bytecode host compiler host linker AP DBMS library DBMS server database 158

6. Application Programming 6.3 Embedded SQL in Java (esql, SQLJ) Syntactical tagging of database operations in Java APs syntax: #sql{<sql statement>} An SQL statement relates to database objects. An exception are the so-called host variables that are used for the data transfer between the database and the AP. A host variable can be declared and used like a usual variable in Java. A host variable can be used in an SQL statement by preceding the variable name with a :. The purpose of host variables is to receive the results of a query. Only one result may be assigned at a time. extension of the select clause by the keyword into followed by the host variable examples: #sql{select A, B from R where B > :x} The value of the variable x is inserted into the SQL command. #sql{select A, B into :a, :b from R where Id = 7} The result is bound to the host variables a and b (assumption: Id is key candidate). 159