CSE 308. Database Issues. Goals. Separate the application code from the database

Similar documents
Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Top 50 JDBC Interview Questions and Answers

JDBC SHORT NOTES. Abstract This document contains short notes on JDBC, their types with diagrams. Rohit Deshbhratar [ address]

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

PERSİSTENCE OBJECT RELATİON MAPPİNG

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

Programming in Java

Introduction to MySQL. Database Systems

JDBC MOCK TEST JDBC MOCK TEST IV

Working with Databases and Java

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

Lecture 9&10 JDBC. Mechanism. Some Warnings. Notes. Style. Introductory Databases SSC Introduction to DataBases 1.

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

SNS COLLEGE OF ENGINEERING, Coimbatore

UNIT III - JDBC Two Marks

SQL in a Server Environment

Acknowledgments About the Authors

Databases and MySQL. COMP 342: Programming Methods. 16 September Databases and MySQL

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Chapter 9 SQL in a server environment

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

JDBC. Sun Microsystems has included JDBC API as a part of J2SDK to develop Java applications that can communicate with databases.

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

Introduction JDBC 4.1. Bok, Jong Soon

Supports 1-1, 1-many, and many to many relationships between objects

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

JAVA AND DATABASES. Summer 2018

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

Unit 3 - Java Data Base Connectivity

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 16: Databases

The Object-Oriented Paradigm. Employee Application Object. The Reality of DBMS. Employee Database Table. From Database to Application.

How to design a database

SQL: Programming Midterm in class next Thursday (October 5)

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

1. PhP Project. Create a new PhP Project as shown below and click next

Database Application Development

CSE 308. UML Overview Use Case Diagrams. Reference. en.wikipedia.org/wiki/class_diagram. Robert Kelly, B. Bruegge,

CSE 308. UML Overview Use Case Diagrams. Reference. Class diagrams. Session 6 UML Intro/Use cases. Robert Kelly, B. Bruegge,

Lecture 2. Introduction to JDBC

Database Foundations. 5-1 Mapping Entities and Attributes. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Efficient Object-Relational Mapping for JAVA and J2EE Applications or the impact of J2EE on RDB. Marc Stampfli Oracle Software (Switzerland) Ltd.

Real SQL Programming 1

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

JDBC - INTERVIEW QUESTIONS

Extracts from Intro to Db - Jdbc - JPA SpringData

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Several major software companies including IBM, Informix, Microsoft, Oracle, and Sybase have all released object-relational versions of their

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

Appendix A - Glossary(of OO software term s)

CSCE Java. Dr. Chris Bourke. Prior to Lab. Peer Programming Pair-Up. Lab 15 - Databases & Java Database Connectivity API

3344 Database Lab. 1. Overview. 2. Lab Requirements. In this lab, you will:

Self-test Database application programming with JDBC

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

Chapter 13 Introduction to SQL Programming Techniques

Data Base Concepts. Course Guide 2

SQL DML and DB Applications, JDBC

JDBC. Oracle ODBC SP API SP API. SQL server C function calls. SQL server ODBC SP API. Oracle DSN Oracle ODBC Oracle

Why use a database? You can query the data (run searches) You can integrate with other business systems that use the same database You can store huge

INTRODUCTION TO JDBC - Revised Spring

O ne of the most important features of JavaServer

The dialog boxes Import Database Schema, Import Hibernate Mappings and Import Entity EJBs are used to create annotated Java classes and persistence.

UNIT-3 Java Database Client/Server

Database Design and Programming

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

CS221 Lecture: Java Database Connectivity (JDBC)

Essay Question: Explain 4 different means by which constrains are represented in the Conceptual Data Model (CDM).

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

What is database? Types and Examples

Java application using JDBC to connect to a database.

THE RELATIONAL DATABASE MODEL

Entity Relationship Diagram (ERD) Dr. Moustafa Elazhary

DATABASES SQL INFOTEK SOLUTIONS TEAM

Database Application Development

Introduction to MySQL. Database Systems

Intoduction to JDBC (Java Database Connectivity-- Connecting to DBMS)

Announcements. SQL: Part IV. Transactions. Summary of SQL features covered so far. Fine prints. SQL transactions. Reading assignments for this week

COMP9321 Web Application Engineering

JDBC [Java DataBase Connectivity]

Pieter van den Hombergh. March 25, 2018

Understanding Impact of J2EE Applications On Relational Databases. Dennis Leung, VP Development Oracle9iAS TopLink Oracle Corporation

INTRODUCTION TO JDBC - Revised spring

Introduction to SQL & Database Application Development Using Java

COMP9321 Web Application Engineering

Client/Server-Architecture

TopLink Grid: Scaling JPA applications with Coherence

T Technical specification: FASTAXON Group: Muuntaja

