53

Database Connectivity and Server-Side Scripting Chapter 12

Embed Size (px)

DESCRIPTION

Learn how to… Define the technologies through which Web servers provide client access to server-side databases. Describe the three basic kinds of databases. List the steps in designing a relational database. Define a database connection object and SQL statement. Describe how server-side scripts make decisions based on dynamic database content.

Citation preview

Page 1: Database Connectivity and Server-Side Scripting Chapter 12
Page 2: Database Connectivity and Server-Side Scripting Chapter 12

Database Connectivity and Server-Side Scripting

Chapter 12

Page 3: Database Connectivity and Server-Side Scripting Chapter 12

Learn how to…

• Define the technologies through which Web servers provide client access to server-side databases.

• Describe the three basic kinds of databases.• List the steps in designing a relational database.• Define a database connection object and SQL

statement.• Describe how server-side scripts make decisions

based on dynamic database content.

Page 4: Database Connectivity and Server-Side Scripting Chapter 12

Providing Web Access to Server-Side

Databases

Page 5: Database Connectivity and Server-Side Scripting Chapter 12

Access to Databases

• The three-tier Web application model consists of:– User interface – Business object – Back office databases

• Web sites that provide access to server-side databases are sometimes called HTTP gateways.

Page 6: Database Connectivity and Server-Side Scripting Chapter 12

Access to Databases

• Data input by users in forms is sent to the business object.

• The business object can then retrieve, update, delete, or insert information in the database.

Page 7: Database Connectivity and Server-Side Scripting Chapter 12

CGI

• The common gateway interface (CGI) defines the manner in which a form’s data, cookies, and other kinds of information in a Web request get submitted to the program or script that processes and responds to the request.

• CGI scripts typically reside in the cgi-bin directory.– CGI scripts can be written in any programming

language.• Perl is a scripting language for short programs.• Bourne shell language is the original UNIX scripting

language.

Page 8: Database Connectivity and Server-Side Scripting Chapter 12

SAPI

• A Server Application Programming Interface (SAPI) is a collection of software components used by the business tier to obtain, process, and respond to a form’s data submitted by users.– SAPI loads once to process multiple requests.– CGI loads separately for each request.– Microsoft’s brand of SAPI is called Internet

SAPI (ISAPI).– Netscape’s is called Netscape SAPI (NSAPI).

Page 9: Database Connectivity and Server-Side Scripting Chapter 12

ASP

• Active Server Page (ASP) is a Microsoft ISAPI technology that enables Web developers to embed on a Web page server-side scripts written in either the JScript or VBScript programming language.– End users see only the result of the scripts,

and not the scripts.• See the next three slides for example.

Page 10: Database Connectivity and Server-Side Scripting Chapter 12

ASP Source Code

Page 11: Database Connectivity and Server-Side Scripting Chapter 12

Returned HTML Code

Page 12: Database Connectivity and Server-Side Scripting Chapter 12

What the User Sees

Page 13: Database Connectivity and Server-Side Scripting Chapter 12

Java

• Java is an object-oriented programming language that developers can use to create almost any kind of software.– Java code compiles into an intermediary

language that executes on any platform running the JVM.

• Sun created Java but battles with Microsoft over legalities.– Microsoft can create its own version of Java.– Microsoft will include Sun’s Java Virtual

Machine (JVM) in future versions of Windows.

Page 14: Database Connectivity and Server-Side Scripting Chapter 12

Java Servlets and JSP

• A servlet is a Java applet that runs on the server instead of in the browser; hence the name.

• Java Server Page (JSP) is an active Web page technology that is Sun’s equivalent to Microsoft’s ASP.– JSP runs in the JVM (Java Virtual Machine).– ASP runs in Microsoft’s ISAPI.

Page 15: Database Connectivity and Server-Side Scripting Chapter 12

PHP

• The PHP Hypertext Preprocessor (PHP) is another active page technology that enables the Web developer to include code on the page that will run on the server, which executes the code before sending the completed page to the user.– For more information, go to http://us3.php.net

