Advanced Java Class Java Database Connectivity (JDBC)

Slides:



Advertisements
Similar presentations
Accounting System Design
Advertisements

Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
1 Introduction to Web Application Introduction to Data Base.
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Chapter 14 & 15 Conceptual & Logical Database Design Methodology
Database Systems Lecture 5 Natasha Alechina
Phil Brewster  One of the first steps – identify the proper data types  Decide how data (in columns) should be stored and used.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Oracle for Software Developers. What is a relational database? Data is represented as a set of two- dimensional tables. (rows and columns) One or more.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
What is JDBC? Java Database Connectivity (JDBC) is an API for the Java programming language that defines how a client may access a database. provides.
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.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
ABC Insurance Co. Paul Barry Steve Randolph Jing Zhou CSC8490 Database Systems & File Management Dr. Goelman Villanova University August 2, 2004.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
1 What is database 2? What is normalization? What is SQL? What is transaction?
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
JDBC – Java Database Concentricity
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
DAT602 Database Application Development Lecture 2 Review of Relational Database.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
1 Session 2 Module 3: Scrollable Resultset and Rowsets.
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Session 1 Module 1: Introduction to Data Integrity
What is MySQL? MySQL is a relational database management system (RDBMS) based on SQL (Structured Query Language). First released in January, Many.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Slide 1 Chapter 7 – Part 3 Stored Procedure, Function &Trigger.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
DEPTT. OF COMP. SC & APPLICATIONS
Designing Tables for a Database System
Translation of ER-diagram into Relational Schema
Chapter 8 Working with Databases and MySQL
Interacting with Database
Chapter 7 Using SQL in Applications
Presentation transcript:

Advanced Java Class Java Database Connectivity (JDBC)

Question from yesterday Please talk a little about “security.policy” –java –Djava.security.policy=policyfilename –It will look for a default file in java.home/lib/security/java.policy –Or you can supply your own: java –Djava.security.policy=policyfilename Example: my RMISecurity.policy file: grant { permission java.net.SocketPermission "*: ","connect,accept"; }; Also, you can use the policytool program that comes with the JDK to write your own security file. See java.security.Permissions class for more documentation

Policy for Assignment 1a As written in an yesterday, your WebCrawler will only be tested on html pages that are xhtml pages. In other words, you may assume that the links will be well-formed.

PostGresQL Syntax You need to know a little PostGresQL syntax to set up your DB and use JDBC Types: see p. 623, figure 9-6 –Also in postgresQL: money, text (among others) Documentation: Look at \h and \? for command syntax At the command line: –psql –pg_dump –man psql

Common Table Commands CREATE TABLE table_name ( column_name1 column_type1, column_name2 column_type2, etc.); Can also specify a DEFAULT value, or other constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, etc. \dt (shows tables) \d table_name (describes that table) DROP TABLE table_name; ALTER TABLE table_name RENAME TO new_table_name;

Common Column Commands ALTER TABLE table_name –ADD column_name column_type [constraints]; –ALTER column_name SET DEFAULT value; –ALTER column_name DROP DEFAULT; –RENAME old_column_name TO new_column_name;

Common Row Commands INSERT INTO table_name values (42, 3, ‘foo’) INSERT INTO table_name (col2_name, col3_name) values (3, ‘foo’); UPDATE table_name SET col = expression [WHERE condition]; DELETE FROM table_name [WHERE condition];

Common “\” Commands \? Shows all “\” commands \h shows help menu \h COMMAND_NAME shows help for a specific command \q quits psql program

Driver Types 1.JDBC-ODBC bridge (inefficient) 2.Most efficient driver if all classes using db are on the db host 3.Most efficient driver if not all classes using db are on the db host 4.pure Java, and therefore platform independent A driver for your first project is linked from the assignment page. For many databases, there may be only one choice, not all four.

Basic use of java.sql 1.Load driver class 2.Get connection 3.Get statement 4.Ask statement to execute sql string 5.Process results if needed 6.Close Statement and Connection

Optional arguments for getting Statement Scrollable? –TYPE_FORWARD_ONLY [Default] (note error in text: TYPE_READ_ONLY) –TYPE_SCROLL_INSENSITIVE [ignores changes made to ResultSet by other code] –TYPE_SCROLL_SENSITIVE Concurrency? –CONCUR_READ_ONLY [rs can’t change while processing it] –CONCUR_UPDATABLE [not allowed by some DB drivers]

Execution of SQL by Statement int Statement.executeUpdate(String sql); –Returns number rows affected –Good for INSERT, UPDATE, and DELETE ResultSet Statement.executeQuery(String sql); –Good for SELECT –Good if only one ResultSet is returned boolean execute(String sql); –Returns true if a ResultSet(s) was returned –Good if more than one ResultSet might be returned –Very rare to do this.

Processing Result Sets Move to appropriate row –beforeFirst(), next()) Process values at that row –get values (methods are named by type) –update values [local], then updateRow() [db] –deleteRow() –insertRow()

Advanced Efficiency Options 1.Prepared Statements [use if many sql statements vary only in the literal values] – SEE NEXT SLIDE 2.Stored Procedures –Creating them varies from db to db – not covered in this class –Can use them in Java via CallableStatement –Can produce more than one ResultSet 3.Use fewer Connections –If single threaded program, open a Connection and keep it, rather than closing it and opening a new one constantly –Connection Pooling is provided by DataSource objects for J2EE

Prepared Statements – Very Useful! PreparedStatement ps = Connection.prepareStatement( “INSERT INTO my_table (col_a, col_b) VALUES (?, ?)” ); ps.setString(1, “foo”); ps.setInt(2, 512); ps.executeUpdate() or ps.executeQuery(); (note: indexing starts from 1)

Good Database Programming Program layers (i.e. Model-View- Persistence) Define mapping between Java Objects and Data Elements Utilize optimizations and special powers of db Program transactions

Program Layers (and not just in JDBC) See p. 613, figure 9-1 Conceptually clearer Layers of security Scalability Abstraction of implementations

Define mapping between Java Objects and Data Elements Relationships between Relational Database Concepts and Object Oriented Programming Concepts –See figure 9-2 Mappings (see 9-4 and 9-5) –One-to-one (use same ids) –One-to-many (give each of the many a reference to the one, using a foreign key) –Many-to-many (make another table just for the mappings – this table will have 2 foreign keys)

Utilize optimizations and special powers of DB serial “types” DB can check for uniqueness of primary key Foreign key constraints signal error for impossible values Set up dependency rules for deletes and modifications (i.e. DB will automatically nix employee records when a department is deleted if the foreign key is defined with “cascade delete” WARNING: if you depend on these too much, you may have difficulty switching Databases.

Program For Transactions Goal: –Atomic [all or nothing] –Consistent [same each time you run it under same conditions] –Isolated [independent of other transactions] (several possible levels of isolation) –Durable [permanent once commited]

Program For Transactions Implementation –Connection.setAutoCommit(false) [true by default] –Connection.commit() or Connection.rollback() –Note: can also rollback to Savepoints within a transaction, as of Java 1.4

Group Database Task Draw and Entity Relationship diagram (see example on board) to show the structure of a DB for an online shopping application. –Show Foreign and Primary Keys –Show column names, SQL types, any restraints Classes in Application: –Customer: Has name and address. Has one or more accounts. –Account: has owner, balance, credit rating, value of last statement, and date of last payment. –Order: Knows what account it’s owned by. Also has date opened, date closed, status, and total value. Has one or more Items, and associates them with the quantity ordered. –Item: Has a part number, picture (image file), unit cost. –Inventory: list of the items available for sale. Associates each Item with a quantity in stock.