DT228/3 Web Development Databases. Querying a database: Partial info Search engines, on-line catalogues often need to allow user to search a database.

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

Web Database Programming Connecting Database to Web.
DT211/3 Internet Application Development JSP: Processing User input.
DT211/3 Internet Application Development
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
DT228/3 Web Development JSP: Directives and Scripting elements.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
DT211/3 Internet Application Development Databases.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
JSP Standard Tag Library
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Database Programming in Java Corresponds with Chapter 32, 33.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
SQL Review Tonga Institute of Higher Education. SQL Introduction SQL (Structured Query Language) a language that allows a developer to work with data.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
PHP meets MySQL.
1 Accelerated Web Development Course JavaScript and Client side programming Day 2 Rich Roth On The Net
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
1 Working with MS SQL Server Textbook Chapter 14.
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
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.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Chapter 8 Databases.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
In the Name Of Almighty Allah. Java Application Connection To Mysql Created by Hasibullah (Sahibzada) Kabul Computer Science Faculty Afghanistan.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Web Systems & Technologies
CS320 Web and Internet Programming Database Access with JSTL SQL
Chapter 5 Introduction to SQL.
JSP: Actions elements and JSTL
ATS Application Programming: Java Programming
Introduction to Web programming
JDBC.
ISC440: Web Programming 2 Server-side Scripting PHP 3
SQL Tutorial.
Web DB Programming: PHP
Tutorial 6 PHP & MySQL Li Xu
Introduction to Web programming
Presentation transcript:

DT228/3 Web Development Databases

Querying a database: Partial info Search engines, on-line catalogues often need to allow user to search a database using partial information e.g. First name begins with “A”, surname contains “mc” In SQL, use the LIKE keyword and wildcard characters (%, _) SQL Examples of partial searches Search * From Customers WHERE FirstName LIKE “Jon%” Search * from Customers WHERE LastName LIKE “Sm_th”

Querying a database: Partial info To implement in JSTL, need to just incorporate the LIKE keyword and the wildcard characters Example: < sql:query var = “nameResult” dataSource = “S{customerDb}” SELECT FirstName, LastName FROM Customers WHERE FirstName LIKE ? AND LastNAME LIKE ? / Will return all rows from Employee table where first name begins with firstName parameter and last name contains value of lastName parameter

In the example, saw that query result is put into a variable Processing result of a query < sql:query var = “nameResult” …. etc This variable will contains the results set (a set of rows) of the query This variable is of type Result ( javax.servlet.jsp.jstl.sql.Result class ) and has a number of properties that can be used to process the result When a query is run, need to be able to process the results e.g. to display rows back as a HTML table

Properties for Result interface rows rowCount columnNames limitByMaxRows rowsByIndex Processing result of a query Rows returned by the query (array of column names and values) Number of rows in the result Array of column names boolean. true if Result was truncated due to maxRows attribute Rows return by the query, as arrays (rows/columns) of indexes

Processing result of a query: example To display back a list of customer names in a HTML table as a result of previous query: < sql:query var = “nameResult” etc Want to display back on web page: First nameLast name JohnMurphy SylviaMcAllister TomJones etc Need to display column names, followed by row results… with appropriate HTML tags,, etc

First name / Last name / <c:forEach items = “${nameResult.rows}” var = “row” Processing result of a query: example Rows from nameResult Property “rows” is used Assign any variable name to access the columns

Query: Code sample – outputs all rows on a table to a html page <sql:setDataSource var="shopDb" scope = "session" driver = "sun.jdbc.odbc.JdbcOdbcDriver" url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\test\Labdb.mdb" />

<%-- Selects username and address details from user table for all rows --%> SELECT UserName, Address1 FROM Customers Sorry, there are no customers on the Here is the list of customers User name Address Query: Code sample – outputs all rows on a table

Query: Code sample – outputs all rows on a table

Updating a database To update database information, can use INSERT, UPDATE or DELETE Use the is used for any SQL statement that doesn’t return rows --  it is used for INSERT, UPDATE, DELETE specification on page 148 of jstl specificaiton document has three attributes: sql (= sql statement), var (for result) and scope Uses ? and to assign parameter values in same way as

Updating a database Reminder --- SQL statements are of the form:… INSERT INSERT INTO CUSTOMERS (customer_ID, name, phone) VALUES (5, “JOHN”, “875895”) UPDATE UPDATE CUSTOMERS SET NAME = “Robert” WHERE CUSTOMER_ID = 1 DELETE DELETE FROM CUSTOMERS WHERE CUSTOMER_ID = 2

INSERT To INSERT a new record on a table called Customers Example: INSERT INTO Customers (UserName, Password, FirstName, LastName,DateofBirth) VALUES (?, ?, ?, ?, ?) Table name Column names Column values

Place holders (?) for column values are filled up by request parameters (I.e. very unlikely to be hardcoded!) Request parameters are matched against the ?s in the order they appear Add in the datasource name into the statement INSERT

Note: setting a column that contains date or time – need to use a special tag action called because of JDBC API quirk (requires specific JDBC data and time types). Also, if database table contains columns that are numeric (e.g. INT, REAL etc), may need to use the action to convert a string request parameter Data conversion

UPDATE To update database row(s), simply use the UPDATE statement in the SQL statement example: to update password, firstname and lastname on the row(s) in the Customer table where userName matches that held in the username request parameter. UPDATE Customers SET Password = ?, FirstName = ? LastNAme = ? WHERE UserName = ?

DELETE To update database row(s), simply use the UPDATE statement in the SQL statement example: To delete all rows from the Customer table where the username matches that held in the userName request parameter. DELETE FROM Customers WHERE UserName = ?,

Example of a simple search application that allows a user to search for an employee using a form (search.html). The search is processed by find.jsp, and presented back by list.jsp** ** Separates presentation from business logic/request processing

Sample code: find.jsp <sql:setDataSource var="empDb" scope = "session" driver = "sun.jdbc.odbc.JdbcOdbcDriver" url = "jdbc:odbc:employeedb" /> SELECT * FROM Employees WHERE FirstName LIKE ? AND LastName LIKE ? AND Department LIKE ? ORDER BY LastName connect to the database with appropriate driver Results of query must be available during the full request (in order to send result on to another page) Forward controL to list.jsp to display results

Sample code – search.html Search in Employee Database Please enter information about the employee you're looking for. You can use partial information in all fields. First Name: Last Name: … ETC ETC….. rest of form Calls find.jsp

List.jsp displays the rows found. If not row found, displays an error

Sample code – list.jsp Search Result Sorry, no employees were found. The following employees were found: Last Name First Name Department The name of the query

Sample code – list.jsp The name used by the developer to access the contents of each row. The used as row.LastName… etc

Common errors Not specifying the datasource in your SQL command (get “database null” error in Apache when running) Using wrong number of parameters in SQL action When Specifying the dataSource name in a SQL statement, need to put it as an expression in ${ }... otherwise, it will take the exact name in the “ “ and use it. Get an error, no suitable driver if you do this wrong. Scope: Make sure the scope on the SQL statement is correct, so that the connection or SQL statement results carry through to the required pages.

Tags used

Database access using JSLT Usually used for simple applications Can use java beans or servlets for database access for more complex applications

Info on JSTL Sun tutorial. Chapter 14 is on JSTL at: ml Contains examples Also, Sun’s JSTL 1.0 tag specification on distrib. Good for definition of each tag. Poor on examples