Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Database Access Through the Web.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
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.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Chapter 12 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 12 Sebesta: Programming the World Wide Web.
PHP and MySQL. Why Use a Database  Easy access to data  Simultaneous access by multiple users is handled properly  Security - easy to control access.
Objectives Connect to MySQL from PHP
Introduction to Structured Query Language (SQL)
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
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 14 Database Access through the Web. © 2006 Pearson Addison-Wesley. All rights reserved Relational Databases - A database is a collection.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
7/8/05MySQL David Lawrence1 David Lawrence, JLab An introduction for the novice.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Chapter 14 © 2005 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
Chapter 13 © 2013 by Pearson Relational Databases - A database is a collection of data organized to allow relatively easy access for retrievals,
INTERNET APPLICATION DEVELOPMENT For More visit:
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.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Chapter 13 © 2010 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Announcements Read JDBC Project Step 5, due Monday.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
Programming for WWW (ICE 1338) Lecture #11 Lecture #11 July 30, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Database System Concepts and Architecture.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
Chapter 13 © 2010 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Database Connectivity and Server-Side Scripting Chapter 12.
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,
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Chapter 13 © 2009 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
13.1 Relational Databases - A database is a collection of data organized to allow relatively easy access for retrievals, additions, and deletions - A relational.
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
JDBC.
Chapter 8 Working with Databases and MySQL
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Database Access Through the Web

13-2 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.1 Relational Databases A database stores data in a way allowing Efficient changes Efficient searching The relational model is currently the most popular model Data is stored in tables Columns are named Each row contains values for each column, though some values may be missing Rows are referred to as entities The primary key is one or more columns in a table whose value(s) uniquely identify each row Example, Corvettes table Primary key is an index number Each row represents a different vehicle Columns are important characteristics of the vehicles

13-3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.1 Multi-valued Attributes Each state can, potentially, be associated with several cars Each state could have important data, besides the name A separate State table is created with an index primary key Each entity in the Corvettes table refers to the state index That way, changes in information about a state would not have to be repeated on each line of the Corvettes table Each type of equipment could appear in many cars, each car could have many types of equipment A table describing equipment is set up A table giving the Corvette to Equpment relation is set up This just has pairs of id’s: Corvette-id and Equipment-id

13-4 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.1 Logical Data Model for Corvettes DB

13-5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 Structured Query Language SQL is a standardized language for manipulating and querying relational databases Although relational databases support SQL there may be some minor and some significant differences in the implementations SQL reserved words are not case sensitive However, some systems may treat names such as column names as case sensitive SQL commands may have extra white space, including new lines, added to improve readability Single quotes ‘ are used for literal strings

13-6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 The SELECT Command Used to query databases The command returns a result, a virtual table SELECT column-names FROM table-names [WHERE condition]; The result table has columns as named Rows are derived from the table named (see the Join discussion about multiple tables) The WHERE clause is optional The WHERE clause specifies constraints on the rows selected If * is used for the column names, all columns are selected

13-7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 Joins Task: list corvettes that have CD players This involves three tables: Corvettes, Equipment, Corvettes_Equipment A virtual table is constructed with combinations of rows from the two tables Corvettes and Equipment: a join of the three tables Or from all combinations of all three tables The WHERE clause selects which rows of the join are to be retained in the result

13-8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 A Query Using a Join SELECT Corvettes.Vette_id, Corvettes.Body_style, Corvettes.Miles, Corvettes.Year, Corvettes.State, Equipment.Equip FROM Corvettes, Equipment WHERE Corvettes.Vette_id = Corvettes_Equipment.Vette_id AND Corvettes_Equipment.Equip = Equipment.Equip_id AND Equipment.Equip = 'CD';

13-9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 The INSERT Command Inserts a new row into a table Syntax INSERT INTO table_name (column_name_1,..., column_name_n) VALUES (value_1, value_2,..., value_n); The values provided will be placed into the corresponding columns Columns not named will receive no value This will cause an error if the column was created with a NOT NULL contraint

13-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 The UPDATE Command Changes values in an existing row Syntax UPDATE table_name SET column_name_1 = value_1,... column_name_n = value_n WHERE column_name = value The WHERE clause identifies the row to be updated, probably by its primary key

13-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 The DELETE Command Removes one or more rows Syntax DELETE FROM table_name WHERE column_name = value; The WHERE clause determines which rows are deleted The sample syntax would probably be specifying a primary key value to identify one row However, the clause could be more general

13-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 The DROP Command Remove a table or database from the system A database system usually has several databases operating within it, essentially, named collections of tables Syntax DROP (TABLE|DATABASE)[IF EXISTS] name; The IF EXISTS clause may be included to prevent an error indication if the table or database doesn’t exist

13-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 The CREATE TABLE Command Create a table with specified columns, each column having a specified type of data and satisfying certain constraints Syntax CREATE TABLEtable_name( column_name_1 data_type constraints,... column_name_n data_type constraints); Most system support many data types Common types: INTEGER, REAL, DOUBLE, CHAR(length)

13-14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 Create Table Constraints The constraint NOT NULL causes an error to be raised if a row is inserted in which the corresponding column does not have a value The PRIMARY KEY constraint causes an error to be raised if a row is inserted in which the corresponding column has a value that equals the value in another row This can be applied to a group of several columns if the primary key is multi-column

