Mark Dixon Page 1 5 – Persistent data storage: relational databases.

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 16 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 03 – Database Design: Multiple Tables.
Java, Access, SQL, HTML. Three-tier architecture involves: Client - Browser Server - Tomcat Database - Access - Server-side language - JSP could just.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 SOFT – Module Introduction.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
MIS2502: Data Analytics MySQL and SQL Workbench David Schuff
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 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
CSCI 6962: Server-side Design and Programming
Mark Dixon 1 20 – Persistent data storage: relational databases and ADO.
JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.
Interacting With Data Databases.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Database Programming in Java Corresponds with Chapter 32, 33.
Concepts of Database Management Seventh Edition
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
M Dixon 1 Soft – Module Introduction and HTML.
Mark Dixon Page 1 3 – Web applications: Server-side code (JSP)
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Mark Dixon Page 1 18 – Web applications: Server-side code (PhP)
Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL.
Mark Dixon 1 05 – JSP Databases: Multiple Tables.
Mark Dixon 1 06 – JSP Databases: Multiple Tables.
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.
Concepts of Database Management Seventh Edition
Chapter 17 Creating a Database.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
Mark Dixon Page 1 Soft – Module Introduction and HTML.
Database Management Systems.  Database management system (DBMS)  Store large collections of data  Organize the data  Becomes a data storage system.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Mark Dixon 1 21 – Databases: Multiple Tables. Mark Dixon 2 Questions: Databases How many records are in the following table? How many fields does the.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data.
When I want to work with SQL, I start off as if I am doing a regular query.
Mark Dixon 1 08 – In-class test. Mark Dixon 2 In-class Test Date: Wednesday 16 th November 2011 Time: 09:05-10:55 Location: SMB109 Type: Individual 40%
M Dixon 1 Tech HTML. M Dixon 2 Admin Attendance Register: –log in to your profile.
Introduction to Morpho RCN Workshop Samantha Romanello Long Term Ecological Research University of New Mexico.
AVCE ICT – Unit 7 - Programming Session 16 – Database and VB.
MS-Access XP Lesson 4. Modifying Queries 1.Select query in queries 2.Click design button or Right click on query and click design view 3.Change query.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Mark Dixon 1 07 – In-class test. Mark Dixon 2 In-class Test Date: Monday 5 th November 2012 Time: 11:05-120:55 Location: SMB109 Type: Individual 40% of.
Mark Dixon, SoCCE SOFT 131Page 1 14 – Databases: Multiple Tables.
CHAPTER 7 LESSON C Creating Database Reports. Lesson C Objectives  Display image data in a report  Manually create queries and data links  Create summary.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
JDBC Database Management Database connectivity
18 – Databases: Structured Query Language
17 – Persistent data storage: relational databases and ADO
19 – Databases: Multiple Tables
Presentation transcript:

Mark Dixon Page 1 5 – Persistent data storage: relational databases

Mark Dixon Page 2 Questions: Session variables Write a line of code to put 31 into a session variable called score. Write code that adds 1 to a variable called g, when a session variable called i is over 25. if (session.getAttribute("i") > 25){ g = g + 1; } Session.setAttribute("score", 31);

Mark Dixon Page 3 Admin: SQL Book Gennick J (2006) SQL Pocket Guide (2 nd edition). O'Reilly. ISBN:

Mark Dixon Page 4 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in using relational databases for persistent data storage Objectives, by end of this week’s sessions, you should be able to: –create a relational database table –create a web page (JSP) that displays data from a single table in a database –use SQL in your programs to create more complex record-sets

Mark Dixon Page 5 Persistent Data Storage So far –programs (web-pages) lose data when closed Not realistic –typically data stored to persistent storage device (e.g. hard disk, key drive, floppy disk, CD-RW) Use either –flat files –database (relational, or object oriented)

Mark Dixon Page 6 Example: People (analysis) SPECIFICATION User Requirements –need to have access to people's details Software Requirements –Functional: –Display list of people from a database –Non-functional should be viewable anywhere in the world

Mark Dixon Page 7 Record Field Example: People (Database) IDSurnameForenamesPhoneGender 1DixonMark Male 2SmithJohn Male 3JonesSally Female Information organised into –tables (e.g. person) –fields (e.g. phone) –records (e.g. 1 Dixon Mark …) Person

Mark Dixon Page 8 How many fields? How many records? Questions: Music (Database) Track TitleArtist NameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track 9 3

Mark Dixon Page 9 DBMS Database Management Systems (DBMS) provide facilities for: –creating and changing databases add/remove records add/remove fields add/remove data –For example: Microsoft Access dBase Borland Paradox MySQL Microsoft SQL Server Oracle home/small business large scale

Mark Dixon Page 10 MS Access Music database

Mark Dixon Page 11 Start Menu Control Panel Administrative Tools Data Sources (ODBC) System DSN Add How to: Create Data Source 64bit Problem – No MS Access driver Solution: use C:\Windows\SysWOW64\odbcad32.exe

Mark Dixon Page 12 How to: Create Data Source Select MS Access Driver Click Finish

Mark Dixon Page 13 How to: Create Data Source Type Data Source Name Click Select button

Mark Dixon Page 14 How to: Create Data Source Locate Database Select Database Click OK

Mark Dixon Page 15 standard library access relational databases –establish connection to database –initiating queries –create stored (parameterised) queries –The data structure of query result (table) Determining the number of columns Looking up metadata, etc. –located in java.sql package Note: JDBC not officially an acronym –unofficially “Java Database Connectivity” JDBC

