1st Semester, 2017 Sanghyun Park

Slides:



Advertisements
Similar presentations
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Advertisements

Chapter 5: Advanced SQL Accessing SQL From a Programming Language
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Advanced SQL Database Management Systems I Alex Coman, Winter 2006.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Advanced SQL.
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Advanced SQL.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
Chapter 4~5 Intermediate/Advanced SQL Yonsei University 1 st Semester, 2015 Sanghyun Park.
Chapter 4: Advanced SQL.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
CSc340 4a1 Advanced SQL Chapter 5 Accessing SQL from a Programming Language Functions and Procedures Triggers.
SQL By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Lecture 10 of 42 Tuesday, 06 February 2007.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Computing & Information Sciences Kansas State University Monday, 15 Sep 2008CIS 560: Database System Concepts Lecture 8 of 42 Monday, 15 September 2008.
Chapter 4: Advanced SQL. 4.2Unite International CollegeDatabase Management Systems Chapter 4: Advanced SQL SQL Data Types and Schemas Integrity Constraints.
Computing & Information Sciences Kansas State University Mon, 11 Sep 2006CIS 560: Database System Concepts Lecture 8 of 42 Monday, 11 September 2006 William.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 7 of 42 Friday, 08 September 2006.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 4 Advanced SQL SQL Data.
Advanced SQL: Cursors & Stored Procedures
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Advanced SQL.
BD05/06 Database Application Development  Embedded SQL  ODBC  JDBC  Application architectures.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
 SQL Data Types and Schemas  Integrity Constraints  Authorization  Embedded SQL  Dynamic SQL  Functions and Procedural Constructs**  Recursive.
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.
Chapter 4: Advanced SQL. SQL Data Types and Schemas Integrity Constraints Authorization Embedded SQL Dynamic SQL Functions and Procedural Constructs**
Computing & Information Sciences Kansas State University Mon, 10 Sep 2007CIS 560: Database System Concepts Lecture 8 of 42 Monday, 10 September 2007 William.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Chapter 1: Introduction
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Chapter 5: Advanced SQL.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 4 SQL Basic Structure.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
1 Schema Used in Examples. 2 Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module 7: Advanced SQL.
1. Writing a Java program to connect to SQL Server 2008 and Create a table Populate the table (insert data) Perform queries to retrieve information from.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
: Advanced SQL.
Chapter 4: Advanced SQL.
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
CS320 Web and Internet Programming Database Access with JDBC
SQL By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) 1.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
HW#4 Making Simple BBS Using JDBC
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Basic Structure of SQL The basic structure of an SQL expression consists of three clauses: The select clause corresponds to the projection operation of.
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
CPSC-310 Database Systems
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Introduction to Database
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Unit I-2.
Chapter 5: Advanced SQL.
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
JDBC Example.
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
Presentation transcript:

1st Semester, 2017 Sanghyun Park Chapter 5 Advanced SQL 1st Semester, 2017 Sanghyun Park

Outline Accessing SQL From Programming Language Embedded SQL ODBC JDBC Functions and Procedures (생략) Triggers (생략)

Embedded SQL (1/4) The SQL standard defines embeddings of SQL in a variety of programming languages such as C, C++, and Java A language to which SQL queries are embedded is referred to as a host language, and the SQL structures permitted in the host language comprise embedded SQL The basic form of these languages follows that of the System R embedding of SQL into PL/I EXEC SQL statement is used to identify embedded SQL request to the preprocessor: EXEC SQL <embedded SQL statement > END-EXEC

Embedded SQL (2/4) From within a host language, find the IDs and names of all students who have taken more than credit_amount credit hours Specify the query in SQL and declare a cursor for it EXEC SQL declare c cursor for select ID, name from student where tot_cred > :credit_amount; END-EXEC

Embedded SQL (3/4) The open statement causes the query to be evaluated EXEC SQL open c END-EXEC The fetch statement causes the values of one tuple in the query result to be placed on host language variables EXEC SQL fetch c into :si, :sn END-EXEC Repeated calls to fetch get successive tuples A variable called SQLSTATE in the SQL communication area (SQLCA) gets ‘02000’ to indicate that no more data is available The close statement causes the database system to delete the temporary relation that holds the result of the query EXEC SQL close c END-EXEC

Embedded SQL (4/4) Can update tuples fetched by cursor by declaring that the cursor is for update declare c cursor for select * from instructor where dept_name = ‘Music’ for update To update tuple at the current location of cursor update instructor set salary = salary + 100 where current of c

Embedded SQL: Practice 프로그램 데모 – 과목 조교