13-15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.3 Client/Server Database Architecture Two-tier architecture Client connects to the database to get information Server or client performs computations and user interactions Problems with two-tier Servers getting smaller so client software getting more complex Keeping clients up to date difficult Three-tier architecture Web server with applications sits between a browser and the database system The web server accesses the database and carries out computations and deals with user interaction

13-16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.3 Microsoft Access Architectrue Open Database Connectivity (ODBC) An application programming interface (API) providing services to interact with a database One service is to submit SQL to be executed by the database system and to return results An ODBC driver manager, on the client, chooses the proper interface for a particular databas

13-17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.3 The Perl DBI/DBD Architecture Perl DBI is a module providing generic access to a database system including submitting SQL Programmers use the DBI in creating software Perl DBD comprises a variety of modules that interface between the DBI and particular database systems

13-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.2 PHP and Database Access There are modules available in PHP to access numerous different database systems

13-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.3 The Java JDBC Architecture JDBC is a standard API programmers use to access database systems Various JDBC drivers are available that interface with particular database systems A few database systems (HSQLDB and Derby) use JDBC as their native API

13-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.3 Database Access Architecture

13-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.4 The MySQL Database System Popular free databse system Most programming language libraries have some interface to MySQL Logging in to MySQL mysql [-h host] [-u username] [database_name] [-p] Starts an interactive shell sending commands the server host indicates the MySQL server host, defaults to local host username, if absent, defaults to login user name of the current user database_name, if present, selects a database for commands -p indicates a password is needed Connecting to a database is necessary Either on the initial command line use database_name

13-22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.4 MySQL Commands MySQL supports a large subset of standard SQL Other commands CREATE DATABSE database_name; SHOW TABLES; DESCRIBE table_name ;

13-23 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.5 Database Access with Perl and MySQL Import DBI into the running Perl instance Load the appropriate DBD module

13-24 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.5 The DBI Module Database handles are used as targets for operations The handle is an object so a reference is used to access methods Import the module use DBI; Create a database handle $dbh = DBI->connect( "DBI:driver_name:database_name" [, username][, password]); Use with ‘or die’ to handle possible errors

13-25 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.5 Submitting Commands with DBI Convenient to use the prepare method to compile SQL commands Returns a statement handle A statement handle supports the execute method The statement handle then receives the results of the operation NAME attribute is an array of column names fetchrow_array returns a reference to the next row of results Returns false if no further rows

13-26 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.5 An Example The access_cars.pl example allows the user to enter SQL commands and responds with the results The escapeHTML function is used to replace special characters with HTML entities

13-27 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.6 DB Access with PHP and MySQL

13-28 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.6 Problems with Special Characters HTML Characters Function htmlspecialchars replaces HTML special characters with entities Special PHP characters Values from HTTP requests may have characters, such as quotes, that interfere with PHP scripts Setting magic_quotes_gpc causes values from $_POST, $_GET and $_COOKIE to have backslashes put in front of certain characters This can cause problems since the backslash is actually part of the string Must be disabled for the example collecting SQL commands via a web page since the single quote character is important to preserve The stripslashes function removes the extra backslashes from a string

13-29 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.6 Connecting to MySQL The mysql_connect function First parameter is MySQL server host Second parameter is the MySQL username Third parameter is the password Returns false if it fails The mysql_close function Selecting a database with mysql_select

13-30 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.6 Requesting MySQL Operations The mysql_query function Takes a string parameter with an SQL query Returns a result object Functions that apply to the result object mysql_num_rows returns number of rows in result mysql_num_fields returns the number of fields (columns) in the result mysql_fetch_array returns an array with the next row of results Each array with a row from the result contains each field value indexed by position and by column name The array_values applied to this array has each value twice, once for each possible index

13-31 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.6 PHP/MySQL Example The example with carsdata.html and access_cars.php allows users to submit SQL commands that are executed against the Corvette database The two files could be combined, access_cars2php A hidden text field is used to hold a value that tells the script whether the script whether this is an initial request for the page or a second request with values from the form

13-32 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.7 JDBC and MySQL A DriverManager must be available for the database system to which connections are being made A driver can be assigned to the property jdbc.drivers The driver class can be referenced in the program The database is specified by a string of the form jdbc:subprotocol_name:more_info The subprotocol name is mysql for MySQL other_info might include the database name and a query string providing values such as a username or password A connection object is created Using the static getConnection method of DriverManager Getting a connection from a connection pool

13-33 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.7 JDBC Queries and Commands From a connection, create a Statement object Statement objects support two important methods Both take an SQL command as a string parameter executeQuery is for commands that return tabular results, such as SELECT executeUpdate is for other SQL commands An executeQuery returns a ResultSet object The ResultSet object has a cursor pointing to the next row in the result, initially pointing before the first row The next method moves the cursor to the next row, returns false if there is no next row Fields from the current result row are obtained with ‘get’ methods, one for each type of data: getString, getInteger for example The ‘get’ methods take a parameter, either an integer column index or a string column name

13-34 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.7 Metadata Metadata refers to information about a database and its tables, including the virtual tables returned from queries JDBC supplies methods to retrieve metadata from a database and form a query result From a database, table names, column names, column types, for example From a result set, column names and the number of columns, for example

13-35 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13.7 JDBC Example The example JDBCServlet.java implements a servlet that collects an SQL query from a user, applies it to the Corvettes database and displays the result