Mark Dixon Page 16 Example: People <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> Import JDBC class People.jsp

Mark Dixon Page 17 Example: People 1 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> load driver People.jsp

Mark Dixon Page 18 Example: People 2 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> connection People.jsp

Mark Dixon Page 19 Example: People 3 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> statement People.jsp

Mark Dixon Page 20 Example: People 4 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> results People.jsp

Mark Dixon Page 21 Example: People 5 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> process People.jsp

Mark Dixon Page 22 Example: People 6 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> close connection People.jsp

Mark Dixon Page 23 Embedding html in java <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> Need data on separate lines html by hand put br tags between data

Mark Dixon Page 24 Embedding html in java <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> error – java does not understand html

Mark Dixon Page 25 Embedding html in java <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = " " ; while(r.next()){ html += r.getString("Surname"); } cn.close(); %> runs, but in wrong place Need double quotes around tag java sees html as literal string

Mark Dixon Page 26 Embedding html in java <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname") ; } cn.close(); %> Move br tag inside loop. Which bit of code pulls data from database?

Mark Dixon Page 27 Embedding html in java <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname") + " " ; } cn.close(); %> Move br tag inside loop after field data

Mark Dixon Page 28 Embedding html in java use view source – see what html has been created:

Mark Dixon Page 29 Embedding html in java html must be string (inside double quotes) follows normal pattern for expressions: data operator data operator s = s + " " + r.getString("Name") + " "

Mark Dixon Page 30 Embedding html in java (errors) s = s + " " r.getString("Gender") + " " missing operator s = s + r.getString("Height") + " missing double quote s = s + + r.getString("Height") html tag must be inside double quotes s = s + " " + r.getString(" Height") looks for field in database called Height

Mark Dixon Page 31 Questions: HTML in java Are these correct (assume variables and fields exist)? g = g + r.getString("Surname ") h = h + " " rs.getString("Width") a = " " + a + " " html = html + " " h = + h + " "   

Mark Dixon Page 32 Example: People v2 Display Surname of Male people: <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ if (r.getBoolean("Gender")){ html += r.getString("Surname") + " "; } cn.close(); %>

Mark Dixon Page 33 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery(" SELECT * FROM Person WHERE Gender = TRUE; "); String html = ""; while(r.next()){ html += r.getString("Surname") + " "; } cn.close(); %> Example: People v3 Display Surname of Male people: SQL statement

Mark Dixon Page 34 SQL: Queries main purpose of databases: –get information back out: searching Structured Query Language –dedicated to interacting with databases 3 rd Generation Language (such as VB, C++) –code describes how to do task 4 th Generation Language (such as SQL) –code describes what to do (not how to do it)

Mark Dixon Page 35 SQL: SELECT statement SELECT statement –used to get data –can be embedded in VB, via rs.Open: rs.Open("Person", cs) rs.Open("SELECT * FROM [Person]", cs) all fields

Mark Dixon Page 36 SQL: WHERE & ORDER BY WHERE clause –used to restrict data SELECT * FROM [People] WHERE [age]>=18; ORDER BY clause –used to change order of data SELECT * FROM [People] ORDER BY [Surname];

Mark Dixon Page 37 SQL: strings (text data) Possible confusion: SELECT * FROM Person WHERE Surname = Smith this will look for field called Smith - gives error need single (SQL) quotes to signify literal text SELECT * FROM Person WHERE Surname = 'Smith'

Mark Dixon Page 38 SQL & MS access queries MS Access –Queries: select data from database –really SQL select statements –can use queries to test SQL code MS Access: People.mdb

Mark Dixon Page 39 Questions: SQL Create an SQL statement to extract Track Title of records by Aerosmith Track TitleArtist NameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track SELECT [Track Title] FROM Track WHERE [Artist Name] = 'Aerosmith'; MS Access: Music.mdb

Mark Dixon Page 40 Questions: SQL Create an SQL statement to extract all fields of songs by Disturbed, ordered by track name Track TitleArtist NameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track SELECT * FROM Track WHERE [Artist Name] = 'Disturbed' ORDER BY [Track Title]; MS Access: Music.mdb

Mark Dixon Page 41 Example: People v4 User controls what is displayed:

Mark Dixon Page 42 SQL: DISTINCT records SELECT [Artist Name] FROM [Track]; Artist Name Black Sabbath Aerosmith Alien Ant Farm Disturbed Artist Name Black Sabbath Aerosmith Alien Ant Farm Disturbed SELECT DISTINCT [Artist Name] FROM [Track];

Mark Dixon Page 43 Tutorial Exercise: People Task 1: Create your own People database: –Open MS Access –Create a new database file –Create a new table –Create fields –Enter data Task 2: Get the People v1 example (from the lecture) working. Task 3: Modify your page so that it displays phone number as well as the person's name. Task 4: Modify your page so that it displays the data in an html table. Task 5: Modify your page so that records for males are displayed in blue, and records for females are displayed in red. Task 6: Modify your page as per version 4. You will need to: –Add a form to the page, and three submit buttons –In your code, detect when a button has been pressed (have a look at previous weeks) Task 7: Modify your page so that the user can type a letter, and only names starting with that letter are displayed. Task 8: Modify your page so that the user can type a series of numerical digits and only phone numbers containing those digits are displayed. Task 9: Modify your code so that the user can order the data by surname, or address (You may want to use a Query String)