Page 16: Database Connectivity and Server-Side Scripting Chapter 12

ColdFusion

• ColdFusion is an active scripting technology that uses its own proprietary scripting language, the ColdFusion Markup Language invented by Macromedia.– ColdFusion pages have the filename extension .cfm,

which stands for ColdFusion markup.– Visit www.macromedia.com/software/coldfusion for

more information.

Page 17: Database Connectivity and Server-Side Scripting Chapter 12

ASP.NET

• ASP.NET allows you to create code behind the Web page, on so-called code-behind pages that can be part of complete applications.

Page 18: Database Connectivity and Server-Side Scripting Chapter 12

Understanding Databases

Page 19: Database Connectivity and Server-Side Scripting Chapter 12

Flat File Database

• A flat file database keeps all the records in a single file.– It uses comma-delimited data or tab-

delimited data to separate entries.– It contains one data table containing rows

and columns.

Page 20: Database Connectivity and Server-Side Scripting Chapter 12

Relational Database

• A relational database contains multiple tables with primary key columns through which the records in one table can relate (i.e. key) to the data in another table.– The primary key is a column in which every entry

is unique.– A foreign key is a data field that relates the record

to the table in which that same column occurs as a primary key.

– The software that powers this type of database is called a relational database management system (RDBMS).

Page 21: Database Connectivity and Server-Side Scripting Chapter 12

Relational Database

Page 22: Database Connectivity and Server-Side Scripting Chapter 12

Object-Oriented Database

• In an object-oriented database, programmers write code in object-oriented languages to create complex data structures in which one data type can build upon, or inherit, properties from another.– The software is called an object-oriented

database management system (ODBMS).

Page 23: Database Connectivity and Server-Side Scripting Chapter 12

Designing Databases

• Here is an eight-step process to design a relational database:

1. Write a paragraph describing the purpose of the database.

2. Make a list of the tables that the database will comprise.

3. List the fields (data columns) each table will comprise.

Page 24: Database Connectivity and Server-Side Scripting Chapter 12

Designing Databases

4. Indicate the kind of data (data type) that each column will hold.

5. Indicate which data columns are primary keys.

6. Indicate which data columns are foreign keys and state the name of the table and data column in which each foreign key is a primary key.

Page 25: Database Connectivity and Server-Side Scripting Chapter 12

Designing Databases

7. Write a walkthrough to make sure you have not missed anything important.• Describe how the typical user will enter your site

and navigate its pages.• State what will happen in the database as the

user submits information.• Explain how the data will be used onscreen to

create pages whose content vary depending on the content of the database.

Page 26: Database Connectivity and Server-Side Scripting Chapter 12

Designing Databases

8. If you have any data tables with no keys, ask yourself whether the data really stands alone. If not, add the necessary foreign key column to key the data to the primary key column of the data table to which it relates.

Page 27: Database Connectivity and Server-Side Scripting Chapter 12

Normalizing

• Normalization is the process of separating a large table fulfilling multiple roles into smaller tables that increase efficiency by serving smaller roles that relate through keys to other tables in the database.– Normalized databases contain tables with

fewer columns and are more efficient.

Page 28: Database Connectivity and Server-Side Scripting Chapter 12

Indexing

• An index is a database column or collection of columns that the database engine uses to keep the data presorted in the order in which you plan to query it.– This increases performance.

Page 29: Database Connectivity and Server-Side Scripting Chapter 12

Design Principles

1. Each table should have a column containing a unique row ID number.

2. A table should store data for a single type of entity.

3. A table should avoid columns that are allowed to contain null values.

4. A table should not have repeating values.5. A field should have the same meaning in each

row of a table.6. Multiple instances of an entity should not be

represented as multiple columns.

Page 30: Database Connectivity and Server-Side Scripting Chapter 12

Accessing Server-Side Databases

Page 31: Database Connectivity and Server-Side Scripting Chapter 12

Three-Tier Model

Page 32: Database Connectivity and Server-Side Scripting Chapter 12

Major Databases

