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



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

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

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

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

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

CS346: Database Programming.

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

SQL and Java. Database Systems Lecture 19 Natasha Alechina

CS/CE 2336 Computer Science II

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

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

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

The JAVA Way: JDBC and SQLJ

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

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

Database Access from a Programming Language:

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

Making Oracle and JDBC Work For You

Applets, RMI, JDBC Exam Review

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

Java DataBase Connectivity (JDBC)

DEVELOPING MULTITHREADED DATABASE APPLICATION USING JAVA TOOLS AND ORACLE DATABASE MANAGEMENT SYSTEM IN INTRANET ENVIRONMENT

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

More SQL: Assertions, Views, and Programming Techniques

Self-test Database application programming with JDBC

1 SQL Data Types and Schemas

Part IV: Java Database Programming

Programming Database lectures for mathema

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

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

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

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

CHAPTER 3. Relational Database Management System: Oracle. 3.1 COMPANY Database

Using ORACLE in the CSLab

Database Access Through Java Technologies

Porting from Oracle to PostgreSQL

Oracle8/ SQLJ Programming

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

Database System Concepts

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

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

Real SQL Programming 1

Applications of JAVA programming language to database management

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

LSINF1124 Projet de programmation

1 File Processing Systems

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

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

Oracle 10g PL/SQL Training

An Introduction to SQL Injection Attacks for Oracle Developers. January 2004 INTEGRIGY. Mission Critical Applications Mission Critical Security

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Developing SQL and PL/SQL with JDeveloper

Using DOTS as Apache Derby System Test

Oracle 11g PL/SQL training

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

Using SQL Server Management Studio

Performance Tuning for the JDBC TM API

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

WHITE PAPER. An Introduction to SQL Injection Attacks for Oracle Developers

FileMaker 11. ODBC and JDBC Guide

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

Database Programming with PL/SQL: Learning Objectives

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

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

14 Triggers / Embedded SQL

A Brief Introduction to MySQL

Java Application Developer Certificate Program Competencies

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

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

SQL and programming languages

Database Query 1: SQL Basics

Chapter 1 JDBC: Databases The Java Way! What Is The JDBC? The JDBC Structure ODBC s Part In The JDBC Summary

Apéndice C: Código Fuente del Programa DBConnection.java

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

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

Oracle to MySQL Migration

Abstract. Introduction. Web Technology and Thin Clients. What s New in Java Version 1.1

Database Access via Programming Languages

Embedded SQL programming

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

ODBC Client Driver Help Kepware, Inc.

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

2. Follow the installation directions and install the server on ccc

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

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Week 5: Embedded SQL. Embedded SQL 4. Application Program. Interactive vs. Non-Interactive SQL. Update Statements

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

5.1 Database Schema Schema Generation in SQL

How To Let A Lecturer Know If Someone Is At A Lecture Or If They Are At A Guesthouse

FileMaker 14. ODBC and JDBC Guide

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

SQL. Short introduction

How To Create A Table In Sql (Ahem)

Chapter 4: SQL. Schema Used in Examples

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Transcription:

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

A SQL Query Joke A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?' 2

Database Programming Database access through interactive interface (SQL*Plus) Database access through application programs or web applications 3

Database Programming Approaches Embedded SQL: database commands are embedded in a general-purpose programming language (language extension) SQLJ Database API: library of database functions available to the host language (API) JDBC New language PL/SQL 4

Comparison Embedded SQL: programs tend to be shorter SQL syntax can be checked at compile time. static SQL which performs better than dynamic SQL requires a preprocessing step Database API: Flexible and dynamic SQL for more complex applications New language Fast, tightly integrated, minimizes impedance mismatch Product specific 5

Impedance Mismatch Incompatibilities between a host programming language and the database model Attribute type mismatch and incompatibilities; requires binding for each language set vs. record-at-a-time processing; requires iterators to loop over query results and manipulate individual values 6

Sequence of Interaction in Database Programming 1. Opens a connection to database server 2. Interacts with database by submitting queries, updates, and other commands 3. Terminates the connection to database 7

Database Programming Approaches Embedded SQL: database commands are embedded in a general-purpose programming language (language extension) SQLJ Database API: library of database functions available to the host language (API) JDBC New language PL/SQL 8

Embedded SQL in Java SQLJ: an language extension for embedding SQL in Java An embedded SQL statement is distinguished from the host language statements by special prefix A preprocessor SQLJ translator is required to convert SQL statements into Java before the application program is compiled 9

10

11

Database Programming Approaches Embedded SQL: database commands are embedded in a general-purpose programming language (language extension) SQLJ Database API: library of database functions available to the host language (API) JDBC New language (SQL extension) PL/SQL 12

PL/SQL PL/SQL (Procedural Language/Structured Query Language) is Oracle's proprietary server-based procedural extension to the SQL database language. Gives user the power of general programming language 13