Lightweight J2EE Framework

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 4 Entity Relationship (ER) Modeling

Transcription:

CSE 308 Database Issues The following databases are created with password as changeit anticyber cyber cedar dogwood elm clan Goals Separate the application code from the database Encourages you to think objects not relational Requires you to defer the design of the DB Allows you to modify the design of the DB without changing all your application code Consistent application-level view of data Portability of DBMS (change DBMS vendor without rewriting a substantial part of application) Easy access to test data and GUI-related data Good DB performance 2 1

References Object-Relational Mapping SW en.wikipedia.org/wiki/list_of_object-relational_mapping_software TopLink-Reference implementation of JPA htten.wikipedia.org/wiki/toplink JDBC www.tutorialspoint.com/jdbc/index.htm docs.oracle.com/cd/e11882_01/java.112/e16548/toc.htm docs.oracle.com/cd/b28359_01/java.111/b31224.pdf en.wikipedia.org/wiki/java_database_connectivity API java.sql and javax.sql Standard reference 3 Approaches Create data classes corresponding to domain classes Create a robust persistence layer Use a persistence layer framework (e.g., Hibernate. Java Persistence API, etc) Don t write DB access code directly in your JSPs (including tags) Good to get experience with a persistence framework We deal with JPA in a different session 4 2

Your Own Persistence Layer You decide which of your classes and which attributes of those classes need to persist (i.e., stored in DB) Ideally, you should just need to send the following messages to an object to have the desired result save retrieve delete Allows you to build your application before you complete your DB Allows you to insert simple test data in domain objects 5 Approach incremental builds Approach to Testing DB access does not need to be in early builds Entity class getter methods (e.g., getbooktitle) can be initially implemented with simple test data 6 3

Database Preliminaries Recap important topics Recap terminology Use a good DB modelling tool (e.g., Workbench) You will implement the DB on a shared CS server (e.g., MySQL) You might do some early testing on a DB on your system (e.g., Derby) 7 An Entity Usually corresponds to something concrete in the domain of the application Represented by a rectangle An instance is a particular occurrence of an entity (corresponds to a row in a DB table) Entity name Shares 8 4

Attributes Also referred to as properties An attribute is a discrete data element that describes an entity Attribute names should be meaningful Shares attributes code name price quantity dividend pe 9 Identifiers (Primary Keys) Every instance of an entity (think row of a table) must be uniquely identified An identifier (primary key) can be one or more attributes Better to use an identifier that does not relate to a domain attribute (guaranteed uniqueness) A leading asterisk denotes an identifier (sometimes, another notation is used, e.g., PK) Shares *code name price quantity dividend pe Shares *ID code name price quantity dividend pe 10 5

DB Naming Conventions No universal standard Good to be consistent within a project Camel case used frequently 11 CSE308 DB Naming Conventions Camel case for table names (upper cc) and column names (lower cc) Table names plural (unlike OO convention) Column names singular Primary key field ID Avoid acronyms and abbreviations except where well known (e.g., PI for Principal Investigator) 12 6

A technique for modeling data We assume Data Modeling RDM Model (Relational Data Model) The goal is to identify the structure of data to be stored in the database ER Model is applicable to non-relational DB, but we assume a relational implementation 13 Entity Attribute Relationship Identifier The Building Blocks Entity name Shares Remember our notation is plural entity name in upper camel case and singular attribute names in lower camel case attributes code name price quantity dividend pe 14 7

A Well-Formed Data Model Follow organization (e.g., your company) convention No ambiguity All entities, attributes, relationships, and identifiers are defined Names are meaningful to the client 15 Relationships ERD and RDM show relationships between entities 1-1 1-many Recursive ERD shows Many-many No foreign keys RDM usually shows Associative entity (in-between table) Foreign keys Workbench uses more of a DB model style (not an ERD) 16 8

RDM Iteration You will usually iterate on your RDM model, as you learn the entities and relationships better Is the level of detail correct? Do the names represent the real-world entities and attributes? Are the model components situated so that the design is clear (e.g., no hidden or crossing lines) Are all exceptions handled? Is the model accurate? 17 Normalization A theoretical foundation for the relational model Application of a series of rules that gradually improve the design Minimize redundancy Minimize dependency Objectives* Free the collection of relations from undesirable insertion, update and deletion dependencies Isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database * Wikipedia 18 9

Normal Forms Based on rules about relationships among the columns of a table Removes data redundancies that can cause update anomalies A classification of relations 1NF 2NF 3NF BCNF 4NF 5NF Usually, only the first 3 normal forms are applied to a DB 19 Data Redundancy Major aim of relational database design is to group columns into tables to: 1. minimize data redundancy and 2. reduce file storage space required by implemented base tables Problems associated with data redundancy are illustrated in the example on the following slides 20 10