ODBC (1/8) Open DataBase Connectivity (ODBC) standard Standard for application program to communicate with a DB Application program interface (API) to Open a connection with a database Send queries and updates Get results back Applications such as GUI, statistical packages, and spreadsheets can use ODBC

ODBC (2/8) Each database system supporting ODBC provides a “driver” library that must be linked with the client program When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action, and fetch results ODBC program first allocates an SQL environment, then a database connection handle

ODBC (3/8) Opens database connection using SQLConnect() Parameters for SQLConnect: Connection handle The server to which to connect The user identifier, password Must also specify types of arguments: SQL_NTS denotes that argument is a null-terminated string

ODBC (4/8) int ODBCexample() { RETCODE error; HENV env; /* environment */ HDBC conn; /* database connection */ SQLAllocEnv(&env); SQLAllocConnect(env, &conn); SQLConnect(conn, “db.yale.edu", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); { …. Do actual work … } SQLDisconnect(conn); SQLFreeConnect(conn); SQLFreeEnv(env); }

ODBC (5/8) Program sends SQL commands to the database by using SQLExecDirect SQLBindCol() binds C language variables to attributes of the query result. Arguments to SQLBindCol(): ODBC stmt variable, attribute position in query result The type conversion from SQL to C The address of the variable For variable-length types like character arrays, the maximum length of the variable and location to store actual length when a tuple is fetched Result tuples are fetched using SQLFetch()

ODBC (6/8) Main body of program char deptname[80]; float salary; int lenOut1, lenOut2; HSTMT stmt; char * sqlquery = “select dept_name, sum (salary) from instructor group by dept_name”; SQLAllocStmt(conn, &stmt); error = SQLExecDirect(stmt, sqlquery, SQL_NTS); if (error == SQL_SUCCESS) { SQLBindCol(stmt, 1, SQL_C_CHAR, deptname, 80, &lenOut1); SQLBindCol(stmt, 2, SQL_C_FLOAT, &salary, 0 , &lenOut2); while (SQLFetch(stmt) == SQL_SUCCESS) { printf (“ %s %g\n”, deptname, salary); } } SQLFreeStmt(stmt, SQL_DROP);

ODBC (7/8) Prepared Statement Metadata features SQL statement prepared: compiled at the database Can have placeholders: E.g. insert into department values(?,?,?) Repeatedly executed with actual values for the placeholders Metadata features Finding all the relations in the database and Finding the names and types of columns of a query result or a relation in the database

ODBC (8/8) By default, each SQL statement is treated as a separate transaction that is committed automatically Can turn off automatic commit on a connection SQLSetConnectOption(conn, SQL_AUTOCOMMIT, 0) Transactions must then be committed or rolled back explicitly SQLTransact(conn, SQL_COMMIT) or SQLTransact(conn, SQL_ROLLBACK)

ODBC: Practice 프로그램 데모 – 과목 조교

JDBC (1/5) JDBC is a Java API for communicating with database systems supporting SQL JDBC supports a variety of features for querying and updating data, and for retrieving query results JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes

JDBC (2/5) Model for communicating with the database: Open a connection Create a “statement” object Execute queries using the Statement object to send queries and fetch results Exception mechanism to handle errors

JDBC (3/5) public static void JDBCexample(String dbid, String userid, String passwd) { try { Class.forName (“oracle.jdbc.driver.OracleDriver”); Connection conn = DriverManager.getConnection(“jdbc:oracle:thin:@db.yale.edu: 1521:univdb”, userid, passwd); Statement stmt = conn.createStatement(); … Do Actual Work …. stmt.close(); conn.close(); } catch (SQLException sqle) { System.out.println(“SQLException : ” + sqle);

JDBC (4/5) Update to database try { stmt.executeUpdate(“insert into instructor values (‘77987’, ‘Kim’, ‘Physics’, 98000)”); } catch (SQLException sqle) { System.out.println(“Could not insert tuple. “ + sqle); } Execute query and fetch and print results ResultSet rset = stmt.executeQuery( “select dept_name, avg(salary) from instructor group by dept_name”); while (rset.next()) { System.out.println( rset.getString(“dept_name”) + “ ” + rset.getFloat(2));

JDBC (5/5) PreparedStatement pStmt = conn.prepareStatement( Prepared statement allows queries to be compiled once and executed multiple times with different arguments PreparedStatement pStmt = conn.prepareStatement( “insert into instructor values(?,?,?,?)”); pStmt.setString(1, “88877”); pStmt.setString(2, “Perry”); pStmt.setString(3, “Finance”); pStmt.setInt(4, 125000); pStmt.executeUpdate(); pStmt.setString(1, “88878”); pStmt.executeUpdate();

JDBC: Practice 프로그램 데모 – 과목 조교