Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 DT228/3 Web Development Databases

2 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”

3 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

4 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

5 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

6 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

7 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

8 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" />

9 <%-- 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

10 Query: Code sample – outputs all rows on a table

11 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

12 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

13 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

14 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

15 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

16 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 = ?

17 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 = ?,

18 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

19 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

20 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

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

22 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

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

24 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.

25 Tags used

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

27 Info on JSTL Sun tutorial. Chapter 14 is on JSTL at: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.ht ml Contains examples Also, Sun’s JSTL 1.0 tag specification on distrib. Good for definition of each tag. Poor on examples


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

Similar presentations


Ads by Google