StaffDistributionCenters Table Note the details of a distribution center are repeated for every employee (not normal form) 21 Update Anomalies Tables that contain redundant information may potentially suffer from update anomalies Types of update anomalies include: Insertion how do you insert details of a new distribution center that has no employees? Deletion when we delete the last employee in a distribution center, we lose the information about the distribution center Modification changes to a distribution center must be made for all records containing that distribution center 22 11

Better Design 23 First Normal Form (1NF) All rows must have the same number of columns Single valued attributes only No universal agreement as to what would disqualify a table from being in 1NF Typical violation of 1NF Resist the temptation to include repeated fields as CSV text 24 12

Example Table not 1NF Repeated field 25 Converting to 1NF Replace a repeating group with a foreign key relationship 26 13

Second Normal Form (2NF) Violated when a non-key column is a fact about part of the primary key A column is not fully functionally dependent on the primary key customer-credit in this case order Mainly applies to tables with multiple natural keys itemno customerid quantity customer-credit 12 57 25 OK 34 679 3 POOR 27 Third Normal Form (3NF) Violated when a non-key column is a fact about another non-key column, restated as A column is not fully functionally dependent on the primary key stock stock code nation exchange rate MG USA 0.67 IR AUS 0.46 Exchange rate is a fact about a nation 28 14

Example - not 3NF Values in daddress and dtelno can be determined from dcenterno Values in staffno, staffname, position and salary are determined from ID 29 Interface Issues Application code JDBC Application code ODBC The application deals with objects, language specific data types, and higher level concepts The DB deals with relational tables and SQL Translation is usually performed to allow the two components to work together DB Translation required DB code and application libraries 30 15

ODBC Open Database Connectivity (ODBC) provides a standard software API (C) to a DBMS An ODBC implementation contains a library, one or more drivers, and DB support Library provides a general abstraction to the DB Drivers contain DB specific detail Adaptive programming techniques (e.g., introspection) are often required Introspection is the ability of a program to examine the type or properties of an object at runtime. Reflection, is the ability for a program to manipulate the values, metadata, properties and/or functions of an object at runtime. 31 JDBC Java Database Connectivity (JDBC) provides a standard software API (Java) to a DBMS A JDBC implementation contains a library, one or more drivers, and DB support J2SE contains JDBC libraries (java.sql, javax.sql) and a bridge to ODBC JDBC JDBC 4.2 is the most recent 32 16

Old approach (still supported) Drivers are loaded dynamically Drivers Class.forName( "com.somejdbcvendor.theirjdbcdriver" ); When the driver is loaded it registers with DeviceManager New approach DataSource ConnectionPoolDataSource 33 Driver Types Type 1 JDBC-ODBC Bridge (used when no pure-java driver is available) Type 2 Native API (converts JDBC calls into native DBMS calls, usually using some non-java code) Type 3 Network Protocol driver (uses a middle tier application server to convert data) Type 4 Native-protocol driver (pure-java) 34 17

JDBC Connections You get a connection to the DB through the DataSource or DriverManager (remember, your driver is registered with the DriverManager) Connection conn = DriverManager.getConnection( "jdbc:somejdbcvendor:other data needed by jdbc vendor", "mylogin", "mypassword" ); Connection pooling supported since JDBC 3.0 35 JDBC Abstractions Connection (and connection pooling) Statement Statement PreparedStatement CallableStatement Transaction Result set Others 36 18

Typical JDBC Operation Sequence Open a connection to the DB Prepare a statement object Execute the statement query (e.g., executequery or executeupdate methods of Statement object) Query returns a ResultSet object Use the next method of the ResultSet object to iterate through the results Close the ResultSet and Statement objects Similar approach when making updates to DB 37 Means for a server to communicate with a DB server In many DBMS, a connection is a heavy action Connection pooling is often used to reduce overhead of connections JDBC Connection 38 19

Connection Pooling Technique used to mitigate the overhead in repeatedly opening and closing DB connections Approach Pool of connections is created Requests for connections are made to the object holding the pool of connections by the application Application returns (releases) the connection to the pool when DB access is completed Implementation JDBC support Do it yourself 39 Transactions Unit of work within a DBMS Goals Keep a database consistent, even in the event of a system failure Synchronize access in the case of multiple simultaneous access to the DB Typical steps Begin the transaction Execute a set of data manipulations and/or queries If no errors occur then commit the transaction and end it If errors occur then roll back the transaction and end it 40 20

ResultSet Essentially a table resulting from a query Supports methods to allow iteration through the table Maintains a cursor that points to the current row in the result set Supports various navigation methods 41 System Issues Which DBMS? Which DBMS server (yours or CS shared)? What is your approach to connection pooling? What is your approach to object/relational mapping? What is your data migration plan? What is your DB population plan? Can you switch between a test DB and a live DB? 42 21