Group A: Assignment No 2 Regularity (2) Performance(5) Oral(3) Total (10) Dated Sign Title of Assignment: SQL Joins in MySQL using 3-tier Problem Definition: DBMS using connections ( Client-application server-data sever, three tier) Oracle/MySQL (ODBC/JDBC), SQL Joints, prompt. Prerequisite: Basic DDL and DML queries and JDBC. Objective: To learn and understand the concept and queries of SQL joints using MYSQL using 3-tier. New Concepts: 2.1 Introduction to Joins An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. Before we continue with examples, we will list the types of JOIN you can use, and the differences between them. JOIN: Return rows when there is at least one match in both tables LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table FULL JOIN: Return rows when there is a match in one of the tables SQL JOIN Syntax FROM table_name1,table_name2 WHERE table_name1.column_name=table_name2.column_name
SQL JOIN EXAMPLE The "Persons" table: P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger The "Orders" table: O_Id OrderNo P_Id 1 77895 3 2 44678 3 3 22456 1 4 24562 1 5 34764 15 Now we want to list all the persons with any orders. We use the following SELECT statement: FROM Persons, Orders WHERE Persons.P_Id=Orders.P_Id SQL LEFT JOIN KEYWORD The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). SQL LEFT JOIN Syntax FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name SQL LEFT JOIN EXAMPLE FROM Persons LEFT JOIN Orders ON Persons.P_Id=Orders.P_Id
Svendson Tove The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table (Orders). SQL RIGHT JOIN KEYWORD The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1). SQL RIGHT JOIN Syntax FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name SQL RIGHT JOIN EXAMPLE: Now we want to list all the orders with containing persons - if any, from the tables above. FROM Persons RIGHT JOIN Orders ON Persons.P_Id=Orders.P_Id 34764 The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons). SQL FULL JOIN KEYWORD The FULL JOIN keyword return rows when there is a match in one of the tables. SQL FULL JOIN Syntax FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name
SQL FULL JOIN EXAMPLE Now we want to list all the persons and their orders, and all the orders with their persons. FROM Persons FULL JOIN Orders ON Persons.P_Id=Orders.P_Id Svendson Tove 34764 The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows from the right table (Orders). If there are rows in "Persons" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Persons", those rows will be listed as well. 2.2 THREE-TIER ARCHITECTURE In software engineering, multi-tier architecture is a client server architecture in which presentation, application processing, and data management functions are logically separated. The three tiers in a three-tier architecture are: 1. PRESENTATION TIER: Occupies the top level and displays information related to services available on a website. This tier communicates with other tiers by sending results to the browser and other tiers in the network. 2. APPLICATION TIER: Also called the middle tier, logic tier, business logic or logic tier, this tier is pulled from the presentation tier. It controls application functionality by performing detailed processing. 3. DATA TIER: Houses database servers where information is stored and retrieved. Data in this tier is kept independent of application servers or business logic. Figure: Example of 3-tier architecture using JSP
2.3 JSP (Java Server Pages): Java Server Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>. Using JSP, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically. 2.3.1 Creating a simple JSP page To create the first JSP page, write some html code as given below, and save it by.jsp extension. Let's see the simple example of JSP, here we are using the scriptlet tag to put java code in the JSP page. <html> <body> <% out.print(2*5); %> </body> </html> It will print 10 on the browser. 2.3.2 Scripting Element The scripting elements provide the ability to insert java code inside the jsp. There are three types of scripting elements: Scriptlet tag JSP Scriptlet tag Expression tag Declaration tag Syntax is as follows: <% java source code %> Simple Example of JSP scriptlet tag to display Welcome Message <html> <body> <% out.print("welcome to jsp"); %> </body> </html>
JSP declaration tag The JSP declaration tag is used to declare fields and methods. The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet. So it doesn't get memory at each request. Syntax of JSP declaration tag <%! statement %> Example of JSP declaration tag that declares field In this example of JSP declaration tag, we are declaring the field and printing the value of the declared field using the jsp expression tag. <%! int data=50; %> <%= "Value of the variable is:"+data %> JSP expression tag The code placed within expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method. Syntax of JSP expression tag <%= statement %> Example of JSP expression tag In this example of jsp expression tag, we are simply displaying a welcome message. <html> <body> <%= "welcome to jsp" %> </body> </html> 2.3.3 Creating JSP in Eclipse IDE with Tomcat server 1) Create the dynamic web project For creating a dynamic web project click on File Menu -> New -> dynamic web project -> write your project name e.g. first -> Finish.
2) Create the JSP file in eclipse IDE For creating a jsp file explore the project by clicking the + icon -> right click on WebContent -> New -> jsp -> write your jsp file name e.g. index -> next -> Finish. 3) Start the server and deploy the project: For starting the server and deploying the project in one step Right click on your project -> Run As -> Run on Server -> choose tomcat server -> next -> addall -> finish. 2.4 Assignment questions 1. What is importance of SQL join? What are different types of joins? 2. What is 1- tier, 2-tier and 3-tier Architecture? 3. What are advantages of 3-tier architecture over 2-tier? 4. What is JSP? 5. What is java beans?