Database Management COP4540, SCS, FIU Programming with ODBC.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Fundamentals, Design, and Implementation, 9/e Chapter 12 ODBC, OLE DB, ADO, and ASP.
Fundamentals, Design, and Implementation, 9/e COS 346 DAY 22.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
Dec 11, 2003Murali Mani SQL with other Programming Languages B term 2004: lecture 16.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Client-server database systems and ODBC l Client-server architecture and components l More on reliability and security l ODBC standard.
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 12-1 David M. Kroenke’s Chapter Twelve: ODBC Database Processing: Fundamentals,
The Client/Server Database Environment
ODBC Open DataBase Connectivity a standard database access method developed by Microsoft to access data from any application regardless of which database.
Database Applications.  Database Programming  Web databases  Application architecture.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Agenda Journalling More Embedded SQL. Journalling.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
1 ODBC, OLE DB, ADO, and ASP. 2 Introduction  Because database applications today reside in a complicated environment, various standards have been developed.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
ODBC : Open Database Connectivity SNU OOPSLA Lab. October 2005.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
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.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
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.
Triggers and Stored Procedures in DB 1. Objectives Learn what triggers and stored procedures are Learn the benefits of using them Learn how DB2 implements.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Advanced SQL: Cursors & Stored Procedures
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.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
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.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
AVCE ICT – Unit 7 - Programming Session 16 – Database and VB.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009.
Basics of JDBC Session 14.
ODBC : Open Database Connectivity SNU OOPSLA Lab. October 2005.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
ISC321 Database Systems I Chapter 2: Overview of Database Languages and Architectures Fall 2015 Dr. Abdullah Almutairi.
Introduction to Database Programming with Python Gary Stewart
ASP.NET Programming with C# and SQL Server First Edition
1st Semester, 2017 Sanghyun Park
JDBC Database Management Database connectivity
The Client/Server Database Environment
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 8 Working with Databases and MySQL
Introduction of Week 11 Return assignment 9-1 Collect assignment 10-1
Lecture 11 Database Connection
Objectives In this lesson, you will learn to:
Presentation transcript:

Database Management COP4540, SCS, FIU Programming with ODBC

Database Management COP4540, SCS, FIU Open DataBase Connectivity Standard database accessing interface. DBMS independent. Supporting many programming languages. Microsoft implements the first ODBC driver manager over Windows Platforms. A good way to implement client-server database under MS. Windows environment.

Database Management COP4540, SCS, FIU ODBC Components Application Performs processing and calls ODBC functions to submit SQL statements and retrieve result. Driver Manager Loads drivers on behalf of an application. Driver Processes ODBC function calls, submits SQL requests to a specific data source, and returns results to the application. Data Source Consists of the data the user want to access and its associated operating system, DBMS, and network platform (if any) used to access the DBMS

Database Management COP4540, SCS, FIU ODBC Architecture Application Driver Manager Kinds of DBMS Drivers Communications Software Kinds of DBMSs Operating System Hard Disk Client Computer Server Computer Communications Software

Database Management COP4540, SCS, FIU Two Types of Drivers in ODBC Single tier driver. –Processes both ODBC calls and SQL requests. –Usually used to manipulate files, for example Xbase files. Multiple tier driver. –Only processes ODBC calls, and passes the SQL directly to the data source.

Database Management COP4540, SCS, FIU Conformance Levels ODBC conformance levels –Core API. –Level 1 API. –Level 2 API. SQL conformance levels –Minimum SQL grammar. –Core grammar. –Extended SQL grammar. Most products support at least Level 1 API and Core grammar SQL.

Database Management COP4540, SCS, FIU For Example(a comparison): Minimum SQL grammar Core SQL Grammar DDLCreate Table, Drop Table Alter table, create index, drop index, create view, drop view, grant and revoke DMLsimple Select, insert, update and delete full SELECT

Database Management COP4540, SCS, FIU Steps to Develop an ODBC Application Make sure you have the necessary ODBC driver installed Setup an ODBC data source (windows platform) –Start  Settings  Control Panel  ODBC data sources –Add a data source(set the database name, sometimes server name, user log in information, etc.) Develop the ODBC application

Database Management COP4540, SCS, FIU

Database Management COP4540, SCS, FIU Basic Application Steps SQLAllocEnv SQLAllocConnect SQLConnect SQLAllocStmt Process SQL Statements and Receive Result SQLFreeStmt SQLDisconnect SQLFreeConnect SQLFreeEnv

Database Management COP4540, SCS, FIU Connecting to a data source Initializing ODBC environment –Declare a variable of type HENV, such as: HENV henv1; –Calls SQLAllocEnv and passes it the address of the variable; –These steps should be performed only once by an application; SQLAllocEnv supports one or more connections to data sources. Allocating a connection handle –Declare a variable of type HDBC, such as: HDBC hdbc1; –Calls SQLAllocConnect and passes it the address of the variable. The driver allocates memory to store information about the connection and returns the connection handle in the variable. Connecting to a data source –Call SQLConnect with the following information data source name user id authentication string (password)

