– How do I program such applica<ons? – SQL environment – Programming language environment (eg. Java) – Web-‐based applica<ons ? • Systems perspec<ve – How do I setup the system(s)? – Is applica<on program on the same machine as DBMS? – How does applica<on program “talk” to the DBMS server ? Lipyeow Lim -‐-‐ University of Hawaii at Manoa 2
large internet enterprises Lipyeow Lim -‐-‐ University of Hawaii at Manoa 3 Database Server Applica<on Server Webserver Eg. Apache/Tomcat Connects clients to database systems Eg. IBM Websphere Applica<on Server, Jboss, SAP Netweaver, etc. Performs business logic like shopping cart, checkout etc Eg. IBM DB2, Oracle, MS SQL Server Runs DBMS, performs queries and updates from app server
triggers – CREATE SCHEMA <schema name> – Your login id is your default schema – SET SCHEMA <schema> – A fully qualified table name is <schema>.<table> • Catalogs : collec<on of schemas – Corresponds to “databases” in DB2 • Clusters : collec<on of catalogs – Corresponds to “database instance” in DB2 Lipyeow Lim -‐-‐ University of Hawaii at Manoa 4 Schema Schema Catalog Schema Schema Catalog Cluster SQL Environment
name> AUTHORIZATION • DISCONNECT/CONNECT RESET/TERMINATE • Session – SQL opera<ons performed while a connec<on is ac<ve • Programming API – Generic SQL Interface – Embedded SQL in a host language – True Modules. Eg. Stored procedures. Lipyeow Lim -‐-‐ University of Hawaii at Manoa 5 SQL-‐Server SQL-‐Client Connection Session SQL-‐agent Module Applica<on Program Can be on same machine or different machines
integra<on spectrum: • Highly integrated eg. Microsof linq – Compiler checking of database opera<ons • Loosely integrated eg. ODBC & JDBC – Provides a way to call SQL from host language – Host language compiler doesn’t understand database opera<ons. • Requirements: – Perform DB opera<ons from host language – DB opera<ons need to access variables in host language Lipyeow Lim -‐-‐ University of Hawaii at Manoa 6
Manoa 7 Internet MAC address IP address Port number Higher level protocols MAC address IP address Port number Higher level protocols Each network “card” has a unique MAC address. IP address assigned by network provider: sta<c or DHCP Client Application DBMS Server Port number usually fixed by applica<on type Eg. hjp URLs, DNS DBMS servers use their own protocols (eg. DRDA) Servers use sta<c IP address + DNS name Servers use a port that is known by its clients
that is separate from the DB server • DBMS “thin” client – Libraries to link your app to – App needs to know how to talk to DBMS server via network • DBMS “full” client layer – Need to pre-‐configure the thick client layer to talk to DBMS server – Your app talks to a DBMS client layer as if it is talking to the server Lipyeow Lim -‐-‐ University of Hawaii at Manoa 8 Server DBMS App DB Libs App DB Client What informa<on is needed for 2 machines to talk over a network ?
to find the server db2 CATALOG TCPIP NODE mydbsrv REMOTE 123.3.4.12 SERVER 50001 • Tell the client where to find the server db2 CATALOG DATABASE bookdb AS mybookdb AT NODE mydbsrv Lipyeow Lim -‐-‐ University of Hawaii at Manoa 9 Give a name for this node Specify the IP address/ hostname and the port number of the DB server machine Specify the name of the database on the server Give a local alias for the database Specify the name of the node that is associated with this database
translates special macros to DB-‐ specific func<on calls • Pre-‐processor needs access to DBMS instance for valida<on. • Executable needs to be bound to a specific database in a DBMS in order to execute Lipyeow Lim -‐-‐ University of Hawaii at Manoa 11 .sqc DBMS-‐specific Precompiler .c C Compiler C Linker DBMS-‐specific Binder .bnd .o DBMS-‐ specific libraries .exe Database package
for host language to get data from SQL environment • Need a way to pass values from host language to SQL environment • Shared variables – DECLARE SECTION – In SQL, refer using :Salary, :EmployeeNo Lipyeow Lim -‐-‐ University of Hawaii at Manoa 12 EXEC SQL BEGIN DECLARE SECTION; char EmployeeNo[7]; char LastName[16]; double Salary; short SalaryNI; EXEC SQL END DECLARE SECTION;
#include <string.h> #include <sql.h> int main() { // Include The SQLCA Data Structure Variable EXEC SQL INCLUDE SQLCA; // Define The SQL Host Variables Needed EXEC SQL BEGIN DECLARE SECTION; char EmployeeNo[7]; char LastName[16]; double Salary; short SalaryNI; EXEC SQL END DECLARE SECTION; // Connect To The Appropriate Database EXEC SQL CONNECT TO SAMPLE USER db2admin USING ibmdb2; // Declare A Sta<c Cursor EXEC SQL DECLARE C1 CURSOR FOR SELECT EMPNO, LASTNAME, DOUBLE(SALARY) FROM EMPLOYEE WHERE JOB = 'DESIGNER'; // Open The Cursor EXEC SQL OPEN C1; Lipyeow Lim -‐-‐ University of Hawaii at Manoa 13
The Cursor Was Opened Successfully, while (sqlca.sqlcode == SQL_RC_OK) { EXEC SQL FETCH C1 INTO :EmployeeNo, :LastName, :Salary, :SalaryNI; // Display The Record Retrieved if (sqlca.sqlcode == SQL_RC_OK) { prinz("%-‐8s %-‐16s ", EmployeeNo, LastName); if (SalaryNI >= 0) prinz("%lf\n", Salary); else prinz("Unknown\n"); } } // Close The Open Cursor EXEC SQL CLOSE C1; // Commit The Transac<on EXEC SQL COMMIT; // Terminate The Database Connec<on EXEC SQL DISCONNECT CURRENT; // Return Control To The Opera<ng System return(0); } • A cursor is an iterator for looping through a rela<on instance. • Why is a cursor construct necessary ? Lipyeow Lim -‐-‐ University of Hawaii at Manoa 14
require current of <cursor> EXEC SQL BEGIN DECLARE SECTION; int certNo , worth ; char execName[31], execName[31], execAddr [256], SQLSTATE [6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE execCursor CURSOR FOR MovieExec; EXEC SQL OPEN execCursor while (1) { EXEC SQL FETCH FROM execCursor INTO :execName, :execAddr, :certNo, :worth; if (NO_MORE_ TUPLES) break; if ( worth < 1000) EXEC SQL DELETE FROM MovieExec WHERE CURRENT OF execCursor; else EXEC SQL UPDATE MovieExec SET netWorth=2*netWorth WHERE CURRENT OF execCursor; } EXEC SQL CLOSE execCursor Lipyeow Lim -‐-‐ University of Hawaii at Manoa 15
SQL queries that are completely specified at compile <me. Eg. // Declare A Sta<c Cursor EXEC SQL DECLARE C1 CURSOR FOR SELECT EMPNO, LASTNAME, DOUBLE(SALARY) FROM EMPLOYEE WHERE JOB = 'DESIGNER'; • Dynamic SQL refers to SQL queries that are note completely specified at compile <me. Eg. strcpy(SQLStmt, “SELECT * FROM EMPLOYEE WHERE JOB="); strcat(SQLStmt, argv[1]); EXEC SQL PREPARE SQL_STMT FROM :SQLStmt; EXEC SQL EXECUTE SQL_STMT; Lipyeow Lim -‐-‐ University of Hawaii at Manoa 16
terms of “Servicing a request” – Eg., a click on a “create” bujon on a “customer” page in a browser is a request. • Building a Web applica<on requires an approach of handling and processing hjp requests and sending appropriate responses back to the requesters Lipyeow Lim -‐-‐ University of Hawaii at Manoa 17 see http://www.javacodegeeks.com/ 2013/02/introduction-to-javaee- concepts.html