Comp2513 Database and E-Commerce

40
Comp2513 Comp2513 Database and E- Database and E- Commerce Commerce Daniel L. Silver, Ph.D. Daniel L. Silver, Ph.D.

description

Comp2513 Database and E-Commerce. Daniel L. Silver, Ph.D. Objectives. To introduce the basic concepts of database and DBMS To describe the relational database model To discuss the Stuctured Query Language To define ODBC and JDBC To outline the role that database plays in E-Commerce - PowerPoint PPT Presentation

Transcript of Comp2513 Database and E-Commerce

Page 1: Comp2513 Database and E-Commerce

Comp2513Comp2513

Database and E-CommerceDatabase and E-Commerce

Daniel L. Silver, Ph.D.Daniel L. Silver, Ph.D.

Page 2: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 2

ObjectivesObjectives

To introduce the basic concepts of database To introduce the basic concepts of database and DBMSand DBMS

To describe the relational database model To describe the relational database model To discuss the Stuctured Query LanguageTo discuss the Stuctured Query Language To define ODBC and JDBCTo define ODBC and JDBC To outline the role that database plays in E-To outline the role that database plays in E-

CommerceCommerce Reference: portions of Chapter 6Reference: portions of Chapter 6

Page 3: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 3

OutlineOutline

Databases and DBMSDatabases and DBMS Relation databaseRelation database Structured Query Language (SQL)Structured Query Language (SQL) ODBC and JDBCODBC and JDBC

Page 4: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 4

What is a Database?What is a Database?

Database - A collection of data, structured Database - A collection of data, structured in a well defined format, accessed by in a well defined format, accessed by multiple applications using standard multiple applications using standard commands, ensuring integrity of accesscommands, ensuring integrity of access

A Database can contain many records and A Database can contain many records and the equivalent of many files each containing the equivalent of many files each containing many recordsmany records

Page 5: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 5

Database Management SystemDatabase Management System

DBMS – a collection of software that DBMS – a collection of software that facilitates and optimizes database I/O for facilitates and optimizes database I/O for applicationsapplications– Flexible access to data - independent of Flexible access to data - independent of

physical storagephysical storage– Rapid response to ad hoc queries Rapid response to ad hoc queries – Access by multiple applications in various Access by multiple applications in various

waysways– Ensures data integrityEnsures data integrity– Elimination of redundant dataElimination of redundant data

Page 6: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 6

Relational DatabaseRelational Database

Different databases have different ways of Different databases have different ways of organizing and representing data – referred organizing and representing data – referred to as a data modelto as a data model

The relational data model – data is placed in The relational data model – data is placed in tables where rows represent records and tables where rows represent records and columns represent fieldscolumns represent fields

Tables have no predefined relation to one Tables have no predefined relation to one another, instead data can be dynamically another, instead data can be dynamically relatedrelated

Page 7: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 7

Relation DBMS - RDBMSRelation DBMS - RDBMS

Major Commercial RDBMS vendors:Major Commercial RDBMS vendors:– IBM (DB2)IBM (DB2)– OracleOracle– MS (Access, SQL Server)MS (Access, SQL Server)– INGRES (RTI)INGRES (RTI)– InformixInformix

Freely Available RDBMS:Freely Available RDBMS:– PostgreSQLPostgreSQL– MySQLMySQL

Page 8: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 8

An Example: ERDAn Example: ERD

ERD = Entity Relationship Diagram

Page 9: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 9

An Example: Relational TablesAn Example: Relational Tables

pnum pdesc psd ped1 E911 Apr Nov2 CAPC Aug Dec

eid ename9902 Ritter, Tex0103 Nasium, Jim

aid ………………..apnum aeid alnum abd aed1 9902 Hfx1 May Oct1 0103 Hfx1 May Aug2 0103 Yrm2 Sep Oct

lnum laddressHfx1 1234 Barrington St.Yrm2 56 Front St.

PROJECT EMPLOYEE

ASSIGNMENTLOCATION

Page 10: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 10

SQL- Structured Query SQL- Structured Query LanguageLanguage

Data within a DBMS is manipulated via a Data within a DBMS is manipulated via a 4GL or by a specific application program 4GL or by a specific application program using a DBMS access languageusing a DBMS access language

SQL is data definition and manipulation SQL is data definition and manipulation langauge for relational databaseslangauge for relational databases

SQL has become an international standardSQL has become an international standard

Page 11: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 11

SQL BasicsSQL Basics