Database Management COP4540, SCS, FIU Executing SQL Statements Allocating a statement handle –Declare a variable of type HSTMT, such as HSTMT hstmt1; –Calls SQLAllocStmt and passes it the address of the variable and the connected hdbc with which to associate the statement. The driver allocates memory to store information about the statement, associates the statement handle with the hdbc, and returns the statement handle in the variable. Submit an SQL statement for execution in two ways –Prepared: Call SQLPrepare and then call SQLExecute –Direct: Call SQLExecDirect

Database Management COP4540, SCS, FIU Prepared Execution To prepare and execute an SQL statement, an application: 1. Calls SQLPrepare to prepare the statement 2. Sets the values of any statement parameters. 3. Retrieves information about the result set, if necessary 4. Calls SQLExecute to execute the statement 5. Repeats steps 2 through 4 as necessary

Database Management COP4540, SCS, FIU Direct Execution An application should execute a statement directly if both of the following are true: –The application will execute the statement only once –The application does not need information about the result set prior to execution To execute an SQL statement directly, an application: 1. Sets the values of any statement parameters. 2. Call SQLExecDirect to execute the statement.

Database Management COP4540, SCS, FIU Setting Parameter Values An SQL statement can contain parameter markers that indicate values that the driver retrieves from the application at execution time. For example: INSERT INTO EMPLOYEE (NAME, AGE, HIREDATE) VALUES (?, ?, ?) An application uses parameter markers instead of literal values if: –It needs to execute the same prepared statement several times with different parameter values. –The parameter values are not known when the statement is prepared. –The parameter values need to be converted from on data type to another. To set a parameter value, an application performs the following steps in any order –Calls SQLBindParameter to bind a storage location to a parameter marker and specify the data types of the storage location and the column associated with the parameter, as well as the precision and scale of the parameter. –Places the parameter’s value in the storage location.

Database Management COP4540, SCS, FIU Retrieving Results The steps an application takes to process a result set depends on what is known about it –Known result set The application knows the exact form of the SQL statement, and therefore the result set, at compile time. For example: SELECT SSN, NAME FROM EMP returns two specific columns. –Unknown result set The application does not know the exact form of the SQL statement, and therefore the result set, at compile time. For example SELECT * FROM EMP returns all currently defined columns in the EMP table. The application may not be able to predict the format of these result prior to execution.

Database Management COP4540, SCS, FIU Assigning Storage for Results (Binding) An application can assign storage for results before or after it executes an SQL statement. If an application prepares or executes the SQL statement first, it can inquire about the result set before it assigns storage for result. For example, if the result set is unknown, the application must retrieve the number of columns before it can assign storage for them. To associate storage for a column of data, an application calls SQLBindCol and passes it the following information: –Data type, address of output buffer, the length of output buffer, the address of the storage buffer in which to return the number of bytes of available data.

Database Management COP4540, SCS, FIU Fetching Result Set To retrieve a row of data from the result set, an application: 1. Calls SQLBindCol to bind the columns of the result set to storage location if it has not already done so. 2. Calls SQLFetch to move the next row in the result set and retrieve data for all bound colunms. SELECT Statement SQLNumResultCols SQLDescribeCol SQLBindCol SQLFetch More row? Finished Yes No

Database Management COP4540, SCS, FIU Terminating Connections Calls SQLFreeStmt to free resources associated with a statement handle. Calls SQLDisconnect to close the connection Calls SQLFreeConnect to free the connection handle and free all resources associated with the handle Calls SQLFreeEnv to free environment handle and free all resources associated with the handle.

Database Management COP4540, SCS, FIU Appendix: Some ODBC Functions(check MSDN Library for details) 1) RETCODE SQLAllocEnv( HENV FAR * OutputHandle); e.g. RETCODE retcode; HENV henv; retcode = SQLAllocEnv(&henv); 2)RETCODE SQLAllocConnect(HENV InputHandle, HDBC FAR* outputHandle); e.g. HDBC hdbc; retcode = SQLAllocConnect(henv, &hdbc ); 3)RETCODE SQLAllocStmt(HDBC InputHandle, HSTMT FAR* outputHandle ); e.g. HSTMT hstmt; retcode = SQLAllocStmt(hdbc, &hstmt );

Database Management COP4540, SCS, FIU ODBC Functions(cont.) 4) RETCODE SQLConnect( SQLHDBC ConnectionHandle, SQLCHAR * ServerName,  data source name SQLSMALLINT NameLength1, SQLCHAR * UserName, SQLSMALLINT NameLength2, SQLCHAR * Authentication, SQLSMALLINT NameLength3); e.g. Retcode = SQLConnect(hdbc, (SQLCHAR*) “Sales”, SQL_NTS, (SQLCHAR *)”JohnS”, SQL_NTS, (SQLCHAR *)”Sesame”, SQL_NTS); Note: Set nameLength1,nameLength2,nameLength3 to SQL_NTS if ServerName, UserName,Authentication are null-terminated strings.