• The major brands of databases include Microsoft SQL Server, Oracle 9i, Borland Interbase, IBM DB2, and iPlanet Application Server.

• Standards define alternate means for connecting with databases.– ODBC– JDBC

Page 33: Database Connectivity and Server-Side Scripting Chapter 12

ODBC

• Open database connectivity (ODBC) is a Microsoft standard database access method.– The goal is to make it possible for any application

to access any database, regardless of its vendor.– ODBC uses database drivers that translate queries

and commands issued by the application into a format that the database can process.

– An ODBC driver exists for almost every brand of relational database.

Page 34: Database Connectivity and Server-Side Scripting Chapter 12

JDBC

• Java database connectivity (JDBC) enables Java programs to communicate with SQL-based database systems.

Page 35: Database Connectivity and Server-Side Scripting Chapter 12

OLE DB Data Sources

• Object linking and embedding database (OLE DB) enables Windows applications to open a wide variety of connections to data stored in all sources and not only in ODBC data sources.

Page 36: Database Connectivity and Server-Side Scripting Chapter 12

ADO

• ActiveX Data Objects (ADO) is an API that enables business objects to make connections and issue commands against different kinds of data sources.

Page 37: Database Connectivity and Server-Side Scripting Chapter 12

Creating an ADO Object

• The connection object is the ADO component that connects you to the database.– Once you have the connection open, you can

execute queries that insert, update, retrieve, or delete records from the database.

– The next slide shows the code to open a connection in two different ASP languages.

Page 38: Database Connectivity and Server-Side Scripting Chapter 12

Creating an ADO Object

Page 39: Database Connectivity and Server-Side Scripting Chapter 12

Sample Connection Strings

Page 40: Database Connectivity and Server-Side Scripting Chapter 12

SQL

• The Structured Query Language (SQL) is an international standard that defines the syntax for issuing commands that can query, update, insert, or delete records in a database.

• The SELECT command is used to retrieve records from a data table.

Page 41: Database Connectivity and Server-Side Scripting Chapter 12

SELECT Command

Page 42: Database Connectivity and Server-Side Scripting Chapter 12

SELECT Order

Page 43: Database Connectivity and Server-Side Scripting Chapter 12

SELECT Search

Page 44: Database Connectivity and Server-Side Scripting Chapter 12

Creating Data-Driven Web Pages

Page 45: Database Connectivity and Server-Side Scripting Chapter 12

Data-Drive Web Page

• A data-driven Web page is an HTML document in which part or all of the content derives from or depends on records or relationships in one or more databases.– Queries return data in a set of records called

a recordset.– The next few slides show some ASP code

that reads data from a recordset and makes decisions based on the status of this data.

Page 46: Database Connectivity and Server-Side Scripting Chapter 12

Preface

• The following script reads the names of registered users from a Users table and prints the names onscreen.

• Each comment line begins with the symbol //

Page 47: Database Connectivity and Server-Side Scripting Chapter 12

Jscript Version

Page 48: Database Connectivity and Server-Side Scripting Chapter 12

VBScript Version

Page 49: Database Connectivity and Server-Side Scripting Chapter 12

Using Logic

• The true power of data-driven Web pages comes from the script’s ability to make decisions based on the current contents of the database.– Suppose you want to check a user database

to decide whether to allow a user to log on to gain access to a Web page. What steps would be needed?

Page 50: Database Connectivity and Server-Side Scripting Chapter 12

Logical Steps for Login

1. Retrieve the user name and password from the incoming form data.

2. Open a connection to the username database.3. Issue an SQL command to query the database.4. Use an IF-THEN statement to make the

following decision based on the query result:– If the recordset is empty, this user is not valid, so you

deny access.– If the recordset contains the requested record, the

user is allowed in.

Page 51: Database Connectivity and Server-Side Scripting Chapter 12

JScript Version

Page 52: Database Connectivity and Server-Side Scripting Chapter 12

VBScript Version

Page 53: Database Connectivity and Server-Side Scripting Chapter 12