CREATE TABLE – creates a table and CREATE TABLE – creates a table and defines its fields (columns), e.g.:defines its fields (columns), e.g.:CREATE TABLE PROJECTCREATE TABLE PROJECT

(pnum integer NOT NULL.(pnum integer NOT NULL.

pdesc character NOT NULL.pdesc character NOT NULL.

PRIMARY KEY (pnum);PRIMARY KEY (pnum);

ALTER TABLE – delete or add fieldsALTER TABLE – delete or add fields DROP TABLE – delete an entire tableDROP TABLE – delete an entire table

Page 12: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 12

SQL BasicsSQL Basics

INSERT INTO – places values into a tableINSERT INTO – places values into a table UPDATE – changes values in a tableUPDATE – changes values in a table DELETE FROM – removes records in tableDELETE FROM – removes records in table SELECT – columns from a table, general SELECT – columns from a table, general

format:format:SELECT <colname>, <colname> FROM SELECT <colname>, <colname> FROM

<tablename> WHERE <condition> <tablename> WHERE <condition>

Page 13: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 13

An Example: Relational TablesAn Example: Relational Tables

pnum pdesc psd ped1 E911 Apr Nov2 CAPC Aug Dec

eid ename9902 Ritter, Tex0103 Nasium, Jim

aid ………………..apnum aeid alnum abd aed1 9902 Hfx1 May Oct1 0103 Hfx1 May Aug2 0103 Yrm2 Sep Oct

lnum laddressHfx1 1234 Barrington St.Yrm2 56 Front St.

PROJECT EMPLOYEE

ASSIGNMENTLOCATION

Page 14: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 14

SQL Select ExampleSQL Select Example

Find all projects in which Jim Nasium is Find all projects in which Jim Nasium is involved …involved …

Set qename = “Nasium, Jim”Set qename = “Nasium, Jim”SELECT pnum, pdescSELECT pnum, pdesc

FROM employee, assignment, projectFROM employee, assignment, projectWHERE ename = qename AND WHERE ename = qename AND

assignment.aeid = employee.eid ANDassignment.aeid = employee.eid ANDproject.pnum = assignment.apnumproject.pnum = assignment.apnum

Returns:Returns:pnum pdesc

1 E9112 CAPC

Page 15: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 15

Our E-Commerce Mall DBMSOur E-Commerce Mall DBMS

We are using PostgreSQL (postgres)We are using PostgreSQL (postgres) Freely available off the webFreely available off the web What is What is PostgreSQLPostgreSQL?? The The PostgreSQLPostgreSQL page. page.

Page 16: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 16

Our E-Commerce Mall DBOur E-Commerce Mall DB

Consists of 3 tables created and managed by Consists of 3 tables created and managed by PostgreSQL PostgreSQL

CategoriesCategories– category_id category_id intint– category_name category_name char 50char 50– descriptiondescription texttext– imageimage char 100char 100– parentparent int (null if a store)int (null if a store)

Page 17: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 17

Our E-Commerce Mall DBOur E-Commerce Mall DB

ProductsProducts– product_id product_id intint– product_name product_name char 50char 50– skusku char 50char 50– descriptiondescription texttext– imageimage char 100char 100– priceprice realreal– categorycategory intint

Page 18: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 18

Our E-Commerce Mall DBOur E-Commerce Mall DB

Product_categoryProduct_category– product_idproduct_id intint– category_idcategory_id intint

Used to display a product in more than one Used to display a product in more than one categorycategory

Page 19: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 19

SQL and Our E-Commere SQL and Our E-Commere Mall DatabaseMall Database

From index.jsp, a java bean is used to query From index.jsp, a java bean is used to query our E-Commerce Mall DB to get the our E-Commerce Mall DB to get the categories for a particular store:categories for a particular store:

(“SELECT category_id, category_name, description, (“SELECT category_id, category_name, description, image image FROM category WHERE parent = ?");FROM category WHERE parent = ?");

pstmt.setInt(1,getId());pstmt.setInt(1,getId());

Page 20: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 20

SQL and Our E-Commere SQL and Our E-Commere Mall DatabaseMall Database

DETAILS:DETAILS:From index.jsp, the following use of a java bean gets the categories of a store:From index.jsp, the following use of a java bean gets the categories of a store:List categories = store.getCategories();List categories = store.getCategories();

The getCategories method in turn uses another bean to get all categories:The getCategories method in turn uses another bean to get all categories:category.getChildren()category.getChildren()

The getChildren method makes the SQL query via a JDBC request:The getChildren method makes the SQL query via a JDBC request:conn = StoreDatabase.getConnection();conn = StoreDatabase.getConnection();pstmt = conn.prepareStatement("SELECT category_id,category_name,description,image pstmt = conn.prepareStatement("SELECT category_id,category_name,description,image

FROM category WHERE parent = ?");FROM category WHERE parent = ?");pstmt.setInt(1,getId());pstmt.setInt(1,getId());rs = pstmt.executeQuery();rs = pstmt.executeQuery();

Page 21: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 21

SQL and Our E-Commere SQL and Our E-Commere Mall DatabaseMall Database

From category.jsp, a java bean is used to From category.jsp, a java bean is used to query our E-Commerce Mall DB to get query our E-Commerce Mall DB to get the products of a particular category:the products of a particular category:

("SELECT product_id, product_name, sku,description, image,price ("SELECT product_id, product_name, sku,description, image,price FROM product WHERE category = ?" + FROM product WHERE category = ?" +

" OR EXISTS (SELECT * FROM product_category " OR EXISTS (SELECT * FROM product_category

WHERE product_category.category_id = ?" + WHERE product_category.category_id = ?" +

" AND product_category.product_id = product.product_id)"); " AND product_category.product_id = product.product_id)");

pstmt.setInt(1,category.getId()); pstmt.setInt(2,category.getId());pstmt.setInt(1,category.getId()); pstmt.setInt(2,category.getId());

Page 22: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 22

SQL and Our E-Commere SQL and Our E-Commere Mall DatabaseMall Database

DETAILS:DETAILS:From category.jsp, a java bean is used to get the categories of a store:From category.jsp, a java bean is used to get the categories of a store:List products = Product.getProducts(category);List products = Product.getProducts(category);

The getProducts method makes the SQL query via a JDBC request:The getProducts method makes the SQL query via a JDBC request:conn = StoreDatabase.getConnection();conn = StoreDatabase.getConnection();pstmt = conn.prepareStatement("SELECT pstmt = conn.prepareStatement("SELECT

product_id,product_name,sku,description, image,price FROM product_id,product_name,sku,description, image,price FROM product WHERE category = ?" +product WHERE category = ?" +

" OR EXISTS (SELECT * FROM product_category " OR EXISTS (SELECT * FROM product_category WHERE product_category.category_id = ?" +WHERE product_category.category_id = ?" + " AND product_category.product_id = product.product_id)");" AND product_category.product_id = product.product_id)");pstmt.setInt(1,category.getId());pstmt.setInt(1,category.getId());pstmt.setInt(2,category.getId());pstmt.setInt(2,category.getId());rs = pstmt.executeQuery(); rs = pstmt.executeQuery();

Page 23: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 23

ODBCODBC

Open Database Connectivity is a widely Open Database Connectivity is a widely accepted application programming interface accepted application programming interface (API) for database access developed by a (API) for database access developed by a consortium led by MicroSoftconsortium led by MicroSoft

ODBC is a combination of ODBC API ODBC is a combination of ODBC API function calls and the SQL languagefunction calls and the SQL language

Page 24: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 24

ODBCODBC Originally, a proprietary language was used Originally, a proprietary language was used

to talk to each DBMSto talk to each DBMS A program required unique code to interact A program required unique code to interact

with Access, DB2 and Oracle databaseswith Access, DB2 and Oracle databases

Program with3 different setsof API calls

and SQLOracle DB

OracleDBMS

DB2 DBDB2

DBMS

Access DBAccessDBMS

Page 25: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 25

ODBCODBC ODBC abstracts away specific DBMSODBC abstracts away specific DBMS The application issues ODBS API callsThe application issues ODBS API calls ODBC Manager interfaces with the target DBMSODBC Manager interfaces with the target DBMS ODBC driver must be installed for each DBMSODBC driver must be installed for each DBMS

ODBCManager

Oracle DBOracleDBMS

DB2 DBDB2

DBMS

Access DBAccessDBMS

Program withODBC API

calls and SQL

Page 26: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 26

JDBCJDBC

Java Database ConnectivityJava Database Connectivity JDBC is a trademark of SUN MicrosystemsJDBC is a trademark of SUN Microsystems Standard API that lets you access virtually Standard API that lets you access virtually

any tabular data source from Java programsany tabular data source from Java programs– relational databases, flat files, spreadsheet filesrelational databases, flat files, spreadsheet files

JDBC builds on and reinforces the style and JDBC builds on and reinforces the style and virtues of Java (easy to use)virtues of Java (easy to use)

Page 27: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 27

JDBC Facilitates DB I/OJDBC Facilitates DB I/O

Connect to DB and establish a session:Connect to DB and establish a session:conn = StoreDatabase.getConnection();conn = StoreDatabase.getConnection(); … … within getConnection …within getConnection …

Connection conn = Connection conn = DriverManager.getConnection(“jdbc:postgresql://raven.acadiaDriverManager.getConnection(“jdbc:postgresql://raven.acadiau.ca/u.ca/2513DB”, “storexx_uid”, “storexx_pwd”);2513DB”, “storexx_uid”, “storexx_pwd”);

Creates an object for issuing SQL statements (commands) Creates an object for issuing SQL statements (commands) to the connection:to the connection:pstmt = conn.prepareStatement("SELECT pstmt = conn.prepareStatement("SELECT category_id,category_name, description,image FROM category_id,category_name, description,image FROM category WHERE parent = ?");category WHERE parent = ?");

Two JDBC statement methods:Two JDBC statement methods:– executeQuery() – used for issuing SELECT queriesexecuteQuery() – used for issuing SELECT queries– executeUpdate() – used for issuing DB inserts, updates and deletesexecuteUpdate() – used for issuing DB inserts, updates and deletes

(The following examples are taken from IndexServlet.java)

Page 28: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 28

JDBC Facilitates DB I/OJDBC Facilitates DB I/O The executeQuery() method returns a ResultSet object that contains The executeQuery() method returns a ResultSet object that contains

the results of the query operation on the DB:the results of the query operation on the DB:conn = StoreDatabase.getConnection();conn = StoreDatabase.getConnection();

pstmt = conn.prepareStatement("SELECT category_id,category_name,pstmt = conn.prepareStatement("SELECT category_id,category_name,description, image FROM category WHERE parent = ?");description, image FROM category WHERE parent = ?");

pstmt.setInt(1,getId());pstmt.setInt(1,getId()); rs = pstmt.executeQuery();rs = pstmt.executeQuery();

This “rs” object can be explored row by row: This “rs” object can be explored row by row: while (rs.next()) {while (rs.next()) { int childId = rs.getInt("category_id");int childId = rs.getInt("category_id"); String childName = rs.getString("category_name");String childName = rs.getString("category_name"); String childDesc = rs.getString("description");String childDesc = rs.getString("description"); String childImage = rs.getString("image");String childImage = rs.getString("image");

……}}

(The following examples are taken from IndexServlet.java)

Page 29: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 29

JDBC Facilitates DB I/OJDBC Facilitates DB I/ODETAILS of getChildren:DETAILS of getChildren:List children = new Vector();List children = new Vector(); Connection conn = null;Connection conn = null; PreparedStatement pstmt = null;PreparedStatement pstmt = null; ResultSet rs = null;ResultSet rs = null; try {try { conn = StoreDatabase.getConnection();conn = StoreDatabase.getConnection(); pstmt = conn.prepareStatement("SELECT pstmt = conn.prepareStatement("SELECT

category_id,category_name,description,image FROM category WHERE parent = ?");category_id,category_name,description,image FROM category WHERE parent = ?"); pstmt.setInt(1,getId());pstmt.setInt(1,getId()); rs = pstmt.executeQuery();rs = pstmt.executeQuery(); while (rs.next()) {while (rs.next()) { int childId = rs.getInt("category_id");int childId = rs.getInt("category_id"); String childName = rs.getString("category_name");String childName = rs.getString("category_name"); String childDesc = rs.getString("description");String childDesc = rs.getString("description"); String childImage = rs.getString("image");String childImage = rs.getString("image"); Category child = new Category(childId,childName,childDesc,childImage,this);Category child = new Category(childId,childName,childDesc,childImage,this); children.add(child);children.add(child); child.isNew = false;child.isNew = false; }} … … return children;return children;

Page 30: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 30

JSPs simplify JDBC (WS/DB2)JSPs simplify JDBC (WS/DB2) Java Server Pages can make use of pre-written code to Java Server Pages can make use of pre-written code to

facilitate DB accessfacilitate DB access To connect:To connect:

<jsp:dbconnect id="conn" url="jdbc:db2:demomall" <jsp:dbconnect id="conn" url="jdbc:db2:demomall" driver="COM.ibm.db2.jdbc.app.DB2Driver" driver="COM.ibm.db2.jdbc.app.DB2Driver"

userid="db2user" passwd="db2pass">userid="db2user" passwd="db2pass"></jsp:dbconnect></jsp:dbconnect>

To execute a query:To execute a query:<jsp:dbquery connection="conn" id="catalog"><jsp:dbquery connection="conn" id="catalog">

SELECT …SELECT …</jsp:dbquery></jsp:dbquery>

To get the result (in this case a category name):To get the result (in this case a category name):<jsp:getProperty name="catalog" property=“name" /><jsp:getProperty name="catalog" property=“name" />

Page 31: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 31

JSPs simplify JDBCJSPs simplify JDBC

Portion of code within index.jsp that accesses Portion of code within index.jsp that accesses the Mall DB when displaying the categories the Mall DB when displaying the categories at the top of the page:at the top of the page:

<a class="catLink" <a class="catLink"

href="category.jsp?id=<jsp:getProperty name="category" href="category.jsp?id=<jsp:getProperty name="category" property="id" />" property="id" />"

onMouseOver="hiliteCell(<%=i%>)“onMouseOver="hiliteCell(<%=i%>)“

onMouseOut="unhiliteCell(<%=i%>)">onMouseOut="unhiliteCell(<%=i%>)">

<jsp:getProperty name="category" property="name" /><jsp:getProperty name="category" property="name" />

</a></a>

Page 32: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 32

JSPs simplify JDBCJSPs simplify JDBCDETAILS:DETAILS:<%<% int spaceWidth = 600 - (categories.size() * 110);int spaceWidth = 600 - (categories.size() * 110); int i = 0;int i = 0; for (Iterator it = categories.iterator(); it.hasNext();) {for (Iterator it = categories.iterator(); it.hasNext();) { Category c = (Category) it.next();Category c = (Category) it.next(); pageContext.setAttribute("category",c);pageContext.setAttribute("category",c);%>%> <td width="110"><table border="0" cellspacing="0" cellpadding="0" <td width="110"><table border="0" cellspacing="0" cellpadding="0"

width="110"><tr><td align="center"><font face="Verdana, Arial, width="110"><tr><td align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Helvetica, sans-serif" size="2"><a class="catLink" <a class="catLink" href="category.jsp?id=<jsp:getProperty name="category" property="id" href="category.jsp?id=<jsp:getProperty name="category" property="id" />" onMouseOver="hiliteCell(<%=i%>)" onMouseOut="unhiliteCell(<%=i/>" onMouseOver="hiliteCell(<%=i%>)" onMouseOut="unhiliteCell(<%=i%>)"><jsp:getProperty name="category" property="name" /></a>%>)"><jsp:getProperty name="category" property="name" /></a></font></td></tr></table></td></font></td></tr></table></td>

<%<% i++;i++; }}%>%>

Page 33: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 33

The Role of Database The Role of Database in E-Commercein E-Commerce

Database is used within E-Commerce to provide Database is used within E-Commerce to provide dynamic ad hoc information on-demand to usersdynamic ad hoc information on-demand to users

Store administrators use databases to set up categories Store administrators use databases to set up categories and products in a secure and reliable mannerand products in a secure and reliable manner

E-Commerce applications can be written with only the E-Commerce applications can be written with only the logical structure of data and not its physical storagelogical structure of data and not its physical storage

The database holds the content of a page and it also can The database holds the content of a page and it also can hold the presentation of that content such that the same hold the presentation of that content such that the same data can be presented in different ways to different data can be presented in different ways to different customerscustomers

Page 34: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 34

Major Architectural Components Major Architectural Components of the Webof the Web

InternetInternet

Browser

DatabaseServer

Client 1

Server A

Server BBank

Server

URL

HTTPTCP/IP

Browser

Client 2 HTTPServer

App.Server

index.html

BankServer

Dedicated

prog.class

Page 35: Comp2513 Database and E-Commerce

THE ENDTHE END

[email protected]@acadiau.ca

Page 36: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 36

SQL and WebSphereSQL and WebSphere

From index.jsp, the following gets the various From index.jsp, the following gets the various category names (cgname) and reference numbers category names (cgname) and reference numbers for a specified merchant (cgmenbr):for a specified merchant (cgmenbr):

SELECT cgryrel.crccgnbr, category.cgname, category.cgrfnbrSELECT cgryrel.crccgnbr, category.cgname, category.cgrfnbr

FROM cgryrel, categoryFROM cgryrel, category

WHERE crmenbr = <%= request.getParameter("cgmenbr") %> ANDWHERE crmenbr = <%= request.getParameter("cgmenbr") %> AND

(cgryrel.crpcgnbr IS NULL) AND(cgryrel.crpcgnbr IS NULL) AND

category.cgmenbr = <%= request.getParameter("cgmenbr") %> ANDcategory.cgmenbr = <%= request.getParameter("cgmenbr") %> AND

category.cgrfnbr = crccgnbrcategory.cgrfnbr = crccgnbr

Page 37: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 37

SQL and WebSphereSQL and WebSphere

From index.jsp, the following gets the various From index.jsp, the following gets the various category names (cgname) and reference numbers category names (cgname) and reference numbers for a specified merchant (cgmenbr):for a specified merchant (cgmenbr):

SELECT cgryrel.crccgnbr, category.cgname, category.cgrfnbrSELECT cgryrel.crccgnbr, category.cgname, category.cgrfnbr

FROM cgryrel, categoryFROM cgryrel, category

WHERE crmenbr = <%= request.getParameter("cgmenbr") %> ANDWHERE crmenbr = <%= request.getParameter("cgmenbr") %> AND

(cgryrel.crpcgnbr IS NULL) AND(cgryrel.crpcgnbr IS NULL) AND

category.cgmenbr = <%= request.getParameter("cgmenbr") %> ANDcategory.cgmenbr = <%= request.getParameter("cgmenbr") %> AND

category.cgrfnbr = crccgnbrcategory.cgrfnbr = crccgnbr

Page 38: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 38

SQL and WebSphereSQL and WebSphere

From product.jsp, the following gets the various From product.jsp, the following gets the various

product category names (cgname) for specified product category names (cgname) for specified merchant (cgmenbr) and category reference number merchant (cgmenbr) and category reference number

(cgrfnbr):(cgrfnbr):

SELECT category.cgnameSELECT category.cgname

FROM categoryFROM category

WHERE cgmenbr = <%=request.getParameter("cgmenbr") %> ANDWHERE cgmenbr = <%=request.getParameter("cgmenbr") %> AND

cgrfnbr = <%= request.getParameter("cgrfnbr") %>cgrfnbr = <%= request.getParameter("cgrfnbr") %>

Page 39: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 39

SQL and WebSphereSQL and WebSphere

From productDisplay.jsp, the following gets various From productDisplay.jsp, the following gets various product values for display for specified merchant product values for display for specified merchant (prmenbr) and product number (prrfnbr):(prmenbr) and product number (prrfnbr):

SELECT product.prsdesc, product.prnbr, product.prfull, SELECT product.prsdesc, product.prnbr, product.prfull, product.prthmb,prodprcs.ppprc, prodprcs.ppcur, product.prthmb,prodprcs.ppprc, prodprcs.ppcur, product.prldesc1,product.prldesc2, product.prldesc3, product.prldesc1,product.prldesc2, product.prldesc3, product.prwght, product.prwmeas,product.prwght, product.prwmeas,product.prheight,product.prlngth, product.prwidth, product.prheight,product.prlngth, product.prwidth, product.prsmeasproduct.prsmeas

FROM product, prodprcsFROM product, prodprcs WHERE ppprnbr = prrfnbr ANDWHERE ppprnbr = prrfnbr AND

prmenbr = <%= request.getParameter("prmenbr") %> ANDprmenbr = <%= request.getParameter("prmenbr") %> AND prrfnbr = <%= request.getParameter("prrfnbr") %>prrfnbr = <%= request.getParameter("prrfnbr") %>

Page 40: Comp2513 Database and E-Commerce

2002 Daniel L. Silver 40

JDBC Facilitates WS DB I/OJDBC Facilitates WS DB I/O

Connect to DB and establish a session:Connect to DB and establish a session:Connection myConnection = Connection myConnection =

DriverManager.getConnection("jdbc:db2:demomall", DriverManager.getConnection("jdbc:db2:demomall", "db2user", "db2pass");"db2user", "db2pass");

Create an object for issuing SQL statements Create an object for issuing SQL statements (commands):(commands):Statement statement = myConnection.createStatement();Statement statement = myConnection.createStatement();

Two JDBC statement methods:Two JDBC statement methods:– executeQuery() – used for issuing SELECT queriesexecuteQuery() – used for issuing SELECT queries– executeUpdate() – used for issuing DB inserts, updates executeUpdate() – used for issuing DB inserts, updates

and deletesand deletes

(The following examples are taken from IndexServlet.java)