Database Management COP4540, SCS, FIU ODBC Functions(cont.) 5) RETCODE SQLPrepare( HSTMT StatementHandle, SQLCHAR * StatementText, SQLINTEGER TextLength); 6) RETCODE SQLExecute( HSTMT StatementHandle); 7) RETCODE SQLExecDirect( HSTMT StatementHandle, SQLCHAR * StatementText, SQLINTEGER TextLength); Note: StatementText is the SQL text string(such as “select … from … where..”), and set TextLength to SQL_NTS if StatementText is a null-terminated string.

Database Management COP4540, SCS, FIU ODBC Functions(cont.) 8) RETCODE SQLDescribeCol( HSTMT StatementHandle, SQLSMALLINT ColumnNumber, SQLCHAR * ColumnName, SQLSMALLINT BufferLength, SQLSMALLINT * NameLengthPtr, SQLSMALLINT * DataTypePtr, SQLUINTEGER * ColumnSizePtr, SQLSMALLINT * DecimalDigitsPtr, SQLSMALLINT * NullablePtr); 9) RETCODE SQLNumResultCols( HSTMT StatementHandle, SQLSMALLINT * ColumnCountPtr);

Database Management COP4540, SCS, FIU ODBC Functions(cont.) 10) RETCODE SQLBindCol( HSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLPOINTER TargetValuePtr, SQLINTEGER BufferLength, SQLLEN * StrLen_or_Ind); 11) RETCODE SQLFetch( HSTMT StatementHandle); 12) RETCODE SQLFreeStmt( HSTMT StatementHandle, SQLUSMALLINT Option); e.g. SQLFreeStmt(hstmt,SQL_CLOSE);

Database Management COP4540, SCS, FIU ODBC Functions(cont.) 13) RETCODE SQLDisconnect(HDBC ConnectionHandle) e.g. SQLDisconnect(hdbc); 14) RETCODE SQLFreeConnect(HDBC ConnectionHandle) e.g. SQLFreeConnect (hdbc); 15) RETCODE SQLFreeEnv(HENV inputEnvHandle) e.g. SQLFreeEnv (henv);

Database Management COP4540, SCS, FIU //sample code #include int main (int argc, char** argv) {1HENV henv; 2HDBC hdbc; 3HSTMT hstmt; 4RETCODE retcode; 5SQLCHAR szName[50], szPhone[11]; 6 SQLINTEGER sCustID, cbName, cbCustID, cbPhone; 7/* Allocate environment handle */ 8retcode = SQLAllocEnv( &henv); 9if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ 10 /* Allocate connection handle */ 11 retcode = SQLAllocConnect(henv, &hdbc); 12 if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { 13 /* Connect to data source */ 14 retcode = SQLConnect(hdbc, (SQLCHAR*) "Sales", SQL_NTS,\ 15(SQLCHAR*) "JohnS", SQL_NTS, (SQLCHAR*) "Sesame", SQL_NTS); 16 if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ 17 /* Allocate statement handle */ 18 retcode = SQLAllocStmt(hdbc, &hstmt); 19 if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ 20/* Execute sql statement */

Database Management COP4540, SCS, FIU 21 retcode = SQLExecDirect(hstmt, (UCHAR*)"SELECT CUSTID, NAME, \ 22 PHONE FROM CUSTOMERS ", SQL_NTS); 23if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ 24 /* Bind columns 1, 2, and 3 */ 25 SQLBindCol(hstmt, 1, SQL_C_ULONG, &sCustID, 0, &cbCustID); 26 SQLBindCol(hstmt, 2, SQL_C_CHAR, szName, 50, &cbName); 27 SQLBindCol(hstmt, 3, SQL_C_CHAR, szPhone, 11, &cbPhone); 28 /* Fetch and print each row of data. On */ 29 /* an error, display a message and exit. */ 30 while (TRUE){ 31retcode = SQLFetch(hstmt); 32if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 33 show_error(); //to be defined by yourself 34if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 35 printf("%-*s %-5d %*s", 50-1, szName, sCustID, 11-1, szPhone); 36else break; 37 } 38SQLFreeStmt(hstmt,SQL_CLOSE); 39 } 40 SQLDisconnect(hdbc); 41 } 42 } 43 SQLFreeConnect(hdbc); 44 } 45 SQLFreeEnv(henv); 46 } 47 return 0; 48 }

Database Management COP4540, SCS, FIU The Difference between User DSN and System DSN User DSN is only visible to you (as a user) and is accessible only on the machine on which you set the data source.(i.e. if another user logs on this machine, he won’t be able to see this data source created by you). System DSN is visible to all the users on the machine on which you set the data source.