Chapter 8 Databases.

Slides:



Advertisements
Similar presentations
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Advertisements

JDBC – Java DataBase Connectivity CSE432 Object Oriented Software Engineering.
Java and Databases CS-328 Dick Steflik. Database Drivers Think of a database as just another device connected to your computer like other devices it has.
1 JDBC Java Database Connectivity. 2 c.pdf
Object Oriented Programming Java Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent:
Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
CSCI 6962: Server-side Design and Programming
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Think Possibility Integrating Web Applications With Databases.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Database Programming in Java Corresponds with Chapter 32, 33.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
JDBC Establish a connection with a database or access any tabular data source Send SQL statements Process the results Two major sets of interfaces: JDBC.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Structured Query Language 1. Data Definition Language (DDL) is used to manage table and define data structure i.e. CREATE, ALTER, DROP Data Control.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Web Programming Assistant Professor Xiaozhong Liu
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
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.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JDBC – Java DataBase Connectivity
CS3220 Web and Internet Programming Database Access with JDBC
Lec - 14.
Chapter 5 Introduction to SQL.
Course Outcomes of Advanced Java Programming AJP (17625, C603)
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
JDBC.
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
JDBC – Java DataBase Connectivity
JDBC – Java DataBase Connectivity
Introduction to Server-Side Web Development using JSP and Databases
Interacting with Database
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
JDBC – Java DataBase Connectivity
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Chapter 8 Databases

Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use Java/JDBC for accessing relational databases Explain and apply basic principles of good database design

RDBMS A Relational Database Management System (RDBMS) provides data storage and access for web applications

Relation relation is a mathematical term that refers to an ordered set of values drawn from different domains Ex: a relation on numbers x letters x symbols (55, A, #)

Database Structure A database includes one or more tables Each table represents one type of entity Example: Tables in a Library Database Patron Loan (transaction) Recording Book

Database Structure Each table field represents an entity attribute Each table row represents one entity Car table: Year Make Model Color 1973 Volkswagen Jetta Red 1992 Ford Aerostar Blue 2004 Chevrolet Suburban Black row field

Structured Query Language SQL is a standard language for creating and maintaining relational databases SQL statement types: data definition: create databases and tables data manipulation: add, modify, delete data data control: set access permissions

Basic SQL Statements Data definition Data manipulation Data control CREATE, DROP Data manipulation SELECT, INSERT, UPDATE, DELETE Data control GRANT, REVOKE

CREATE Create a database or a table CREATE DATABASE ehsl CREATE ehsl.player ( playerNr int PRIMARY KEY, name VARCHAR(30), isCurrent BOOLEAN NOT NULL)

Basic SQL Data Types INTEGER DECIMAL(T, R) FLOAT CHAR(N) N characters T=total digits, R=right digits (after '.') FLOAT CHAR(N) N characters VARCHAR(N) up to N characters BOOLEAN DATE TIME

DROP DROP can be used to delete an entire database or a table DROP ehsl DROP ehsl.player

SELECT SELECT retrieves data from a database SELECT field-list FROM database.table WHERE condition ORDER BY field-list field-list is a comma-separated list of fields from the named table (* means "all fields") condition is a Boolean condition using field names and/or constants

SELECT Example SELECT * FROM ehsl.player SELECT playerNr, name FROM ehsl.player WHERE isCurrent=TRUE SELECT playerNr, name, status FROM ehsl.player WHERE playerNr >= 90001 ORDER BY status, name

INSERT INSERT adds a new row to a table INSERT INTO ehsl.player VALUES (23752, 'Jane Doe', TRUE)

UPDATE UPDATE changes one or more rows UPDATE database.table SET field-assignment-list WHERE condition UPDATE ehsl.player SET isCurrent=TRUE WHERE playerNr=33256

DELETE DELETE removes one or more rows from a table DELETE FROM database.table WHERE condition DELETE FROM ehsl.player WHERE playerNr=33523

Warning UPDATE and DELETE without a WHERE clause will affect all rows! UPDATE ehsl.player SET isCurrent=true DELETE FROM ehsl.player Change all rows! Delete all rows!

GRANT GRANT can be used to give access permissions to users GRANT ALL PRIVILEGES ON database.table TO user-name user-name is formatted as user@host, for example 'jonesac'@'localhost'

REVOKE REVOKE can be used to eliminate access permissions REVOKE ALL PRIVILEGES on database.table FROM user-name REVOKE ALL PRIVILEGES on ehsl.player FROM 'jonesac'@'localhost'

Create User The CREATE command can also be used to create new users CREATE USER user-name IDENTIFIED BY password CREATE USER 'jonesac'@'localhost' IDENTIFIEC BY 'abc123#'

JDBC Java Database Connectivity (JDBC) is a Java API that allows Java programs to interact with relational database management systems Interaction also requires a database driver, which translates JDBC commands to procedure calls on the RDBMS RDBMS Application Program Driver JDBC

JDBC – Load Driver The first step is to load the database driver Usually provided by the RDBMS vendor String driverClassName = "com.mysql.jdbc.Driver"; try { Class.forName(driverClassName); } catch (ClassNotFoundException cnfe) { ….

JDBC – Execute Query (1/2) To execute an SQL Query: String query = "..."; Vector<String> colNames = new Vector<String>, result = new Vector<String>; try { Connection con = DriverManager.getConnection( dbUrl, dbUserId, dbPassword); Statement st = con.createStatement(); ResultSet rs = st.executeQuery(query); ResultSetMetaData md = rs.getMetaData();

JDBC – Execute Query (2/2) To execute an SQL Query: // get column names for (int i = 1; i <= md.getColumnCount(); i++) { colNames.addColumnName(md.getColumnName(i)); } // get field values while (rs.next()) { for (int i = 1; i<=md.getColumnCount(); i++) { result.addFieldValue(rs.getString(i)); con.close(); } catch (SQLException s) { ... access the next row of the table access the next field of the row

JDBC – Execute Command To execute an SQL command: int result = 0; String command = "..."; try { con = DriverManager.getConnection( dbUrl, dbUserId, dbPassword); stmt = con.createStatement(); result = stmt.executeUpdate(command); con.close(); } catch (SQLException s) { ... } result = number of rows affected (inserted, modified, or deleted)

JDBC Design An effective design for database access: JSP: user interface presentation Java Servlet: application logic Java Bean: database access (JDBC) Java Bean access RDBMS using JDBC JSP create user interface dependency RDBMS Java Servlet process / prepare data

Database Design Principles Each field should contain a single value Repeated fields with empty values should be made a separate table Each table should represent only one entity

Example: Registration multiple valued field WRONG: Better: empty fields

Example: Registration Better still: multiple entities: class registration / class name

Example: Registration RIGHT:

Review Relational Database / RDBMS SQL JDBC Database Design Principles Data Definition Data Manipulation Data Control JDBC Database Driver Query Execution Command Execution Database Design Principles