PL/SQL Demo: loop.sql DECLARE v_counter BINARY_INTEGER := 1; BEGIN LOOP INSERT INTO MyTable VALUES (v_counter, 'Loop index'); v_counter := v_counter + 1; IF v_counter > 50 THEN EXIT; END IF; END LOOP; END; / 14

Database Programming Approaches Embedded SQL: database commands are embedded in a general-purpose programming language (language extension) SQLJ Database API: library of database functions available to the host language (API) JDBC New language PL/SQL 15

JDBC JDBC: Java library for SQL function calls Consists of a set of classes and interfaces written in Java Allows the programmer to send SQL statements to a database server for execution and retrieve query results 16

JDBC Driver JDBC use JDBC Drivers to support communication transparency with specific DBMS A JDBC driver converts requests from Java programs to a protocol that a particular DBMS can understand. A Java program with JDBC functions can access any relational DBMS that has a JDBC driver Different types of JDBC driver 17

JDBC Driver 18

Before writing/running JDBC programs Set up CLASSPATH to include Oracle's JDBC libraries on Math/CS server $ORACLE_HOME/jdbc/lib/ojdbc14.jar (/oracle/app/oracle/product/10.2.0/jdbc/lib/ojdbc14.jar) 19

Writing JDBC programs 1. Import JDBC library (java.sql.*) 2. Load appropriate JDBC driver (via DriverManager.registerDriver) 3. Create a connection object (via DriverManager.getConnection) 4. Create a statement object from the Statement class: 1. Statement 2. PreparedStatment 3. CallableStatement 5. Execute SQL statement (referenced by an object) via JDBC s executequery 6. Process query results (returned in an object of type ResultSet) 20

Step 2. Load JDBC Driver Use generic java function for loading a class (must use try/catch block) Class.forName( oracle.jdbc.driver.oracledriver ) Use DriverManager class (defined in java.sql package) that keeps track of installed drivers DriverManager.registerDriver(new oracle.jdbc.driver.oracledriver()); 21

Step 3. Connect to the Database Connection conn= DriverManager.getConnection(url, user, password); The DriverManager.getConnectioncreate a Connection object that maintains all the state information of a connection to the database server. Connect URL for Math/CS Oracle Server for CS377 "jdbc:oracle:thin:@cs377.mathcs.emory.edu:1521:cs377" Name of machine where the SQL server is running: cs377.mathcs.emory.edu Port: 1521 (Oracle's default port) Database space name: cs377 22

Step 4. Creating SQL Statement Connection conn= DriverManager.getConnection(url, user, password); Statement stmt = SQLconnection.createStatement(); createstatement in Connection class creates a Statement object for sending SQL statements to the database server. 23

5. Executing SQL Statements - Queries Connection conn= DriverManager.getConnection(url, user, password); Statement stmt = SQLconnection.createStatement(); ResultSet rset = stmt.executequery( SELECT * FROM cs377000.employee ); executequery(string sql) in Statement class Executes a given SQL statement returns a single ResultSet object Retrieve data from ResultSet (Step 6) 24

5. Executing SQL Statements - Updates intcount = stmt.executeupdate( "INSERT INTO cs377000.employee(fname) VALUES ( John' ) " ); executeupdate(string sql) in Statement class executes a given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL DDL returns an update count 25

Prepared Statement Prepared Statement - precompiled and can be executed multiple times in an efficient manner. Step 4. Prepare a statement preparestatement(string sql) of Connection class prepare a statement and returns PreparedStatement Step 5. Executing SQL statements ResultSet executequery() of PreparedStatement class - Executes the SQL query in this PreparedStatement object and returns the ResultSet int executeupdate() of PreparedStatement class Executes SQL INSERT, UPDATE or DELETE statement; and returns update count. 26

Step 6. Process Query Results ResultSetrset= stmt.executequery("sql-query"); while ( rset.next() ) { stringvar= rset,getstring(index); intvar= rset.getint(index); } A ResultSet object maintains a cursor to current row Initially the cursor is positioned before the first row Use cursor methods to iterate through tuples (rows) next() moves the cursor forward one row next () if there are still more tuples and returns TRUE Use getter methods to retrieve attribute (column) values from the current row getstring(int columnindex) getstring(string columnlabel) 27

Common Java and SQL Type Equivalence Java method getint getlong getfloat getdouble getbignum getboolean getstring getstring getdate gettime gettimestamp getobject SQL Type INTEGER BIG INT REAL FLOAT DECIMAL BIT VARCHAR CHAR DATE TIME TIME STAMP any type 28

Getting Meta Data about Result Set ResultSet rset = stmt.executequery("sql-query"); ResultSetMetaData metadata = rset.getmetadata(); getmetadata() of ResultSet creates a ResultSetMetaData object that contains meta information about the result set Getter methods of ResultSetMetaData int getcolumncount() - returns the number of columns in the table String getcolumnname(int columnindex) - returns the name of column "columnindex String getcolumntypename(int columnindex) - returns the type of column "columnindex" 29

Demo Employee.java MetaData.java 30